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.

515 lines
16 KiB

  1. // --------------------------------------------------------------------------
  2. // Module Name: RegistryResources.cpp
  3. //
  4. // Copyright (c) 1999-2000, Microsoft Corporation
  5. //
  6. // General class definitions that assist in resource management. These are
  7. // typically stack based objects where constructors initialize to a known
  8. // state. Member functions operate on that resource. Destructors release
  9. // resources when the object goes out of scope.
  10. //
  11. // History: 1999-08-18 vtan created
  12. // 1999-11-16 vtan separate file
  13. // 2000-01-31 vtan moved from Neptune to Whistler
  14. // --------------------------------------------------------------------------
  15. #include "StandardHeader.h"
  16. #include "RegistryResources.h"
  17. #include <stdlib.h>
  18. #include "StringConvert.h"
  19. // --------------------------------------------------------------------------
  20. // CRegKey::CRegKey
  21. //
  22. // Arguments: <none>
  23. //
  24. // Returns: <none>
  25. //
  26. // Purpose: Initializes the CRegKey object.
  27. //
  28. // History: 1999-08-18 vtan created
  29. // --------------------------------------------------------------------------
  30. CRegKey::CRegKey (void) :
  31. _hKey(NULL),
  32. _dwIndex(0)
  33. {
  34. }
  35. // --------------------------------------------------------------------------
  36. // CRegKey::~CRegKey
  37. //
  38. // Arguments: <none>
  39. //
  40. // Returns: <none>
  41. //
  42. // Purpose: Releases resources used by the CRegKey object.
  43. //
  44. // History: 1999-08-18 vtan created
  45. // --------------------------------------------------------------------------
  46. CRegKey::~CRegKey (void)
  47. {
  48. TW32(Close());
  49. }
  50. // --------------------------------------------------------------------------
  51. // CRegKey::Create
  52. //
  53. // Arguments: See the platform SDK under advapi32!RegCreateKeyEx.
  54. //
  55. // Returns: LONG
  56. //
  57. // Purpose: See advapi32!RegCreateKeyEx.
  58. //
  59. // History: 1999-08-18 vtan created
  60. // --------------------------------------------------------------------------
  61. LONG CRegKey::Create (HKEY hKey, LPCTSTR lpSubKey, DWORD dwOptions, REGSAM samDesired, LPDWORD lpdwDisposition)
  62. {
  63. TW32(Close());
  64. return(RegCreateKeyEx(hKey, lpSubKey, 0, NULL, dwOptions, samDesired, NULL, &_hKey, lpdwDisposition));
  65. }
  66. // --------------------------------------------------------------------------
  67. // CRegKey::Open
  68. //
  69. // Arguments: See the platform SDK under advapi32!RegOpenKeyEx.
  70. //
  71. // Returns: LONG
  72. //
  73. // Purpose: See advapi32!RegOpenKeyEx.
  74. //
  75. // History: 1999-08-18 vtan created
  76. // --------------------------------------------------------------------------
  77. LONG CRegKey::Open (HKEY hKey, LPCTSTR lpSubKey, REGSAM samDesired)
  78. {
  79. TW32(Close());
  80. return(RegOpenKeyEx(hKey, lpSubKey, 0, samDesired, &_hKey));
  81. }
  82. // --------------------------------------------------------------------------
  83. // CRegKey::OpenCurrentUser
  84. //
  85. // Arguments: lpSubKey = Subkey to open under the current user.
  86. // samDesired = Desired access.
  87. //
  88. // Returns: LONG
  89. //
  90. // Purpose: Opens HKEY_CURRENT_USER\<lpSubKey> for the impersonated user.
  91. // If the thread isn't impersonating it opens the .default user.
  92. //
  93. // History: 2000-05-23 vtan created
  94. // --------------------------------------------------------------------------
  95. LONG CRegKey::OpenCurrentUser (LPCTSTR lpSubKey, REGSAM samDesired)
  96. {
  97. LONG lErrorCode;
  98. NTSTATUS status;
  99. HKEY hKeyCurrentUser;
  100. status = RtlOpenCurrentUser(samDesired, reinterpret_cast<void**>(&hKeyCurrentUser));
  101. if (NT_SUCCESS(status))
  102. {
  103. lErrorCode = Open(hKeyCurrentUser, lpSubKey, samDesired);
  104. TW32(RegCloseKey(hKeyCurrentUser));
  105. }
  106. else
  107. {
  108. lErrorCode = RtlNtStatusToDosError(status);
  109. }
  110. return(lErrorCode);
  111. }
  112. // --------------------------------------------------------------------------
  113. // CRegKey::QueryValue
  114. //
  115. // Arguments: See the platform SDK under advapi32!RegQueryValueEx.
  116. //
  117. // Returns: LONG
  118. //
  119. // Purpose: See advapi32!RegQueryValueEx.
  120. //
  121. // History: 1999-08-18 vtan created
  122. // --------------------------------------------------------------------------
  123. LONG CRegKey::QueryValue (LPCTSTR lpValueName, LPDWORD lpType, LPVOID lpData, LPDWORD lpcbData) const
  124. {
  125. ASSERTMSG(_hKey != NULL, "No open HKEY in CRegKey::QueryValue");
  126. return(RegQueryValueEx(_hKey, lpValueName, NULL, lpType, reinterpret_cast<LPBYTE>(lpData), lpcbData));
  127. }
  128. // --------------------------------------------------------------------------
  129. // CRegKey::SetValue
  130. //
  131. // Arguments: See the platform SDK under advapi32!RegSetValueEx.
  132. //
  133. // Returns: LONG
  134. //
  135. // Purpose: See advapi32!RegSetValueEx.
  136. //
  137. // History: 1999-08-18 vtan created
  138. // --------------------------------------------------------------------------
  139. LONG CRegKey::SetValue (LPCTSTR lpValueName, DWORD dwType, CONST VOID *lpData, DWORD cbData) const
  140. {
  141. ASSERTMSG(_hKey != NULL, "No open HKEY in CRegKey::SetValue");
  142. return(RegSetValueEx(_hKey, lpValueName, 0, dwType, reinterpret_cast<const unsigned char*>(lpData), cbData));
  143. }
  144. // --------------------------------------------------------------------------
  145. // CRegKey::DeleteValue
  146. //
  147. // Arguments: See the platform SDK under advapi32!RegDeleteValue.
  148. //
  149. // Returns: LONG
  150. //
  151. // Purpose: See advapi32!RegDeleteValue.
  152. //
  153. // History: 1999-10-31 vtan created
  154. // --------------------------------------------------------------------------
  155. LONG CRegKey::DeleteValue (LPCTSTR lpValueName) const
  156. {
  157. ASSERTMSG(_hKey != NULL, "No open HKEY in CRegKey::DeleteValue");
  158. return(RegDeleteValue(_hKey, lpValueName));
  159. }
  160. // --------------------------------------------------------------------------
  161. // CRegKey::QueryInfoKey
  162. //
  163. // Arguments: See the platform SDK under advapi32!RegQueryInfoKey.
  164. //
  165. // Returns: LONG
  166. //
  167. // Purpose: See advapi32!RegQueryInfoKey.
  168. //
  169. // History: 1999-08-18 vtan created
  170. // --------------------------------------------------------------------------
  171. LONG CRegKey::QueryInfoKey (LPTSTR lpClass, LPDWORD lpcbClass, LPDWORD lpcSubKeys, LPDWORD lpcbMaxSubKeyLen, LPDWORD lpcbMaxClassLen, LPDWORD lpcValues, LPDWORD lpcbMaxValueNameLen, LPDWORD lpcbMaxValueLen, LPDWORD lpcbSecurityDescriptor, PFILETIME lpftLastWriteTime) const
  172. {
  173. ASSERTMSG(_hKey != NULL, "No open HKEY in CRegKey::QueryInfoKey");
  174. return(RegQueryInfoKey(_hKey, lpClass, lpcbClass, NULL, lpcSubKeys, lpcbMaxSubKeyLen, lpcbMaxClassLen, lpcValues, lpcbMaxValueNameLen, lpcbMaxValueLen, lpcbSecurityDescriptor, lpftLastWriteTime));
  175. }
  176. // --------------------------------------------------------------------------
  177. // CRegKey::Reset
  178. //
  179. // Arguments: <none>
  180. //
  181. // Returns: <none>
  182. //
  183. // Purpose: Reset the enumeration index member variable used in
  184. // advapi32!RegEnumValue.
  185. //
  186. // History: 1999-08-18 vtan created
  187. // --------------------------------------------------------------------------
  188. void CRegKey::Reset (void)
  189. {
  190. _dwIndex = 0;
  191. }
  192. // --------------------------------------------------------------------------
  193. // CRegKey::Next
  194. //
  195. // Arguments: See the platform SDK under advapi32!RegEnumValue.
  196. //
  197. // Returns: LONG
  198. //
  199. // Purpose: See advapi32!RegEnumValue.
  200. //
  201. // History: 1999-08-18 vtan created
  202. // --------------------------------------------------------------------------
  203. LONG CRegKey::Next (LPTSTR lpValueName, LPDWORD lpcbValueName, LPDWORD lpType, LPVOID lpData, LPDWORD lpcbData)
  204. {
  205. return(RegEnumValue(_hKey, _dwIndex++, lpValueName, lpcbValueName, NULL, lpType, reinterpret_cast<LPBYTE>(lpData), lpcbData));
  206. }
  207. // --------------------------------------------------------------------------
  208. // CRegKey::GetString
  209. //
  210. // Arguments: pszValueName = Name of value in key to get data of.
  211. // pszValueData = String buffer to be filled with data.
  212. // pdwValueDataSize = Size (in characters) of buffer.
  213. //
  214. // Returns: LONG
  215. //
  216. // Purpose: Queries the registry key for the specified value and returns
  217. // the data to the caller. Asserts for REG_SZ.
  218. //
  219. // History: 1999-09-18 vtan created
  220. // --------------------------------------------------------------------------
  221. LONG CRegKey::GetString (const TCHAR *pszValueName, TCHAR *pszValueData, int iStringCount) const
  222. {
  223. LONG errorCode;
  224. DWORD dwType, dwValueDataSizeInBytes;
  225. dwValueDataSizeInBytes = iStringCount * sizeof(TCHAR);
  226. errorCode = QueryValue(pszValueName, &dwType, pszValueData, &dwValueDataSizeInBytes);
  227. if (ERROR_SUCCESS == errorCode)
  228. {
  229. if (dwType != REG_SZ)
  230. {
  231. DISPLAYMSG("CRegKey::GetString retrieved data that is not REG_SZ");
  232. errorCode = ERROR_INVALID_DATA;
  233. }
  234. }
  235. return(errorCode);
  236. }
  237. // --------------------------------------------------------------------------
  238. // CRegKey::GetPath
  239. //
  240. // Arguments: pszValueName = Name of value in key to get data of.
  241. // pszValueData = String buffer to be filled with data.
  242. //
  243. // Returns: LONG
  244. //
  245. // Purpose: Queries the registry key for the specified value and returns
  246. // the data to the caller. Asserts for REG_SZ or REG_EXPAND_SZ.
  247. // Also expands the path stored as well as assumes that MAX_PATH
  248. // is the buffer size.
  249. //
  250. // History: 1999-09-18 vtan created
  251. // --------------------------------------------------------------------------
  252. LONG CRegKey::GetPath (const TCHAR *pszValueName, TCHAR *pszValueData) const
  253. {
  254. LONG errorCode;
  255. DWORD dwType, dwRawPathSize;
  256. TCHAR szRawPath[MAX_PATH];
  257. dwRawPathSize = sizeof(szRawPath);
  258. errorCode = QueryValue(pszValueName, &dwType, szRawPath, &dwRawPathSize);
  259. if (ERROR_SUCCESS == errorCode)
  260. {
  261. if (dwType == REG_SZ)
  262. {
  263. lstrcpyn(pszValueData, szRawPath, MAX_PATH);
  264. }
  265. else if (dwType == REG_EXPAND_SZ)
  266. {
  267. if (ExpandEnvironmentStrings(szRawPath, pszValueData, MAX_PATH) == 0)
  268. {
  269. lstrcpyn(pszValueData, szRawPath, MAX_PATH);
  270. }
  271. }
  272. else
  273. {
  274. DISPLAYMSG("CRegKey::GetPath retrieved data that is not REG_SZ or REG_EXPAND_SZ");
  275. errorCode = ERROR_INVALID_DATA;
  276. }
  277. }
  278. return(errorCode);
  279. }
  280. // --------------------------------------------------------------------------
  281. // CRegKey::GetDWORD
  282. //
  283. // Arguments: pszValueName = Name of value in key to get data of.
  284. // pdwValueData = DWORD buffer to be filled with data.
  285. //
  286. // Returns: LONG
  287. //
  288. // Purpose: Queries the registry key for the specified value and returns
  289. // the data to the caller. Asserts for REG_DWORD.
  290. //
  291. // History: 1999-09-18 vtan created
  292. // --------------------------------------------------------------------------
  293. LONG CRegKey::GetDWORD (const TCHAR *pszValueName, DWORD& dwValueData) const
  294. {
  295. LONG errorCode;
  296. DWORD dwType, dwValueDataSize;
  297. dwValueDataSize = sizeof(DWORD);
  298. errorCode = QueryValue(pszValueName, &dwType, &dwValueData, &dwValueDataSize);
  299. if (ERROR_SUCCESS == errorCode)
  300. {
  301. if (dwType != REG_DWORD)
  302. {
  303. DISPLAYMSG("CRegKey::GetString retrieved data that is not REG_DWORD");
  304. errorCode = ERROR_INVALID_DATA;
  305. }
  306. }
  307. return(errorCode);
  308. }
  309. // --------------------------------------------------------------------------
  310. // CRegKey::GetInteger
  311. //
  312. // Arguments: pszValueName = Name of value in key to get data of.
  313. // piValueData = Integer buffer to be filled with data.
  314. //
  315. // Returns: LONG
  316. //
  317. // Purpose: Queries the registry key for the specified value and returns
  318. // the data to the caller. If the data is REG_DWORD this is
  319. // casted. If the data is REG_SZ this is converted. Everything
  320. // is illegal (including REG_EXPAND_SZ).
  321. //
  322. // History: 1999-09-18 vtan created
  323. // --------------------------------------------------------------------------
  324. LONG CRegKey::GetInteger (const TCHAR *pszValueName, int& iValueData) const
  325. {
  326. LONG errorCode;
  327. DWORD dwType, dwValueDataSize;
  328. errorCode = QueryValue(pszValueName, &dwType, NULL, NULL);
  329. if (ERROR_SUCCESS == errorCode)
  330. {
  331. if (dwType == REG_DWORD)
  332. {
  333. dwValueDataSize = sizeof(int);
  334. errorCode = QueryValue(pszValueName, NULL, &iValueData, &dwValueDataSize);
  335. }
  336. else if (dwType == REG_SZ)
  337. {
  338. TCHAR szTemp[32];
  339. dwValueDataSize = ARRAYSIZE(szTemp);
  340. errorCode = QueryValue(pszValueName, NULL, szTemp, &dwValueDataSize);
  341. if (ERROR_SUCCESS == errorCode)
  342. {
  343. char aszTemp[32];
  344. CStringConvert::TCharToAnsi(szTemp, aszTemp, ARRAYSIZE(aszTemp));
  345. iValueData = atoi(aszTemp);
  346. }
  347. }
  348. else
  349. {
  350. DISPLAYMSG("CRegKey::GetString retrieved data that is not REG_DWORD");
  351. errorCode = ERROR_INVALID_DATA;
  352. }
  353. }
  354. return(errorCode);
  355. }
  356. // --------------------------------------------------------------------------
  357. // CRegKey::SetString
  358. //
  359. // Arguments:
  360. //
  361. // Returns: LONG
  362. //
  363. // Purpose:
  364. //
  365. // History: 1999-10-26 vtan created
  366. // --------------------------------------------------------------------------
  367. LONG CRegKey::SetString (const TCHAR *pszValueName, const TCHAR *pszValueData) const
  368. {
  369. return(SetValue(pszValueName, REG_SZ, pszValueData, (lstrlen(pszValueData) + sizeof('\0')) * sizeof(TCHAR)));
  370. }
  371. // --------------------------------------------------------------------------
  372. // CRegKey::SetPath
  373. //
  374. // Arguments:
  375. //
  376. // Returns: LONG
  377. //
  378. // Purpose:
  379. //
  380. // History: 1999-10-26 vtan created
  381. // --------------------------------------------------------------------------
  382. LONG CRegKey::SetPath (const TCHAR *pszValueName, const TCHAR *pszValueData) const
  383. {
  384. return(SetValue(pszValueName, REG_EXPAND_SZ, pszValueData, (lstrlen(pszValueData) + sizeof('\0')) * sizeof(TCHAR)));
  385. }
  386. // --------------------------------------------------------------------------
  387. // CRegKey::SetDWORD
  388. //
  389. // Arguments:
  390. //
  391. // Returns: LONG
  392. //
  393. // Purpose:
  394. //
  395. // History: 1999-10-26 vtan created
  396. // --------------------------------------------------------------------------
  397. LONG CRegKey::SetDWORD (const TCHAR *pszValueName, DWORD dwValueData) const
  398. {
  399. return(SetValue(pszValueName, REG_DWORD, &dwValueData, sizeof(dwValueData)));
  400. }
  401. // --------------------------------------------------------------------------
  402. // CRegKey::SetInteger
  403. //
  404. // Arguments:
  405. //
  406. // Returns: LONG
  407. //
  408. // Purpose:
  409. //
  410. // History: 1999-10-26 vtan created
  411. // --------------------------------------------------------------------------
  412. LONG CRegKey::SetInteger (const TCHAR *pszValueName, int iValueData) const
  413. {
  414. TCHAR szString[kMaximumValueDataLength];
  415. wsprintf(szString, TEXT("%d"), iValueData);
  416. return(SetString(pszValueName, szString));
  417. }
  418. // --------------------------------------------------------------------------
  419. // CRegKey::Close
  420. //
  421. // Arguments: <none>
  422. //
  423. // Returns: LONG
  424. //
  425. // Purpose: Closes HKEY resource (if open).
  426. //
  427. // History: 1999-08-18 vtan created
  428. // --------------------------------------------------------------------------
  429. LONG CRegKey::Close (void)
  430. {
  431. LONG errorCode;
  432. if (_hKey != NULL)
  433. {
  434. errorCode = RegCloseKey(_hKey);
  435. _hKey = NULL;
  436. }
  437. else
  438. {
  439. errorCode = ERROR_SUCCESS;
  440. }
  441. return(errorCode);
  442. }