Leaked source code of windows server 2003
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.

449 lines
13 KiB

  1. /****************************************************************************\
  2. REGAPI.C / OPK Common Library (OPKLIB.LIB)
  3. Microsoft Confidential
  4. Copyright (c) Microsoft Corporation 1999
  5. All rights reserved
  6. Registry API source file for custom registry APIs used in the OPK tools
  7. to easily interface with the registry.
  8. 01/01 - Jason Cohen (JCOHEN)
  9. Added this new source file for the OPK library.
  10. \****************************************************************************/
  11. //
  12. // Include file(s)
  13. //
  14. #include <pch.h>
  15. //
  16. // External Function(s):
  17. //
  18. BOOL RegExists(HKEY hKeyReg, LPTSTR lpKey, LPTSTR lpValue)
  19. {
  20. HKEY hOpenKey = NULL;
  21. BOOL bExists = FALSE;
  22. if (lpKey)
  23. {
  24. if (RegOpenKeyEx(hKeyReg, lpKey, 0, KEY_ALL_ACCESS, &hOpenKey) != ERROR_SUCCESS)
  25. return bExists;
  26. }
  27. else
  28. hOpenKey = hKeyReg;
  29. if (lpValue)
  30. bExists = (RegQueryValueEx(hOpenKey, lpValue, NULL, NULL, NULL, NULL) == ERROR_SUCCESS);
  31. else
  32. bExists = TRUE;
  33. if (lpKey)
  34. RegCloseKey(hOpenKey);
  35. return bExists;
  36. }
  37. BOOL RegDelete(HKEY hRootKey, LPTSTR lpSubKey, LPTSTR lpValue)
  38. {
  39. BOOL bSuccess = FALSE;
  40. if (lpValue) {
  41. if (lpSubKey) {
  42. HKEY hRegKey;
  43. if (RegOpenKeyEx(hRootKey, lpSubKey, 0, KEY_ALL_ACCESS, &hRegKey) == ERROR_SUCCESS) {
  44. bSuccess = (RegDeleteValue(hRegKey, lpValue) == ERROR_SUCCESS);
  45. RegCloseKey(hRegKey);
  46. }
  47. }
  48. else
  49. bSuccess = (RegDeleteValue(hRootKey, lpValue) == ERROR_SUCCESS);
  50. }
  51. else
  52. bSuccess = (RegDeleteKey(hRootKey, lpSubKey) == ERROR_SUCCESS);
  53. return bSuccess;
  54. }
  55. LPTSTR RegGetStringEx(HKEY hKeyReg, LPTSTR lpKey, LPTSTR lpValue, BOOL bExpand)
  56. {
  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 ( ( bExpand ) &&
  89. ( 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. LPTSTR RegGetString(HKEY hKeyReg, LPTSTR lpKey, LPTSTR lpValue)
  127. {
  128. return RegGetStringEx(hKeyReg, lpKey, lpValue, FALSE);
  129. }
  130. LPTSTR RegGetExpand(HKEY hKeyReg, LPTSTR lpKey, LPTSTR lpValue)
  131. {
  132. return RegGetStringEx(hKeyReg, lpKey, lpValue, TRUE);
  133. }
  134. LPVOID RegGetBin(HKEY hKeyReg, LPTSTR lpKey, LPTSTR lpValue)
  135. {
  136. HKEY hOpenKey = NULL;
  137. LPVOID lpBuffer = NULL;
  138. DWORD dwSize = 0,
  139. dwType;
  140. // If the key is specified, we must open it. Otherwise we can
  141. // just use the HKEY passed in.
  142. //
  143. if (lpKey)
  144. {
  145. // If the open key fails, return NULL because the value can't exist.
  146. //
  147. if (RegOpenKeyEx(hKeyReg, lpKey, 0, KEY_ALL_ACCESS, &hOpenKey) != ERROR_SUCCESS)
  148. return NULL;
  149. }
  150. else
  151. hOpenKey = hKeyReg;
  152. // Now query the value to get the size to allocate. Make sure the date
  153. // type is a string and that the malloc doesn't fail.
  154. //
  155. if ( ( RegQueryValueEx(hOpenKey, lpValue, NULL, &dwType, NULL, &dwSize) == ERROR_SUCCESS ) &&
  156. ( dwType == REG_BINARY ) &&
  157. ( lpBuffer = MALLOC(dwSize) ) )
  158. {
  159. // We know the value exists and we have the memory we need to query the value again.
  160. //
  161. if ( ( RegQueryValueEx(hOpenKey, lpValue, NULL, NULL, (LPBYTE) lpBuffer, &dwSize) != ERROR_SUCCESS ) ||
  162. ( dwType != REG_BINARY ) )
  163. // For some reason the query failed, that shouldn't happen
  164. // but now we need to free and return NULL.
  165. //
  166. FREE(lpBuffer);
  167. }
  168. // If we opened a key, we must close it.
  169. //
  170. if (lpKey)
  171. RegCloseKey(hOpenKey);
  172. // Return the buffer allocated, or NULL if something failed.
  173. //
  174. return lpBuffer;
  175. }
  176. DWORD RegGetDword(HKEY hKeyReg, LPTSTR lpKey, LPTSTR lpValue)
  177. {
  178. HKEY hOpenKey = NULL;
  179. DWORD dwBuffer,
  180. dwSize = sizeof(DWORD),
  181. dwType;
  182. if (lpKey) {
  183. if (RegOpenKeyEx(hKeyReg, lpKey, 0, KEY_ALL_ACCESS, &hOpenKey) != ERROR_SUCCESS)
  184. return 0;
  185. }
  186. else
  187. hOpenKey = hKeyReg;
  188. if ( (RegQueryValueEx(hOpenKey, lpValue, NULL, &dwType, (LPBYTE) &dwBuffer, &dwSize) != ERROR_SUCCESS) ||
  189. (dwSize != sizeof(DWORD)) )
  190. dwBuffer = 0;
  191. if (lpKey)
  192. RegCloseKey(hOpenKey);
  193. return dwBuffer;
  194. }
  195. BOOL RegSetStringEx(HKEY hRootKey, LPTSTR lpSubKey, LPTSTR lpValue, LPTSTR lpData, BOOL bExpand)
  196. {
  197. BOOL bSuccess = FALSE;
  198. if (lpSubKey) {
  199. HKEY hRegKey;
  200. DWORD dwBuffer;
  201. if (RegCreateKeyEx(hRootKey, lpSubKey, 0, TEXT(""), 0, KEY_ALL_ACCESS, NULL, &hRegKey, &dwBuffer) == ERROR_SUCCESS) {
  202. bSuccess = (RegSetValueEx(hRegKey, lpValue, 0, bExpand ? REG_EXPAND_SZ : REG_SZ, (CONST BYTE *) lpData, (lstrlen(lpData) + 1) * sizeof(TCHAR)) == ERROR_SUCCESS);
  203. RegCloseKey(hRegKey);
  204. }
  205. }
  206. else
  207. bSuccess = (RegSetValueEx(hRootKey, lpValue, 0, REG_SZ, (CONST BYTE *) lpData, lstrlen(lpData) + 1) == ERROR_SUCCESS);
  208. return bSuccess;
  209. }
  210. BOOL RegSetString(HKEY hRootKey, LPTSTR lpSubKey, LPTSTR lpValue, LPTSTR lpData)
  211. {
  212. return RegSetStringEx(hRootKey, lpSubKey, lpValue, lpData, FALSE);
  213. }
  214. BOOL RegSetExpand(HKEY hRootKey, LPTSTR lpSubKey, LPTSTR lpValue, LPTSTR lpData)
  215. {
  216. return RegSetStringEx(hRootKey, lpSubKey, lpValue, lpData, TRUE);
  217. }
  218. BOOL RegSetDword(HKEY hRootKey, LPTSTR lpSubKey, LPTSTR lpValue, DWORD dwData)
  219. {
  220. BOOL bSuccess = FALSE;
  221. if (lpSubKey) {
  222. HKEY hRegKey;
  223. DWORD dwBuffer;
  224. if (RegCreateKeyEx(hRootKey, lpSubKey, 0, TEXT(""), 0, KEY_ALL_ACCESS, NULL, &hRegKey, &dwBuffer) == ERROR_SUCCESS) {
  225. bSuccess = (RegSetValueEx(hRegKey, lpValue, 0, REG_DWORD, (CONST BYTE *) &dwData, sizeof(dwData)) == ERROR_SUCCESS);
  226. RegCloseKey(hRegKey);
  227. }
  228. }
  229. else
  230. bSuccess = (RegSetValueEx(hRootKey, lpValue, 0, REG_SZ, (CONST BYTE *) &dwData, sizeof(dwData)) == ERROR_SUCCESS);
  231. return bSuccess;
  232. }
  233. /****************************************************************************\
  234. BOOL // Returns TRUE if value queried is non-zero.
  235. // Otherwise returns FALSE.
  236. // Works for REG_SZ, DWORD, and BINARY.
  237. RegCheck( // Checks registry for existing value.
  238. HKEY hKeyRoot, // Root key to open.
  239. LPTSTR lpKey, // Subkey to open.
  240. LPTSTR lpValue // Value to check.
  241. );
  242. \****************************************************************************/
  243. BOOL RegCheck(HKEY hKeyRoot, LPTSTR lpKey, LPTSTR lpValue)
  244. {
  245. LPTSTR lpBuffer;
  246. DWORD dwSize = 0,
  247. dwType,
  248. dwBuffer = 0;
  249. HKEY hKeyReg;
  250. BOOL bReturn = FALSE;
  251. if (lpKey)
  252. {
  253. if (RegOpenKeyEx(hKeyRoot, lpKey, 0, KEY_ALL_ACCESS, &hKeyReg) != ERROR_SUCCESS)
  254. return 0;
  255. }
  256. else
  257. hKeyReg = hKeyRoot;
  258. // Query for the value and allocate the memory for the
  259. // value data if it is type REG_SZ.
  260. //
  261. if (RegQueryValueEx(hKeyReg, lpValue, NULL, &dwType, NULL, &dwSize) == ERROR_SUCCESS)
  262. {
  263. if (dwType == REG_SZ)
  264. {
  265. // It is a string value, must allocate a buffer for the string.
  266. //
  267. if (lpBuffer = (LPTSTR) MALLOC(dwSize))
  268. {
  269. if ( (RegQueryValueEx(hKeyReg, lpValue, NULL, NULL, (LPBYTE) lpBuffer, &dwSize) == ERROR_SUCCESS) &&
  270. (*lpBuffer != '0') && (*lpBuffer) )
  271. {
  272. dwBuffer = 1;
  273. }
  274. FREE(lpBuffer);
  275. }
  276. }
  277. else
  278. {
  279. // Must be a DWORD or BIN value.
  280. //
  281. RegQueryValueEx(hKeyReg, lpValue, NULL, &dwType, (LPBYTE) &dwBuffer, &dwSize);
  282. }
  283. bReturn = (dwBuffer != 0);
  284. }
  285. if (lpKey)
  286. RegCloseKey(hKeyReg);
  287. return bReturn;
  288. }
  289. BOOL RegEnumKeys(HKEY hKey, LPTSTR lpRegKey, REGENUMKEYPROC hCallBack, LPARAM lParam, BOOL bDelKeys)
  290. {
  291. TCHAR szKeyName[MAX_PATH + 1];
  292. DWORD dwIndex = 0,
  293. dwSize = AS(szKeyName);
  294. HKEY hKeyReg = hKey,
  295. hKeyEnum;
  296. BOOL bReturn = TRUE;
  297. // Open a key handle to the key to enumerate.
  298. //
  299. if ( ( lpRegKey == NULL ) ||
  300. ( RegOpenKeyEx(hKey, lpRegKey, 0, KEY_ALL_ACCESS, &hKeyReg) == ERROR_SUCCESS ) )
  301. {
  302. // Enumerate all the subkeys in this key.
  303. //
  304. while ( bReturn && ( RegEnumKeyEx(hKeyReg, dwIndex, szKeyName, &dwSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS ) )
  305. {
  306. if ( RegOpenKeyEx(hKeyReg, szKeyName, 0, KEY_ALL_ACCESS, &hKeyEnum) == ERROR_SUCCESS )
  307. {
  308. bReturn = hCallBack(hKeyEnum, szKeyName, lParam);
  309. RegCloseKey(hKeyEnum);
  310. }
  311. if ( !bDelKeys || ( RegDeleteKey(hKeyReg, szKeyName) != ERROR_SUCCESS ) )
  312. dwIndex++;
  313. dwSize = sizeof(szKeyName);
  314. }
  315. if (lpRegKey)
  316. RegCloseKey(hKeyReg);
  317. }
  318. else
  319. bReturn = FALSE;
  320. return bReturn;
  321. }
  322. BOOL RegEnumValues(HKEY hKey, LPTSTR lpRegKey, REGENUMVALPROC hCallBack, LPARAM lParam, BOOL bDelValues)
  323. {
  324. TCHAR szValueName[MAX_PATH + 1];
  325. LPTSTR lpBuffer;
  326. DWORD dwIndex = 0,
  327. dwSize = AS(szValueName),
  328. dwDataSize = 0,
  329. dwType;
  330. HKEY hKeyReg = hKey;
  331. BOOL bReturn = TRUE;
  332. // Open a key handle to the key to enumerate.
  333. //
  334. if ( (lpRegKey == NULL) ||
  335. (RegOpenKeyEx(hKey, lpRegKey, 0, KEY_ALL_ACCESS, &hKeyReg) == ERROR_SUCCESS) ) {
  336. // Enumerate all the values in this key.
  337. //
  338. while (bReturn && (RegEnumValue(hKeyReg, dwIndex, szValueName, &dwSize, NULL, &dwType, NULL, &dwDataSize) == ERROR_SUCCESS)) {
  339. if ((dwType == REG_SZ) &&
  340. (lpBuffer = (LPTSTR) MALLOC(dwDataSize))) {
  341. if (RegQueryValueEx(hKeyReg, szValueName, NULL, NULL, (LPBYTE) lpBuffer, &dwDataSize) == ERROR_SUCCESS)
  342. bReturn = hCallBack(szValueName, lpBuffer, lParam);
  343. FREE(lpBuffer);
  344. }
  345. if ( !bDelValues || (RegDeleteValue(hKeyReg, szValueName) != ERROR_SUCCESS) )
  346. dwIndex++;
  347. dwSize = sizeof(szValueName);
  348. dwDataSize = 0;
  349. }
  350. if (lpRegKey)
  351. RegCloseKey(hKeyReg);
  352. }
  353. else
  354. bReturn = FALSE;
  355. return bReturn;
  356. }