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.

494 lines
13 KiB

  1. /*++
  2. Copyright (C) 1998-2001 Microsoft Corporation
  3. Module Name:
  4. REG.CPP
  5. Abstract:
  6. Utility Registry classes
  7. History:
  8. raymcc 30-May-96 Created.
  9. raymcc 26-Jul-99 Updated for wchar_t.
  10. --*/
  11. #include "precomp.h"
  12. #include <wbemcli.h>
  13. #include <stdio.h>
  14. #include <reg.h>
  15. #include <malloc.h>
  16. //***************************************************************************
  17. //
  18. //***************************************************************************
  19. // ok
  20. int Registry::Open(HKEY hStart, wchar_t *pszStartKey, DWORD desiredAccess )
  21. {
  22. int nStatus = no_error;
  23. DWORD dwDisp = 0;
  24. m_nLastError = RegCreateKeyEx(hStart, pszStartKey,
  25. 0, 0, 0,
  26. desiredAccess, 0, &hPrimaryKey, &dwDisp);
  27. if (m_nLastError != 0)
  28. nStatus = failed;
  29. return nStatus;
  30. }
  31. //***************************************************************************
  32. //
  33. //***************************************************************************
  34. // ok
  35. Registry::Registry(HKEY hRoot, REGSAM flags, wchar_t *pszStartKey)
  36. {
  37. hPrimaryKey = 0;
  38. hSubkey = 0;
  39. nStatus = RegOpenKeyEx(hRoot, pszStartKey,
  40. 0, flags, &hPrimaryKey
  41. );
  42. hSubkey = hPrimaryKey;
  43. m_nLastError = nStatus;
  44. }
  45. //***************************************************************************
  46. //
  47. //***************************************************************************
  48. // ok
  49. Registry::Registry(HKEY hRoot, DWORD dwOptions, REGSAM flags, wchar_t *pszStartKey)
  50. {
  51. hPrimaryKey = 0;
  52. hSubkey = 0;
  53. int nStatus = no_error;
  54. DWORD dwDisp = 0;
  55. m_nLastError = RegCreateKeyEx(hRoot, pszStartKey,
  56. 0, 0, dwOptions,
  57. flags, 0, &hPrimaryKey, &dwDisp
  58. );
  59. hSubkey = hPrimaryKey;
  60. }
  61. //***************************************************************************
  62. //
  63. //***************************************************************************
  64. // ok
  65. Registry::Registry(wchar_t *pszLocalMachineStartKey, DWORD desiredAccess)
  66. {
  67. hPrimaryKey = 0;
  68. hSubkey = 0;
  69. nStatus = Open(HKEY_LOCAL_MACHINE, pszLocalMachineStartKey, desiredAccess);
  70. hSubkey = hPrimaryKey;
  71. }
  72. //***************************************************************************
  73. //
  74. //***************************************************************************
  75. // ok
  76. Registry::Registry()
  77. {
  78. hPrimaryKey = 0;
  79. hSubkey = 0;
  80. nStatus = 0;
  81. hSubkey = 0;
  82. }
  83. //***************************************************************************
  84. //
  85. //***************************************************************************
  86. // ok
  87. Registry::~Registry()
  88. {
  89. if (hSubkey)
  90. RegCloseKey(hSubkey);
  91. if (hPrimaryKey != hSubkey)
  92. RegCloseKey(hPrimaryKey);
  93. }
  94. //***************************************************************************
  95. //
  96. //***************************************************************************
  97. // ok
  98. int Registry::MoveToSubkey(wchar_t *pszNewSubkey)
  99. {
  100. DWORD dwDisp = 0;
  101. m_nLastError = RegCreateKeyEx(hPrimaryKey, pszNewSubkey, 0, 0, 0, KEY_ALL_ACCESS,
  102. 0, &hSubkey, &dwDisp);
  103. if (m_nLastError != 0)
  104. return failed;
  105. return no_error;
  106. }
  107. //***************************************************************************
  108. //
  109. //***************************************************************************
  110. // ok
  111. int Registry::GetDWORD(wchar_t *pszValueName, DWORD *pdwValue)
  112. {
  113. DWORD dwSize = sizeof(DWORD);
  114. DWORD dwType = 0;
  115. if(hSubkey == NULL)
  116. return failed;
  117. m_nLastError = RegQueryValueEx(hSubkey, pszValueName, 0, &dwType,
  118. LPBYTE(pdwValue), &dwSize);
  119. if (m_nLastError != 0)
  120. return failed;
  121. if (dwType != REG_DWORD)
  122. return failed;
  123. return no_error;
  124. }
  125. //***************************************************************************
  126. //
  127. //***************************************************************************
  128. // ok
  129. int Registry::GetType(wchar_t *pszValueName, DWORD *pdwType)
  130. {
  131. if(hSubkey == NULL)
  132. return failed;
  133. m_nLastError = RegQueryValueEx(hSubkey, pszValueName, 0, pdwType,
  134. NULL, NULL);
  135. if (m_nLastError != 0)
  136. return failed;
  137. return no_error;
  138. }
  139. //***************************************************************************
  140. //
  141. //***************************************************************************
  142. // ok
  143. int Registry::GetDWORDStr(wchar_t *pszValueName, DWORD *pdwValue)
  144. {
  145. wchar_t cTemp[25];
  146. DWORD dwSize = 25;
  147. DWORD dwType = 0;
  148. wchar_t * pEnd = NULL; // gets set to character that stopped the scan
  149. if(hSubkey == NULL)
  150. return failed;
  151. m_nLastError = RegQueryValueEx(hSubkey, pszValueName, 0, &dwType,
  152. (LPBYTE)cTemp, &dwSize);
  153. if (m_nLastError != 0)
  154. return failed;
  155. if (dwType != REG_SZ)
  156. return failed;
  157. *pdwValue = wcstoul(cTemp, &pEnd, 10);
  158. if(pEnd == NULL || pEnd == cTemp)
  159. return failed;
  160. else
  161. return no_error;
  162. }
  163. //***************************************************************************
  164. //
  165. // Use operator delete on the returned pointer!!
  166. //
  167. //***************************************************************************
  168. // ok
  169. int Registry::GetBinary(wchar_t *pszValue, byte ** pData, DWORD * pdwSize)
  170. {
  171. *pData = 0;
  172. DWORD dwSize = 0;
  173. DWORD dwType = 0;
  174. if(m_nLastError)
  175. return failed;
  176. m_nLastError = RegQueryValueEx(hSubkey, pszValue, 0, &dwType,
  177. 0, &dwSize);
  178. if (m_nLastError != 0)
  179. return failed;
  180. if (dwType != REG_BINARY)
  181. return failed;
  182. byte *p = new byte[dwSize];
  183. if (p == NULL)
  184. return failed;
  185. m_nLastError = RegQueryValueEx(hSubkey, pszValue, 0, &dwType,
  186. LPBYTE(p), &dwSize);
  187. if (m_nLastError != 0)
  188. {
  189. delete [] p;
  190. return failed;
  191. }
  192. *pdwSize = dwSize;
  193. *pData = p;
  194. return no_error;
  195. }
  196. //***************************************************************************
  197. //
  198. //***************************************************************************
  199. // ok
  200. int Registry::SetBinary(wchar_t *pszValue, byte * pData, DWORD dwSize)
  201. {
  202. if(hSubkey == NULL)
  203. return failed;
  204. m_nLastError = RegSetValueEx(hSubkey, pszValue, 0, REG_BINARY, pData, dwSize);
  205. if (m_nLastError != 0)
  206. return failed;
  207. return no_error;
  208. }
  209. //***************************************************************************
  210. //
  211. //***************************************************************************
  212. // ok
  213. int Registry::SetDWORD(wchar_t *pszValueName, DWORD dwValue)
  214. {
  215. if(hSubkey == NULL)
  216. return failed;
  217. m_nLastError = RegSetValueEx(hSubkey, pszValueName, 0, REG_DWORD, LPBYTE(&dwValue),
  218. sizeof(DWORD));
  219. if (m_nLastError != 0)
  220. return failed;
  221. return no_error;
  222. }
  223. //***************************************************************************
  224. //
  225. //***************************************************************************
  226. // ok
  227. int Registry::SetDWORDStr(wchar_t *pszValueName, DWORD dwVal)
  228. {
  229. wchar_t cTemp[30];
  230. StringCchPrintfW(cTemp,30, L"%d",dwVal);
  231. if(hSubkey == NULL)
  232. return failed;
  233. m_nLastError = RegSetValueEx(hSubkey, pszValueName, 0, REG_SZ, LPBYTE(cTemp),
  234. (wcslen(cTemp)+1) * sizeof(wchar_t));
  235. if (m_nLastError != 0)
  236. return failed;
  237. return no_error;
  238. }
  239. //***************************************************************************
  240. //
  241. //***************************************************************************
  242. // ok
  243. int Registry::DeleteValue(wchar_t *pszValueName)
  244. {
  245. if(hSubkey == NULL)
  246. return failed;
  247. return RegDeleteValue(hSubkey, pszValueName);
  248. }
  249. //***************************************************************************
  250. //
  251. //***************************************************************************
  252. // ok
  253. int Registry::SetMultiStr(wchar_t *pszValueName, wchar_t * pszValue, DWORD dwSize)
  254. {
  255. if(hSubkey == NULL)
  256. return failed;
  257. m_nLastError = RegSetValueEx(hSubkey,
  258. pszValueName,
  259. 0,
  260. REG_MULTI_SZ,
  261. LPBYTE(pszValue),
  262. dwSize);
  263. if (m_nLastError != 0)
  264. return failed;
  265. return no_error;
  266. }
  267. //***************************************************************************
  268. //
  269. //***************************************************************************
  270. // ok
  271. int Registry::SetStr(wchar_t *pszValueName, wchar_t *pszValue)
  272. {
  273. int nSize = (wcslen(pszValue)+1) * sizeof(wchar_t);
  274. if(hSubkey == NULL)
  275. return failed;
  276. m_nLastError = RegSetValueEx(hSubkey, pszValueName, 0, REG_SZ, LPBYTE(pszValue), nSize);
  277. if (m_nLastError != 0)
  278. return failed;
  279. return no_error;
  280. }
  281. //***************************************************************************
  282. //
  283. //***************************************************************************
  284. // ok
  285. int Registry::SetExpandStr(wchar_t *pszValueName, wchar_t *pszValue)
  286. {
  287. int nSize = (wcslen(pszValue)+1) * sizeof(wchar_t);
  288. if(hSubkey == NULL)
  289. return failed;
  290. m_nLastError = RegSetValueEx(hSubkey, pszValueName, 0, REG_EXPAND_SZ, LPBYTE(pszValue), nSize);
  291. if (m_nLastError != 0)
  292. return failed;
  293. return no_error;
  294. }
  295. //***************************************************************************
  296. //
  297. //***************************************************************************
  298. // ok
  299. wchar_t* Registry::GetMultiStr(wchar_t *pszValueName, DWORD &dwSize)
  300. {
  301. //Find out the size of the buffer required
  302. DWORD dwType;
  303. if(hSubkey == NULL)
  304. return NULL;
  305. dwSize = 0;
  306. m_nLastError = RegQueryValueEx(hSubkey, pszValueName, 0, &dwType, NULL, &dwSize);
  307. if ((m_nLastError == ERROR_SUCCESS) && (dwType != REG_MULTI_SZ))
  308. {
  309. m_nLastError = WBEM_E_TYPE_MISMATCH;
  310. dwSize = 0;
  311. return NULL;
  312. }
  313. //If the error is an unexpected one bail out
  314. if ((m_nLastError != ERROR_SUCCESS) || (dwType != REG_MULTI_SZ))
  315. {
  316. dwSize = 0;
  317. return NULL;
  318. }
  319. if (dwSize == 0)
  320. {
  321. dwSize = 0;
  322. return NULL;
  323. }
  324. wmilib::auto_ptr<BYTE> pData( new BYTE[dwSize]);
  325. if (NULL == pData.get()) return NULL;
  326. //get the values
  327. m_nLastError = RegQueryValueEx(hSubkey,
  328. pszValueName,
  329. 0,
  330. &dwType,
  331. pData.get(),
  332. &dwSize);
  333. //if an error bail out
  334. if (m_nLastError != 0)
  335. {
  336. dwSize = 0;
  337. return NULL;
  338. }
  339. return (wchar_t *)pData.release();
  340. }
  341. //***************************************************************************
  342. //
  343. /// Use operator delete on returned value.
  344. //
  345. //***************************************************************************
  346. // ok
  347. int Registry::GetStr(wchar_t *pszValueName, wchar_t **pValue)
  348. {
  349. *pValue = 0;
  350. DWORD dwSize = 0;
  351. DWORD dwType = 0;
  352. if(hSubkey == NULL)
  353. return failed;
  354. m_nLastError = RegQueryValueEx(hSubkey, pszValueName, 0, &dwType,
  355. 0, &dwSize);
  356. if (m_nLastError != 0)
  357. return failed;
  358. if (dwType != REG_SZ && dwType != REG_EXPAND_SZ)
  359. return failed;
  360. //
  361. // length will not include the null terminated character when you don't
  362. // pass the buffer and the reg value was not already null terminated,
  363. // so make up for it. If you give RegQueryValueEx enough room in the buff
  364. // it will add the null terminator for you.
  365. //
  366. dwSize += sizeof(wchar_t);
  367. wchar_t *p = new wchar_t[dwSize]; // May be twice as big as required when _UNICODE
  368. // is defined, but harmless nonetheless.
  369. if (p == 0)
  370. return failed;
  371. m_nLastError = RegQueryValueEx(hSubkey, pszValueName, 0, &dwType,
  372. LPBYTE(p), &dwSize);
  373. if (m_nLastError != 0)
  374. {
  375. delete [] p;
  376. return failed;
  377. }
  378. if(dwType == REG_EXPAND_SZ)
  379. {
  380. wchar_t tTemp;
  381. // Get the initial length
  382. DWORD nSize = ExpandEnvironmentStrings((wchar_t *)p,&tTemp,1) + 1;
  383. wchar_t * pTemp = new wchar_t[nSize+1];
  384. if (pTemp == 0)
  385. {
  386. delete [] p;
  387. return failed;
  388. }
  389. ExpandEnvironmentStrings((wchar_t *)p,pTemp,nSize+1);
  390. delete [] p;
  391. *pValue = pTemp;
  392. }
  393. else
  394. *pValue = p;
  395. return no_error;
  396. }