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.

412 lines
8.2 KiB

  1. #include "reg.h"
  2. #include "sfstr.h"
  3. #include "dbg.h"
  4. #define ARRAYSIZE(a) (sizeof((a))/sizeof((a)[0]))
  5. ///////////////////////////////////////////////////////////////////////////////
  6. //
  7. HRESULT _RegOpenKey(HKEY hkey, LPCWSTR pszKey, HKEY* phkey)
  8. {
  9. HRESULT hres;
  10. if (ERROR_SUCCESS == RegOpenKeyEx(hkey, pszKey, 0, MAXIMUM_ALLOWED, phkey))
  11. {
  12. hres = S_OK;
  13. }
  14. else
  15. {
  16. hres = S_FALSE;
  17. *phkey = NULL;
  18. }
  19. return hres;
  20. }
  21. HRESULT _RegCreateKey(HKEY hkey, LPCWSTR pszKey, HKEY* phkey, DWORD* pdwDisp)
  22. {
  23. HRESULT hres;
  24. if (ERROR_SUCCESS == RegCreateKeyEx(hkey, pszKey, 0, 0,
  25. REG_OPTION_NON_VOLATILE, MAXIMUM_ALLOWED, NULL, phkey, pdwDisp))
  26. {
  27. hres = S_OK;
  28. }
  29. else
  30. {
  31. hres = S_FALSE;
  32. *phkey = NULL;
  33. }
  34. return hres;
  35. }
  36. HRESULT _RegCloseKey(HKEY hkey)
  37. {
  38. RegCloseKey(hkey);
  39. return S_OK;
  40. }
  41. HRESULT _RegQueryType(HKEY hkey, LPCWSTR pszSubKey, LPCWSTR pszValueName,
  42. DWORD* pdwType)
  43. {
  44. HRESULT hres = S_FALSE;
  45. HKEY hkeyLocal = hkey;
  46. *pdwType = 0;
  47. if (pszSubKey)
  48. {
  49. hres = _RegOpenKey(hkey, pszSubKey, &hkeyLocal);
  50. }
  51. else
  52. {
  53. hres = S_OK;
  54. }
  55. if (SUCCEEDED(hres) && (S_FALSE != hres))
  56. {
  57. if (ERROR_SUCCESS == RegQueryValueEx(hkeyLocal, pszValueName, 0,
  58. pdwType, NULL, NULL))
  59. {
  60. hres = S_OK;
  61. }
  62. else
  63. {
  64. hres = S_FALSE;
  65. }
  66. if (pszSubKey)
  67. {
  68. _RegCloseKey(hkeyLocal);
  69. }
  70. }
  71. return hres;
  72. }
  73. HRESULT _RegQueryDWORD(HKEY hkey, LPCWSTR pszSubKey, LPCWSTR pszValueName,
  74. DWORD* pdwValue)
  75. {
  76. return _RegQueryGeneric(hkey, pszSubKey, pszValueName, (PBYTE)pdwValue,
  77. sizeof(*pdwValue));
  78. }
  79. HRESULT _RegQueryGeneric(HKEY hkey, LPCWSTR pszSubKey, LPCWSTR pszValueName,
  80. PBYTE pbValue, DWORD cbValue)
  81. {
  82. HRESULT hres = S_FALSE;
  83. HKEY hkeyLocal = hkey;
  84. if (pszSubKey)
  85. {
  86. hres = _RegOpenKey(hkey, pszSubKey, &hkeyLocal);
  87. }
  88. else
  89. {
  90. hres = S_OK;
  91. }
  92. if (SUCCEEDED(hres) && (S_FALSE != hres))
  93. {
  94. if (ERROR_SUCCESS == RegQueryValueEx(hkeyLocal, pszValueName, 0, NULL,
  95. pbValue, &cbValue))
  96. {
  97. hres = S_OK;
  98. }
  99. else
  100. {
  101. hres = S_FALSE;
  102. }
  103. if (pszSubKey)
  104. {
  105. _RegCloseKey(hkeyLocal);
  106. }
  107. }
  108. return hres;
  109. }
  110. HRESULT _RegQueryGenericWithType(HKEY hkey, LPCWSTR pszSubKey,
  111. LPCWSTR pszValueName, DWORD* pdwType, PBYTE pbValue, DWORD cbValue)
  112. {
  113. HRESULT hres = S_FALSE;
  114. HKEY hkeyLocal = hkey;
  115. if (pszSubKey)
  116. {
  117. hres = _RegOpenKey(hkey, pszSubKey, &hkeyLocal);
  118. }
  119. else
  120. {
  121. hres = S_OK;
  122. }
  123. if (SUCCEEDED(hres) && (S_FALSE != hres))
  124. {
  125. if (ERROR_SUCCESS == RegQueryValueEx(hkeyLocal, pszValueName, 0,
  126. pdwType, pbValue, &cbValue))
  127. {
  128. hres = S_OK;
  129. }
  130. else
  131. {
  132. hres = S_FALSE;
  133. }
  134. if (pszSubKey)
  135. {
  136. _RegCloseKey(hkeyLocal);
  137. }
  138. }
  139. return hres;
  140. }
  141. HRESULT _RegDeleteValue(HKEY hkey, LPCWSTR pszSubKey, LPCWSTR pszValueName)
  142. {
  143. HRESULT hr = S_FALSE;
  144. HKEY hkeyLocal = hkey;
  145. if (pszSubKey)
  146. {
  147. hr = _RegOpenKey(hkey, pszSubKey, &hkeyLocal);
  148. }
  149. else
  150. {
  151. hr = S_OK;
  152. }
  153. if (SUCCEEDED(hr) && (S_FALSE != hr))
  154. {
  155. if (ERROR_SUCCESS == RegDeleteValue(hkeyLocal, pszValueName))
  156. {
  157. hr = S_OK;
  158. }
  159. else
  160. {
  161. hr = S_FALSE;
  162. }
  163. if (pszSubKey)
  164. {
  165. _RegCloseKey(hkeyLocal);
  166. }
  167. }
  168. return hr;
  169. }
  170. HRESULT _RegQueryValueSize(HKEY hkey, LPCWSTR pszSubKey, LPCWSTR pszValueName,
  171. DWORD* pcbValue)
  172. {
  173. HRESULT hres = S_FALSE;
  174. HKEY hkeyLocal = hkey;
  175. if (pszSubKey)
  176. {
  177. hres = _RegOpenKey(hkey, pszSubKey, &hkeyLocal);
  178. }
  179. else
  180. {
  181. hres = S_OK;
  182. }
  183. if (SUCCEEDED(hres) && (S_FALSE != hres))
  184. {
  185. if (ERROR_SUCCESS == RegQueryValueEx(hkeyLocal, pszValueName, 0, NULL,
  186. NULL, pcbValue))
  187. {
  188. hres = S_OK;
  189. }
  190. else
  191. {
  192. hres = S_FALSE;
  193. }
  194. if (pszSubKey)
  195. {
  196. _RegCloseKey(hkeyLocal);
  197. }
  198. }
  199. return hres;
  200. }
  201. HRESULT _RegSetGeneric(HKEY hkey, LPCWSTR pszValueName, DWORD dwType,
  202. PBYTE pbValue, DWORD cbValue)
  203. {
  204. HRESULT hres = S_FALSE;
  205. if (ERROR_SUCCESS == RegSetValueEx(hkey, pszValueName, 0, dwType, pbValue,
  206. cbValue))
  207. {
  208. hres = S_OK;
  209. }
  210. return hres;
  211. }
  212. HRESULT _RegSetString(HKEY hkey, LPCWSTR pszValueName, LPCWSTR pszValue)
  213. {
  214. DWORD cb = (lstrlen(pszValue) + 1) * sizeof(WCHAR);
  215. return _RegSetGeneric(hkey, pszValueName, REG_SZ, (PBYTE)pszValue, cb);
  216. }
  217. HRESULT _RegSetDWORD(HKEY hkey, LPCWSTR pszValueName, DWORD dwValue)
  218. {
  219. return _RegSetGeneric(hkey, pszValueName, REG_DWORD, (PBYTE)&dwValue,
  220. sizeof(dwValue));
  221. }
  222. HRESULT _RegSetBinary(HKEY hkey, LPCWSTR pszValueName, PVOID pvValue, DWORD cbValue)
  223. {
  224. return _RegSetGeneric(hkey, pszValueName, REG_BINARY, (LPBYTE)pvValue, cbValue);
  225. }
  226. HRESULT _RegQueryString(HKEY hkey, LPCWSTR pszSubKey, LPCWSTR pszValueName,
  227. LPWSTR pszValue, DWORD cchValue)
  228. {
  229. DWORD cb = cchValue * sizeof(WCHAR);
  230. return _RegQueryGeneric(hkey, pszSubKey, pszValueName, (PBYTE)pszValue,
  231. cb);
  232. }
  233. HRESULT _RegEnumStringValue(HKEY hkey, DWORD dwIndex, LPWSTR pszValue,
  234. DWORD cchValue)
  235. {
  236. HRESULT hres = S_FALSE;
  237. LONG lRes;
  238. if (ERROR_SUCCESS == (lRes = RegEnumValue(hkey, dwIndex, pszValue,
  239. &cchValue, 0, 0, 0, 0)))
  240. {
  241. hres = S_OK;
  242. }
  243. else
  244. {
  245. if ((ERROR_SUCCESS != lRes) && (ERROR_NO_MORE_ITEMS != lRes))
  246. {
  247. hres = E_FAIL;
  248. }
  249. }
  250. return hres;
  251. }
  252. HRESULT _RegEnumStringKey(HKEY hkey, DWORD dwIndex, LPWSTR pszKey,
  253. DWORD cchKey)
  254. {
  255. HRESULT hres = S_FALSE;
  256. LONG lRes;
  257. if (ERROR_SUCCESS == (lRes = RegEnumKeyEx(hkey, dwIndex, pszKey,
  258. &cchKey, NULL, NULL, NULL, NULL)))
  259. {
  260. hres = S_OK;
  261. }
  262. else
  263. {
  264. if (ERROR_NO_MORE_ITEMS != lRes)
  265. {
  266. hres = E_FAIL;
  267. }
  268. }
  269. return hres;
  270. }
  271. HRESULT _RegSetKeyAndString(HKEY hkey, LPCWSTR pszKey, LPCWSTR pszSubkey,
  272. LPCWSTR pszValueName, LPCWSTR pszValue)
  273. {
  274. ASSERT(pszKey && *pszKey);
  275. WCHAR szKeyBuf[MAX_KEY];
  276. LPWSTR pszNext;
  277. DWORD cchLeft;
  278. HRESULT hres = SafeStrCpyNEx(szKeyBuf, pszKey, ARRAYSIZE(szKeyBuf),
  279. &pszNext, &cchLeft);
  280. if (SUCCEEDED(hres))
  281. {
  282. HKEY hkeyNew;
  283. if (pszSubkey)
  284. {
  285. hres = SafeStrCpyNEx(pszNext, TEXT("\\"), cchLeft, &pszNext,
  286. &cchLeft);
  287. if (SUCCEEDED(hres))
  288. {
  289. hres = SafeStrCpyNEx(pszNext, pszSubkey, cchLeft, &pszNext,
  290. &cchLeft);
  291. }
  292. }
  293. if (SUCCEEDED(hres))
  294. {
  295. // Create and open key and subkey.
  296. hres= _RegCreateKey(hkey, szKeyBuf, &hkeyNew, NULL);
  297. if (SUCCEEDED(hres) && (S_FALSE != hres))
  298. {
  299. if (pszValue)
  300. {
  301. hres = _RegSetString(hkeyNew, pszValueName, pszValue);
  302. }
  303. RegCloseKey(hkeyNew);
  304. }
  305. }
  306. }
  307. return hres;
  308. }
  309. HRESULT _RegSubkeyExists(HKEY hkey, LPCWSTR pszPath, LPCWSTR pszSubkey)
  310. {
  311. WCHAR szKeyBuf[MAX_PATH];
  312. LPWSTR pszNext;
  313. DWORD cchLeft;
  314. // Copy keyname into buffer.
  315. HRESULT hres = SafeStrCpyNEx(szKeyBuf, pszPath, ARRAYSIZE(szKeyBuf),
  316. &pszNext, &cchLeft);
  317. if (SUCCEEDED(hres))
  318. {
  319. HKEY hkey2;
  320. if (pszSubkey)
  321. {
  322. hres = SafeStrCpyNEx(pszNext, TEXT("\\"), cchLeft, &pszNext,
  323. &cchLeft);
  324. if (SUCCEEDED(hres))
  325. {
  326. hres = SafeStrCpyNEx(pszNext, pszSubkey, cchLeft, &pszNext,
  327. &cchLeft);
  328. }
  329. }
  330. if (SUCCEEDED(hres))
  331. {
  332. // Determine if key exists by trying to open it.
  333. hres = _RegOpenKey(hkey, szKeyBuf, &hkey2);
  334. if (SUCCEEDED(hres) && (S_FALSE != hres))
  335. {
  336. _RegCloseKey(hkey2);
  337. }
  338. }
  339. }
  340. return hres;
  341. }