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