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.

421 lines
9.6 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // REGISTRY.C
  4. //
  5. // Microsoft Confidential
  6. // Copyright (cMicrosoft Corporation 1998
  7. // All rights reserved
  8. //
  9. // Registry functions for the application to easily interface with the
  10. // registry.
  11. //
  12. // 4/98 - Jason Cohen (JCOHEN)
  13. //
  14. //////////////////////////////////////////////////////////////////////////////
  15. // Include files
  16. //
  17. #include <windows.h>
  18. #include "jcohen.h"
  19. #include "registry.h"
  20. BOOL RegExists(HKEY hKeyReg, LPTSTR lpKey, LPTSTR lpValue)
  21. {
  22. HKEY hOpenKey = NULL;
  23. BOOL bExists = FALSE;
  24. if (lpKey)
  25. {
  26. if (RegOpenKeyEx(hKeyReg, lpKey, 0, KEY_ALL_ACCESS, &hOpenKey) != ERROR_SUCCESS)
  27. return bExists;
  28. }
  29. else
  30. hOpenKey = hKeyReg;
  31. if (lpValue)
  32. bExists = (RegQueryValueEx(hOpenKey, lpValue, NULL, NULL, NULL, NULL) == ERROR_SUCCESS);
  33. else
  34. bExists = TRUE;
  35. if (lpKey)
  36. RegCloseKey(hOpenKey);
  37. return bExists;
  38. }
  39. BOOL RegDelete(HKEY hRootKey, LPTSTR lpSubKey, LPTSTR lpValue) {
  40. BOOL bSuccess = FALSE;
  41. if (lpValue) {
  42. if (lpSubKey) {
  43. HKEY hRegKey;
  44. if (RegOpenKeyEx(hRootKey, lpSubKey, 0, KEY_ALL_ACCESS, &hRegKey) == ERROR_SUCCESS) {
  45. bSuccess = (RegDeleteValue(hRegKey, lpValue) == ERROR_SUCCESS);
  46. RegCloseKey(hRegKey);
  47. }
  48. }
  49. else
  50. bSuccess = (RegDeleteValue(hRootKey, lpValue) == ERROR_SUCCESS);
  51. }
  52. else
  53. bSuccess = (RegDeleteKey(hRootKey, lpSubKey) == ERROR_SUCCESS);
  54. return bSuccess;
  55. }
  56. LPTSTR RegGetString(HKEY hKeyReg, LPTSTR lpKey, LPTSTR lpValue)
  57. {
  58. HKEY hOpenKey = NULL;
  59. LPTSTR lpBuffer = NULL,
  60. lpExpand = NULL;
  61. DWORD dwSize = 0,
  62. dwType;
  63. // If the key is specified, we must open it. Otherwise we can
  64. // just use the HKEY passed in.
  65. //
  66. if (lpKey)
  67. {
  68. // If the open key fails, return NULL because the value can't exist.
  69. //
  70. if (RegOpenKeyEx(hKeyReg, lpKey, 0, KEY_ALL_ACCESS, &hOpenKey) != ERROR_SUCCESS)
  71. return NULL;
  72. }
  73. else
  74. hOpenKey = hKeyReg;
  75. // Now query the value to get the size to allocate. Make sure the date
  76. // type is a string and that the malloc doesn't fail.
  77. //
  78. if ( ( RegQueryValueEx(hOpenKey, lpValue, NULL, &dwType, NULL, &dwSize) == ERROR_SUCCESS ) &&
  79. ( ( dwType == REG_SZ ) || ( dwType == REG_EXPAND_SZ ) ) &&
  80. ( lpBuffer = (LPTSTR) MALLOC(dwSize) ) )
  81. {
  82. // We know the value exists and we have the memory we need to query the value again.
  83. //
  84. if ( ( RegQueryValueEx(hOpenKey, lpValue, NULL, NULL, (LPBYTE) lpBuffer, &dwSize) == ERROR_SUCCESS ) &&
  85. ( ( dwType == REG_SZ ) || ( dwType == REG_EXPAND_SZ ) ) )
  86. {
  87. // We should expand it if it is supposed to be.
  88. //
  89. if ( dwType == REG_EXPAND_SZ )
  90. {
  91. if ( ( dwSize = ExpandEnvironmentStrings(lpBuffer, NULL, 0) ) &&
  92. ( lpExpand = (LPTSTR) MALLOC(dwSize * sizeof(TCHAR)) ) &&
  93. ( ExpandEnvironmentStrings(lpBuffer, lpExpand, dwSize) ) &&
  94. ( *lpExpand ) )
  95. {
  96. // The expand worked, so free the original buffer and return
  97. // the expanded one.
  98. //
  99. FREE(lpBuffer);
  100. lpBuffer = lpExpand;
  101. }
  102. else
  103. {
  104. // The expand failed see we should free everything up
  105. // and return NULL.
  106. //
  107. FREE(lpExpand);
  108. FREE(lpBuffer);
  109. }
  110. }
  111. }
  112. else
  113. // For some reason the query failed, that shouldn't happen
  114. // but now we need to free and return NULL.
  115. //
  116. FREE(lpBuffer);
  117. }
  118. // If we opened a key, we must close it.
  119. //
  120. if (lpKey)
  121. RegCloseKey(hOpenKey);
  122. // Return the buffer allocated, or NULL if something failed.
  123. //
  124. return lpBuffer;
  125. }
  126. LPVOID RegGetBin(HKEY hKeyReg, LPTSTR lpKey, LPTSTR lpValue) {
  127. HKEY hOpenKey = NULL;
  128. LPVOID lpBuffer = NULL;
  129. DWORD dwSize = 0,
  130. dwType;
  131. // If the key is specified, we must open it. Otherwise we can
  132. // just use the HKEY passed in.
  133. //
  134. if (lpKey)
  135. {
  136. // If the open key fails, return NULL because the value can't exist.
  137. //
  138. if (RegOpenKeyEx(hKeyReg, lpKey, 0, KEY_ALL_ACCESS, &hOpenKey) != ERROR_SUCCESS)
  139. return NULL;
  140. }
  141. else
  142. hOpenKey = hKeyReg;
  143. // Now query the value to get the size to allocate. Make sure the date
  144. // type is a string and that the malloc doesn't fail.
  145. //
  146. if ( ( RegQueryValueEx(hOpenKey, lpValue, NULL, &dwType, NULL, &dwSize) == ERROR_SUCCESS ) &&
  147. ( dwType == REG_BINARY ) &&
  148. ( lpBuffer = MALLOC(dwSize) ) )
  149. {
  150. // We know the value exists and we have the memory we need to query the value again.
  151. //
  152. if ( ( RegQueryValueEx(hOpenKey, lpValue, NULL, NULL, (LPBYTE) lpBuffer, &dwSize) != ERROR_SUCCESS ) ||
  153. ( dwType != REG_BINARY ) )
  154. // For some reason the query failed, that shouldn't happen
  155. // but now we need to free and return NULL.
  156. //
  157. FREE(lpBuffer);
  158. }
  159. // If we opened a key, we must close it.
  160. //
  161. if (lpKey)
  162. RegCloseKey(hOpenKey);
  163. // Return the buffer allocated, or NULL if something failed.
  164. //
  165. return lpBuffer;
  166. }
  167. DWORD RegGetDword(HKEY hKeyReg, LPTSTR lpKey, LPTSTR lpValue) {
  168. HKEY hOpenKey = NULL;
  169. DWORD dwBuffer,
  170. dwSize = sizeof(DWORD),
  171. dwType;
  172. if (lpKey) {
  173. if (RegOpenKeyEx(hKeyReg, lpKey, 0, KEY_ALL_ACCESS, &hOpenKey) != ERROR_SUCCESS)
  174. return 0;
  175. }
  176. else
  177. hOpenKey = hKeyReg;
  178. if ( (RegQueryValueEx(hOpenKey, lpValue, NULL, &dwType, (LPBYTE) &dwBuffer, &dwSize) != ERROR_SUCCESS) ||
  179. (dwSize != sizeof(DWORD)) )
  180. dwBuffer = 0;
  181. if (lpKey)
  182. RegCloseKey(hOpenKey);
  183. return dwBuffer;
  184. }
  185. BOOL RegSetString(HKEY hRootKey, LPTSTR lpSubKey, LPTSTR lpValue, LPTSTR lpData) {
  186. BOOL bSuccess = FALSE;
  187. if (lpSubKey) {
  188. HKEY hRegKey;
  189. DWORD dwBuffer;
  190. if (RegCreateKeyEx(hRootKey, lpSubKey, 0, TEXT(""), 0, KEY_ALL_ACCESS, NULL, &hRegKey, &dwBuffer) == ERROR_SUCCESS) {
  191. bSuccess = (RegSetValueEx(hRegKey, lpValue, 0, REG_SZ, (CONST BYTE *) lpData, lstrlen(lpData) + 1) == ERROR_SUCCESS);
  192. RegCloseKey(hRegKey);
  193. }
  194. }
  195. else
  196. bSuccess = (RegSetValueEx(hRootKey, lpValue, 0, REG_SZ, (CONST BYTE *) lpData, lstrlen(lpData) + 1) == ERROR_SUCCESS);
  197. return bSuccess;
  198. }
  199. BOOL RegSetDword(HKEY hRootKey, LPTSTR lpSubKey, LPTSTR lpValue, DWORD dwData) {
  200. BOOL bSuccess = FALSE;
  201. if (lpSubKey) {
  202. HKEY hRegKey;
  203. DWORD dwBuffer;
  204. if (RegCreateKeyEx(hRootKey, lpSubKey, 0, TEXT(""), 0, KEY_ALL_ACCESS, NULL, &hRegKey, &dwBuffer) == ERROR_SUCCESS) {
  205. bSuccess = (RegSetValueEx(hRegKey, lpValue, 0, REG_DWORD, (CONST BYTE *) &dwData, sizeof(dwData)) == ERROR_SUCCESS);
  206. RegCloseKey(hRegKey);
  207. }
  208. }
  209. else
  210. bSuccess = (RegSetValueEx(hRootKey, lpValue, 0, REG_SZ, (CONST BYTE *) &dwData, sizeof(dwData)) == ERROR_SUCCESS);
  211. return bSuccess;
  212. }
  213. BOOL RegCheck(HKEY hKeyRoot, LPTSTR lpKey, LPTSTR lpValue)
  214. {
  215. LPTSTR lpBuffer;
  216. DWORD dwSize = 0,
  217. dwType,
  218. dwBuffer = 0;
  219. HKEY hKeyReg;
  220. BOOL bReturn = FALSE;
  221. if (lpKey)
  222. {
  223. if (RegOpenKeyEx(hKeyRoot, lpKey, 0, KEY_ALL_ACCESS, &hKeyReg) != ERROR_SUCCESS)
  224. return 0;
  225. }
  226. else
  227. hKeyReg = hKeyRoot;
  228. // Query for the value and allocate the memory for the
  229. // value data if it is type REG_SZ.
  230. //
  231. if (RegQueryValueEx(hKeyReg, lpValue, NULL, &dwType, NULL, &dwSize) == ERROR_SUCCESS)
  232. {
  233. if (dwType == REG_SZ)
  234. {
  235. // It is a string value, must allocate a buffer for the string.
  236. //
  237. if (lpBuffer = (LPTSTR) MALLOC(dwSize))
  238. {
  239. if ( (RegQueryValueEx(hKeyReg, lpValue, NULL, NULL, (LPBYTE) lpBuffer, &dwSize) == ERROR_SUCCESS) &&
  240. (*lpBuffer != '0') && (*lpBuffer) )
  241. {
  242. dwBuffer = 1;
  243. }
  244. FREE(lpBuffer);
  245. }
  246. }
  247. else
  248. {
  249. // Must be a DWORD or BIN value.
  250. //
  251. RegQueryValueEx(hKeyReg, lpValue, NULL, &dwType, (LPBYTE) &dwBuffer, &dwSize);
  252. }
  253. bReturn = (dwBuffer != 0);
  254. }
  255. if (lpKey)
  256. RegCloseKey(hKeyReg);
  257. return bReturn;
  258. }
  259. BOOL RegEnumKeys(HKEY hKey, LPTSTR lpRegKey, REGENUMKEYPROC hCallBack, LPARAM lParam, BOOL bDelKeys)
  260. {
  261. TCHAR szKeyName[MAX_PATH + 1];
  262. DWORD dwIndex = 0,
  263. dwSize = sizeof(szKeyName);
  264. HKEY hKeyReg,
  265. hKeyEnum;
  266. BOOL bReturn = TRUE;
  267. // Open a key handle to the key to enumerate.
  268. //
  269. if ( ( lpRegKey == NULL ) ||
  270. ( RegOpenKeyEx(hKey, lpRegKey, 0, KEY_ALL_ACCESS, &hKeyReg) == ERROR_SUCCESS ) )
  271. {
  272. // Enumerate all the subkeys in this key.
  273. //
  274. while ( bReturn && ( RegEnumKeyEx(hKeyReg, dwIndex, szKeyName, &dwSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS ) )
  275. {
  276. if ( RegOpenKeyEx(hKeyReg, szKeyName, 0, KEY_ALL_ACCESS, &hKeyEnum) == ERROR_SUCCESS )
  277. {
  278. bReturn = hCallBack(hKeyEnum, szKeyName, lParam);
  279. RegCloseKey(hKeyEnum);
  280. }
  281. if ( !bDelKeys || ( RegDeleteKey(hKeyReg, szKeyName) != ERROR_SUCCESS ) )
  282. dwIndex++;
  283. dwSize = sizeof(szKeyName);
  284. }
  285. if (lpRegKey)
  286. RegCloseKey(hKeyReg);
  287. }
  288. else
  289. bReturn = FALSE;
  290. return bReturn;
  291. }
  292. BOOL RegEnumValues(HKEY hKey, LPTSTR lpRegKey, REGENUMVALPROC hCallBack, LPARAM lParam, BOOL bDelValues) {
  293. TCHAR szValueName[MAX_PATH + 1];
  294. LPTSTR lpBuffer;
  295. DWORD dwIndex = 0,
  296. dwSize = sizeof(szValueName),
  297. dwDataSize = 0,
  298. dwType;
  299. HKEY hKeyReg;
  300. BOOL bReturn = TRUE;
  301. // Open a key handle to the key to enumerate.
  302. //
  303. if ( (lpRegKey == NULL) ||
  304. (RegOpenKeyEx(hKey, lpRegKey, 0, KEY_ALL_ACCESS, &hKeyReg) == ERROR_SUCCESS) ) {
  305. // Enumerate all the values in this key.
  306. //
  307. while (bReturn && (RegEnumValue(hKeyReg, dwIndex, szValueName, &dwSize, NULL, &dwType, NULL, &dwDataSize) == ERROR_SUCCESS)) {
  308. if ((dwType == REG_SZ) &&
  309. (lpBuffer = (LPTSTR) MALLOC(dwDataSize))) {
  310. if (RegQueryValueEx(hKeyReg, szValueName, NULL, NULL, (LPBYTE) lpBuffer, &dwDataSize) == ERROR_SUCCESS)
  311. bReturn = hCallBack(szValueName, lpBuffer, lParam);
  312. FREE(lpBuffer);
  313. }
  314. if ( !bDelValues || (RegDeleteValue(hKeyReg, szValueName) != ERROR_SUCCESS) )
  315. dwIndex++;
  316. dwSize = sizeof(szValueName);
  317. dwDataSize = 0;
  318. }
  319. if (lpRegKey)
  320. RegCloseKey(hKeyReg);
  321. }
  322. else
  323. bReturn = FALSE;
  324. return bReturn;
  325. }