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.

360 lines
8.0 KiB

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