Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
12 KiB

  1. /****************************************************************************************************************
  2. FILENAME: GetReg.cpp
  3. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  4. */
  5. #include "stdafx.h"
  6. #ifndef SNAPIN
  7. #include <windows.h>
  8. #endif
  9. #include <stdio.h>
  10. #include "ErrMacro.h"
  11. #include "Message.h"
  12. /****************************************************************************************************************
  13. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  14. ROUTINE DESCRIPTION:
  15. This routine gets a value from the registry.
  16. INPUT + OUTPUT:
  17. IN OUT phKey - Handle to where to store the registry key if NULL, or a valid key if otherwise.
  18. IN cRegKey - If phKey is NULL, this specifies the name of the key to open.
  19. IN cRegValueName - The name of the value to get from phKey.
  20. OUT cRegValue - Where to write the value.
  21. IN OUT pdwRegValueSize - The size of the cRegValue buffer in bytes on entry; the number of bytes written on return.
  22. GLOBALS:
  23. None.
  24. RETURN:
  25. ERROR_SUCCESS - Success
  26. ERROR_... - Failure
  27. */
  28. LONG
  29. GetRegValue(
  30. IN OUT PHKEY phKey,
  31. IN PTCHAR cRegKey,
  32. IN PTCHAR cRegValueName,
  33. OUT PTCHAR cRegValue,
  34. IN OUT PDWORD pdwRegValueSize
  35. )
  36. {
  37. LONG lReturn;
  38. DWORD dwKeyType;
  39. //0.0E00 If no handle passed in then open the registry key and get a handle.
  40. if (*phKey == NULL) {
  41. //0.0E00 Open the registry
  42. if((lReturn = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  43. cRegKey,
  44. 0,
  45. KEY_QUERY_VALUE,
  46. &*phKey
  47. )) != ERROR_SUCCESS) {
  48. Message(TEXT("GetRegValue - RegOpenKeyEx"), lReturn, cRegKey);
  49. return lReturn;
  50. }
  51. }
  52. //0.0E00 Query the registry for the value
  53. if((lReturn = RegQueryValueEx(*phKey,
  54. cRegValueName,
  55. 0,
  56. &dwKeyType,
  57. (PUCHAR)cRegValue,
  58. &*pdwRegValueSize
  59. )) != ERROR_SUCCESS) {
  60. Message(TEXT("GetRegValue - RegQueryValueEx"), lReturn, cRegValueName);
  61. return lReturn;
  62. }
  63. if (*pdwRegValueSize > 1) {
  64. //0.0E00 Zero terminate the return string value.
  65. cRegValue[(*pdwRegValueSize)/sizeof(TCHAR) - 1] = 0;
  66. }
  67. else {
  68. cRegValue[0] = 0;
  69. lReturn = ERROR_BADKEY;
  70. }
  71. return lReturn;
  72. }
  73. /****************************************************************************************************************
  74. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  75. ROUTINE DESCRIPTION:
  76. This routine gets a value from the registry.
  77. INPUT + OUTPUT:
  78. IN OUT phKey - Handle to where to store the registry key if NULL, or a valid key if otherwise.
  79. IN cRegKey - If phKey is NULL, this specifies the name of the key to open.
  80. IN cRegValueName - The name of the value to get from phKey.
  81. OUT cRegValue - Where to write the value.
  82. IN OUT pdwRegValueSize - The size of the cRegValue buffer in bytes on entry; the number of bytes written on return.
  83. GLOBALS:
  84. None.
  85. RETURN:
  86. ERROR_SUCCESS - Success
  87. ERROR_... - Failure
  88. */
  89. LONG
  90. GetRegValue(
  91. IN OUT PHKEY phKey,
  92. IN PTCHAR cRegKey,
  93. IN PTCHAR cRegValueName,
  94. OUT PLONGLONG cRegValue,
  95. IN OUT PDWORD pdwRegValueSize
  96. )
  97. {
  98. LONG lReturn;
  99. DWORD dwKeyType;
  100. //0.0E00 If no handle passed in then open the registry key and get a handle.
  101. if (*phKey == NULL) {
  102. //0.0E00 Open the registry
  103. if((lReturn = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  104. cRegKey,
  105. 0,
  106. KEY_QUERY_VALUE,
  107. &*phKey
  108. )) != ERROR_SUCCESS) {
  109. Message(TEXT("GetRegValue - RegOpenKeyEx"), lReturn, cRegKey);
  110. return lReturn;
  111. }
  112. }
  113. //0.0E00 Query the registry for the value
  114. if((lReturn = RegQueryValueEx(*phKey,
  115. cRegValueName,
  116. 0,
  117. &dwKeyType,
  118. (LPBYTE)&*cRegValue,
  119. &*pdwRegValueSize
  120. )) != ERROR_SUCCESS) {
  121. Message(TEXT("GetRegValue - RegQueryValueEx"), lReturn, cRegValueName);
  122. return lReturn;
  123. }
  124. return lReturn;
  125. }
  126. /*****************************************************************************************************************
  127. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  128. ROUTINE DESCRIPTION:
  129. This routine gets the NT registry subkey to the key and index value passed in.
  130. INPUT + OUTPUT:
  131. IN OUT phKey - Handle to where to store the registry key if NULL, or a valid key if otherwise.
  132. IN cRegKey - If phKey is NULL, this specifies the name of the key to open.
  133. IN dwIndex - The zero based index number of the sub key to enumerate.
  134. OUT cRegSubKey - A string to return the value in. (Must be at least MAX_PATH length).
  135. IN OUT dwRegSubKeySize - Size of the sub key register. On return this holds the size of the subkey returned.
  136. GLOBALS:
  137. None.
  138. RETURN:
  139. ERROR_SUCCESS or ERROR_NO_MORE_ITEMS - Success
  140. ERROR_... - Failure
  141. */
  142. LONG
  143. GetRegSubKey(
  144. IN OUT PHKEY phKey,
  145. IN PTCHAR cRegKey,
  146. IN DWORD dwIndex,
  147. OUT PTCHAR cRegSubKey,
  148. IN OUT PDWORD pdwRegSubKeySize
  149. )
  150. {
  151. LONG lReturn;
  152. FILETIME ftLastWriteTime;
  153. //0.0E00 If no handle passed in then open the registry key and get a handle.
  154. if (*phKey == NULL) {
  155. if((lReturn = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  156. cRegKey,
  157. 0,
  158. KEY_ENUMERATE_SUB_KEYS,
  159. &*phKey
  160. )) != ERROR_SUCCESS) {
  161. Message(TEXT("GetRegSubKey - RegOpenKeyEx"), lReturn, cRegKey);
  162. return lReturn;
  163. }
  164. }
  165. //0.0E00 Get the sub key.
  166. if((lReturn = RegEnumKeyEx(*phKey,
  167. dwIndex,
  168. cRegSubKey,
  169. &*pdwRegSubKeySize,
  170. NULL,
  171. NULL,
  172. NULL,
  173. &ftLastWriteTime
  174. )) != ERROR_SUCCESS && lReturn != ERROR_NO_MORE_ITEMS) {
  175. Message(TEXT("GetRegSubKey - RegEnumKeyEx"), lReturn, cRegSubKey);
  176. }
  177. return lReturn;
  178. }
  179. /****************************************************************************************************************
  180. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  181. ROUTINE DESCRIPTION:
  182. This routine sets a value in the registry. And creates the registry key if it does not
  183. already exist.
  184. INPUT + OUTPUT:
  185. IN OUT phKey - Handle to where to store the registry key if NULL, or a valid key if otherwise.
  186. IN cRegKey - If phKey is NULL, this specifies the name of the key to open.
  187. IN cRegValueName - The name of the value to save under phKey.
  188. OUT cRegValue - Contains the value to write.
  189. IN OUT pdwRegValueSize - The number of bytes to write.
  190. IN dwKeyType - The type of value to write (e.g. REG_SZ).
  191. GLOBALS:
  192. None.
  193. RETURN:
  194. ERROR_SUCCESS - Success
  195. ERROR_... - Failure
  196. */
  197. LONG
  198. SetRegValue(
  199. IN OUT PHKEY phKey,
  200. IN PTCHAR cRegKey,
  201. IN PTCHAR cRegValueName,
  202. OUT PTCHAR cRegValue,
  203. IN DWORD dwRegValueSize,
  204. IN DWORD dwKeyType
  205. )
  206. {
  207. LONG lReturn;
  208. TCHAR cClass[] = TEXT("");
  209. DWORD dwDisposition;
  210. //0.0E00 If no handle passed in then open the registry key and get a handle.
  211. if (*phKey == NULL) {
  212. // 1.0E00 Create the key if it does not exist otherwise open it.
  213. if((lReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE, // handle of an open key
  214. cRegKey, // address of subkey name
  215. 0, // reserved
  216. cClass, // address of class string
  217. REG_OPTION_NON_VOLATILE, // special options flag
  218. KEY_ALL_ACCESS, // desired security access
  219. NULL, // address of key security structure
  220. &*phKey, // address of buffer for opened handle
  221. &dwDisposition // address of disposition value buffer
  222. )) != ERROR_SUCCESS) {
  223. Message(TEXT("SetRegValue - RegOpenKeyEx"), lReturn, cRegKey);
  224. return lReturn;
  225. }
  226. }
  227. //0.0E00 Query the registry for the value
  228. if((lReturn = RegSetValueEx(*phKey,
  229. cRegValueName,
  230. 0,
  231. dwKeyType,
  232. (PUCHAR)cRegValue,
  233. dwRegValueSize
  234. )) != ERROR_SUCCESS) {
  235. Message(TEXT("SetRegValue - RegSetValueEx"), lReturn, cRegValueName);
  236. }
  237. return lReturn;
  238. }
  239. /****************************************************************************************************************
  240. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  241. ROUTINE DESCRIPTION:
  242. This routine sets a value in the registry. And creates the registry key if it does not
  243. already exist.
  244. INPUT + OUTPUT:
  245. IN OUT phKey - Handle to where to store the registry key if NULL, or a valid key if otherwise.
  246. IN cRegKey - If phKey is NULL, this specifies the name of the key to open.
  247. IN cRegValueName - The name of the value to save under phKey.
  248. OUT cRegValue - Contains the value to write.
  249. IN OUT pdwRegValueSize - The number of bytes to write.
  250. IN dwKeyType - The type of value to write (e.g. REG_SZ).
  251. GLOBALS:
  252. None.
  253. RETURN:
  254. ERROR_SUCCESS - Success
  255. ERROR_... - Failure
  256. */
  257. LONG
  258. SetRegValue(
  259. IN OUT PHKEY phKey,
  260. IN PTCHAR cRegKey,
  261. IN PTCHAR cRegValueName,
  262. IN LONGLONG cRegValue,
  263. IN DWORD dwRegValueSize,
  264. IN DWORD dwKeyType
  265. )
  266. {
  267. LONG lReturn;
  268. TCHAR cClass[] = TEXT("");
  269. DWORD dwDisposition;
  270. //0.0E00 If no handle passed in then open the registry key and get a handle.
  271. if (*phKey == NULL) {
  272. // 1.0E00 Create the key if it does not exist otherwise open it.
  273. if((lReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE, // handle of an open key
  274. cRegKey, // address of subkey name
  275. 0, // reserved
  276. cClass, // address of class string
  277. REG_OPTION_NON_VOLATILE, // special options flag
  278. KEY_ALL_ACCESS, // desired security access
  279. NULL, // address of key security structure
  280. &*phKey, // address of buffer for opened handle
  281. &dwDisposition // address of disposition value buffer
  282. )) != ERROR_SUCCESS) {
  283. Message(TEXT("SetRegValue - RegOpenKeyEx"), lReturn, cRegKey);
  284. return lReturn;
  285. }
  286. }
  287. //0.0E00 Query the registry for the value
  288. if((lReturn = RegSetValueEx(*phKey,
  289. cRegValueName,
  290. 0,
  291. dwKeyType,
  292. (LPBYTE)&cRegValue,
  293. dwRegValueSize
  294. )) != ERROR_SUCCESS) {
  295. Message(TEXT("SetRegValue - RegSetValueEx"), lReturn, cRegValueName);
  296. }
  297. return lReturn;
  298. }