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.

495 lines
14 KiB

  1. // cregkey.h
  2. //
  3. #ifndef CREGKEY_H
  4. #define CREGKEY_H
  5. #include "osver.h"
  6. #include "xstring.h"
  7. /////////////////////////////////////////////////////////////////////////////
  8. //
  9. // CMyRegKey
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. class CMyRegKey
  13. {
  14. public:
  15. CMyRegKey();
  16. ~CMyRegKey();
  17. // Attributes
  18. public:
  19. operator HKEY() const;
  20. HKEY m_hKey;
  21. // Operations
  22. public:
  23. LONG SetValue(DWORD dwValue, LPCTSTR lpszValueName);
  24. virtual LONG QueryValue(DWORD& dwValue, LPCTSTR lpszValueName);
  25. virtual LONG QueryValueCch(LPTSTR szValue, LPCTSTR lpszValueName, ULONG cchValue);
  26. LONG SetValue(LPCTSTR lpszValue, LPCTSTR lpszValueName = NULL);
  27. LONG SetKeyValue(LPCTSTR lpszKeyName,
  28. LPCTSTR lpszValue,
  29. LPCTSTR lpszValueName = NULL);
  30. static LONG WINAPI SetValue(HKEY hKeyParent,
  31. LPCTSTR lpszKeyName,
  32. LPCTSTR lpszValue,
  33. LPCTSTR lpszValueName = NULL);
  34. LONG Create(HKEY hKeyParent,
  35. LPCTSTR lpszKeyName,
  36. LPTSTR lpszClass = REG_NONE,
  37. DWORD dwOptions = REG_OPTION_NON_VOLATILE,
  38. REGSAM samDesired = KEY_ALL_ACCESS,
  39. LPSECURITY_ATTRIBUTES lpSecAttr = NULL,
  40. LPDWORD lpdwDisposition = NULL);
  41. LONG Open(HKEY hKeyParent,
  42. LPCTSTR lpszKeyName,
  43. REGSAM samDesired);
  44. LONG Close();
  45. HKEY Detach();
  46. void Attach(HKEY hKey);
  47. LONG DeleteSubKey(LPCTSTR lpszSubKey);
  48. LONG RecurseDeleteKey(LPCTSTR lpszKey);
  49. LONG DeleteValue(LPCTSTR lpszValue);
  50. LONG QueryBinaryValue(void *p, DWORD& dwCount, LPCTSTR lpszValueName);
  51. LONG SetBinaryValue(void *p, DWORD dwCount, LPCTSTR lpszValueName);
  52. LONG EnumKey(DWORD dwIndex, LPTSTR lpName, ULONG cchName);
  53. LONG EnumValue(DWORD dwIndex, LPTSTR lpName, ULONG cchName);
  54. DWORD GetNumSubKeys();
  55. #ifndef UNICODE
  56. // Operations for Unicode
  57. public:
  58. LONG CreateW(HKEY hKeyParent,
  59. LPCWSTR lpszKeyName,
  60. LPWSTR lpszClass = REG_NONE,
  61. DWORD dwOptions = REG_OPTION_NON_VOLATILE,
  62. REGSAM samDesired = KEY_ALL_ACCESS,
  63. LPSECURITY_ATTRIBUTES lpSecAttr = NULL,
  64. LPDWORD lpdwDisposition = NULL);
  65. LONG OpenW(HKEY hKeyParent,
  66. LPCWSTR lpszKeyName,
  67. REGSAM samDesired);
  68. LONG SetValueW(const WCHAR *lpszValue,
  69. const WCHAR *lpszValueName = NULL,
  70. ULONG cch = (ULONG)-1);
  71. virtual LONG QueryValueCchW(WCHAR *lpszValue, const WCHAR *lpszValueName, ULONG cchValue);
  72. LONG EnumValueW(DWORD dwIndex, WCHAR *lpName, ULONG cchName);
  73. LONG EnumKeyW(DWORD dwIndex, WCHAR *lpName, ULONG cchName);
  74. LONG DeleteValueW(const WCHAR *lpszValue, ULONG cch = (ULONG)-1);
  75. #endif // UNICODE
  76. };
  77. inline CMyRegKey::CMyRegKey()
  78. {
  79. m_hKey = NULL;
  80. }
  81. inline CMyRegKey::~CMyRegKey()
  82. {
  83. Close();
  84. }
  85. inline CMyRegKey::operator HKEY() const
  86. {
  87. return m_hKey;
  88. }
  89. inline HKEY CMyRegKey::Detach()
  90. {
  91. HKEY hKey = m_hKey;
  92. m_hKey = NULL;
  93. return hKey;
  94. }
  95. inline void CMyRegKey::Attach(HKEY hKey)
  96. {
  97. Assert(m_hKey == NULL);
  98. m_hKey = hKey;
  99. }
  100. inline LONG CMyRegKey::DeleteSubKey(LPCTSTR lpszSubKey)
  101. {
  102. Assert(m_hKey != NULL);
  103. return RegDeleteKey(m_hKey, lpszSubKey);
  104. }
  105. inline LONG CMyRegKey::DeleteValue(LPCTSTR lpszValue)
  106. {
  107. Assert(m_hKey != NULL);
  108. return RegDeleteValue(m_hKey, (LPTSTR)lpszValue);
  109. }
  110. inline LONG CMyRegKey::Close()
  111. {
  112. LONG lRes = ERROR_SUCCESS;
  113. if (m_hKey != NULL)
  114. {
  115. lRes = RegCloseKey(m_hKey);
  116. m_hKey = NULL;
  117. }
  118. return lRes;
  119. }
  120. inline LONG CMyRegKey::Create(HKEY hKeyParent, LPCTSTR lpszKeyName,
  121. LPTSTR lpszClass, DWORD dwOptions, REGSAM samDesired,
  122. LPSECURITY_ATTRIBUTES lpSecAttr, LPDWORD lpdwDisposition)
  123. {
  124. Assert(hKeyParent != NULL);
  125. DWORD dw;
  126. HKEY hKey = NULL;
  127. LONG lRes = RegCreateKeyEx(hKeyParent, lpszKeyName, 0,
  128. lpszClass, dwOptions, samDesired, lpSecAttr, &hKey, &dw);
  129. if (lpdwDisposition != NULL)
  130. *lpdwDisposition = dw;
  131. if (lRes == ERROR_SUCCESS)
  132. {
  133. lRes = Close();
  134. m_hKey = hKey;
  135. }
  136. return lRes;
  137. }
  138. inline LONG CMyRegKey::Open(HKEY hKeyParent, LPCTSTR lpszKeyName, REGSAM samDesired)
  139. {
  140. Assert(hKeyParent != NULL);
  141. HKEY hKey = NULL;
  142. LONG lRes = RegOpenKeyEx(hKeyParent, lpszKeyName, 0, samDesired, &hKey);
  143. if (lRes == ERROR_SUCCESS)
  144. {
  145. lRes = Close();
  146. Assert(lRes == ERROR_SUCCESS);
  147. m_hKey = hKey;
  148. }
  149. return lRes;
  150. }
  151. inline LONG CMyRegKey::QueryValue(DWORD& dwValue, LPCTSTR lpszValueName)
  152. {
  153. DWORD dwType = NULL;
  154. DWORD dwCount = sizeof(DWORD);
  155. LONG lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, NULL, &dwType,
  156. (LPBYTE)&dwValue, &dwCount);
  157. Assert((lRes!=ERROR_SUCCESS) || (dwType == REG_DWORD));
  158. Assert((lRes!=ERROR_SUCCESS) || (dwCount == sizeof(DWORD)));
  159. return lRes;
  160. }
  161. inline LONG CMyRegKey::QueryValueCch(LPTSTR szValue, LPCTSTR lpszValueName, ULONG cchValue)
  162. {
  163. Assert(szValue != NULL);
  164. DWORD cb = cchValue*sizeof(TCHAR);
  165. DWORD dwType = NULL;
  166. LONG lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, NULL, &dwType,
  167. (LPBYTE)szValue, &cb);
  168. Assert((lRes!=ERROR_SUCCESS) || (dwType == REG_SZ) ||
  169. (dwType == REG_MULTI_SZ) || (dwType == REG_EXPAND_SZ));
  170. // make sure we're null-terminated no matter what
  171. // RegQueryValueEx does not guarentee null-terminated strings
  172. if (cchValue > 0)
  173. {
  174. szValue[lRes == ERROR_SUCCESS ? cchValue-1 : 0] = '\0';
  175. }
  176. return lRes;
  177. }
  178. inline LONG WINAPI CMyRegKey::SetValue(HKEY hKeyParent, LPCTSTR lpszKeyName, LPCTSTR lpszValue, LPCTSTR lpszValueName)
  179. {
  180. Assert(lpszValue != NULL);
  181. CMyRegKey key;
  182. LONG lRes = key.Create(hKeyParent, lpszKeyName);
  183. if (lRes == ERROR_SUCCESS)
  184. lRes = key.SetValue(lpszValue, lpszValueName);
  185. return lRes;
  186. }
  187. inline LONG CMyRegKey::SetKeyValue(LPCTSTR lpszKeyName, LPCTSTR lpszValue, LPCTSTR lpszValueName)
  188. {
  189. Assert(lpszValue != NULL);
  190. CMyRegKey key;
  191. LONG lRes = key.Create(m_hKey, lpszKeyName);
  192. if (lRes == ERROR_SUCCESS)
  193. lRes = key.SetValue(lpszValue, lpszValueName);
  194. return lRes;
  195. }
  196. inline LONG CMyRegKey::SetValue(DWORD dwValue, LPCTSTR lpszValueName)
  197. {
  198. Assert(m_hKey != NULL);
  199. return RegSetValueEx(m_hKey, lpszValueName, NULL, REG_DWORD,
  200. (BYTE * const)&dwValue, sizeof(DWORD));
  201. }
  202. inline LONG CMyRegKey::SetValue(LPCTSTR lpszValue, LPCTSTR lpszValueName)
  203. {
  204. Assert(lpszValue != NULL);
  205. Assert(m_hKey != NULL);
  206. return RegSetValueEx(m_hKey, lpszValueName, NULL, REG_SZ,
  207. (BYTE * const)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR));
  208. }
  209. inline LONG CMyRegKey::RecurseDeleteKey(LPCTSTR lpszKey)
  210. {
  211. CMyRegKey key;
  212. LONG lRes = key.Open(m_hKey, lpszKey, KEY_READ | KEY_WRITE);
  213. if (lRes != ERROR_SUCCESS)
  214. return lRes;
  215. FILETIME time;
  216. TCHAR szBuffer[256];
  217. DWORD dwSize = ARRAYSIZE(szBuffer);
  218. while (RegEnumKeyEx(key.m_hKey, 0, szBuffer, &dwSize, NULL, NULL, NULL,
  219. &time)==ERROR_SUCCESS)
  220. {
  221. szBuffer[ARRAYSIZE(szBuffer)-1] = '\0';
  222. lRes = key.RecurseDeleteKey(szBuffer);
  223. if (lRes != ERROR_SUCCESS)
  224. return lRes;
  225. dwSize = ARRAYSIZE(szBuffer);
  226. }
  227. key.Close();
  228. return DeleteSubKey(lpszKey);
  229. }
  230. inline LONG CMyRegKey::QueryBinaryValue(void *p, DWORD& dwCount, LPCTSTR lpszValueName)
  231. {
  232. DWORD dwType = REG_BINARY;
  233. LONG lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, 0,
  234. &dwType, (BYTE *)p, &dwCount);
  235. Assert((lRes!=ERROR_SUCCESS) || (dwType == REG_BINARY));
  236. return lRes;
  237. }
  238. inline LONG CMyRegKey::SetBinaryValue(void *p, DWORD dwCount, LPCTSTR lpszValueName)
  239. {
  240. Assert(m_hKey != NULL);
  241. return RegSetValueEx(m_hKey, lpszValueName, NULL, REG_BINARY,
  242. (BYTE * const)p, dwCount);
  243. }
  244. inline LONG CMyRegKey::EnumKey(DWORD dwIndex, LPTSTR lpName, ULONG cchName)
  245. {
  246. LONG lResult;
  247. ULONG cchNameIn;
  248. cchNameIn = cchName;
  249. lResult = RegEnumKeyEx(m_hKey, dwIndex, lpName, &cchName, NULL, NULL,
  250. NULL, NULL);
  251. // null-terminate
  252. if (cchNameIn > 0)
  253. {
  254. lpName[lResult == ERROR_SUCCESS ? cchNameIn-1 : 0] = '\0';
  255. }
  256. return lResult;
  257. }
  258. inline LONG CMyRegKey::EnumValue(DWORD dwIndex, LPTSTR lpName, ULONG cchName)
  259. {
  260. LONG lResult;
  261. ULONG cchNameIn;
  262. cchNameIn = cchName;
  263. lResult = RegEnumValue(m_hKey, dwIndex, lpName, &cchName, NULL, NULL,
  264. NULL, NULL);
  265. // null-terminate
  266. if (cchNameIn > 0)
  267. {
  268. lpName[lResult == ERROR_SUCCESS ? cchNameIn-1 : 0] = '\0';
  269. }
  270. return lResult;
  271. }
  272. #ifndef UNICODE
  273. inline LONG CMyRegKey::CreateW(HKEY hKeyParent, LPCWSTR lpszKeyName,
  274. LPWSTR lpszClass, DWORD dwOptions, REGSAM samDesired,
  275. LPSECURITY_ATTRIBUTES lpSecAttr, LPDWORD lpdwDisposition)
  276. {
  277. if (IsOnNT())
  278. {
  279. Assert(hKeyParent != NULL);
  280. DWORD dw;
  281. HKEY hKey = NULL;
  282. LONG lRes = RegCreateKeyExW(hKeyParent, lpszKeyName, 0,
  283. lpszClass, dwOptions, samDesired, lpSecAttr, &hKey, &dw);
  284. if (lpdwDisposition != NULL)
  285. *lpdwDisposition = dw;
  286. if (lRes == ERROR_SUCCESS)
  287. {
  288. lRes = Close();
  289. m_hKey = hKey;
  290. }
  291. return lRes;
  292. }
  293. return Create(hKeyParent, (LPCTSTR)WtoA(lpszKeyName),
  294. lpszClass ? (LPTSTR)WtoA(lpszClass) : REG_NONE,
  295. dwOptions, samDesired,
  296. lpSecAttr, lpdwDisposition);
  297. }
  298. inline LONG CMyRegKey::OpenW(HKEY hKeyParent, LPCWSTR lpszKeyName, REGSAM samDesired)
  299. {
  300. if (IsOnNT())
  301. {
  302. Assert(hKeyParent != NULL);
  303. HKEY hKey = NULL;
  304. LONG lRes = RegOpenKeyExW(hKeyParent, lpszKeyName, 0, samDesired, &hKey);
  305. if (lRes == ERROR_SUCCESS)
  306. {
  307. lRes = Close();
  308. Assert(lRes == ERROR_SUCCESS);
  309. m_hKey = hKey;
  310. }
  311. return lRes;
  312. }
  313. return Open(hKeyParent, WtoA(lpszKeyName), samDesired);
  314. }
  315. inline LONG CMyRegKey::SetValueW(const WCHAR *lpszValue, const WCHAR *lpszValueName, ULONG cch)
  316. {
  317. Assert(lpszValue != NULL);
  318. Assert(m_hKey != NULL);
  319. if (IsOnNT())
  320. {
  321. return RegSetValueExW(m_hKey,
  322. lpszValueName,
  323. NULL,
  324. REG_SZ,
  325. (BYTE * const)lpszValue,
  326. (cch == -1) ? (wcslen(lpszValue)+1)*sizeof(WCHAR) : cch);
  327. }
  328. WtoA szValue(lpszValue, cch);
  329. WtoA szValueName(lpszValueName);
  330. return RegSetValueExA(m_hKey,
  331. (char *)szValueName,
  332. NULL,
  333. REG_SZ,
  334. (BYTE * const)(char *)szValue,
  335. (lstrlenA(szValue)+1));
  336. }
  337. inline LONG CMyRegKey::QueryValueCchW(WCHAR *lpszValue, const WCHAR *lpszValueName, ULONG cchValue)
  338. {
  339. Assert(lpszValue != NULL);
  340. DWORD dwType = NULL;
  341. DWORD cb;
  342. LONG lRes;
  343. Assert(IsOnNT()); // we don't support win9x anymore
  344. cb = cchValue*sizeof(WCHAR);
  345. lRes = RegQueryValueExW(m_hKey,
  346. (WCHAR *)lpszValueName,
  347. NULL,
  348. &dwType,
  349. (BYTE *)lpszValue,
  350. &cb);
  351. Assert((lRes!=ERROR_SUCCESS) || (dwType == REG_SZ) ||
  352. (dwType == REG_MULTI_SZ) || (dwType == REG_EXPAND_SZ));
  353. // make sure we're null-terminated no matter what
  354. // RegQueryValueEx does not guarentee null-terminated strings
  355. if (cchValue > 0)
  356. {
  357. lpszValue[lRes == ERROR_SUCCESS ? cchValue-1 : 0] = '\0';
  358. }
  359. return lRes;
  360. }
  361. inline LONG CMyRegKey::DeleteValueW(const WCHAR *lpszValue, ULONG cch)
  362. {
  363. Assert(m_hKey != NULL);
  364. if (IsOnNT())
  365. return RegDeleteValueW(m_hKey, (WCHAR *)lpszValue);
  366. return RegDeleteValueA(m_hKey, WtoA(lpszValue, cch));
  367. }
  368. inline DWORD CMyRegKey::GetNumSubKeys()
  369. {
  370. DWORD dwNum = 0;
  371. if (RegQueryInfoKey(m_hKey, NULL, NULL, NULL,
  372. &dwNum, NULL, NULL, NULL,
  373. NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
  374. {
  375. dwNum = 0;
  376. }
  377. return dwNum;
  378. }
  379. inline LONG CMyRegKey::EnumValueW(DWORD dwIndex, WCHAR *lpName, ULONG cchName)
  380. {
  381. LONG lResult;
  382. ULONG cchNameIn;
  383. Assert(IsOnNT()); // we don't support win9x anymore
  384. cchNameIn = cchName;
  385. lResult = RegEnumValueW(m_hKey, dwIndex, lpName, &cchName, NULL, NULL,
  386. NULL, NULL);
  387. // null-terminate
  388. if (cchNameIn > 0)
  389. {
  390. lpName[lResult == ERROR_SUCCESS ? cchNameIn-1 : 0] = '\0';
  391. }
  392. return lResult;
  393. }
  394. inline LONG CMyRegKey::EnumKeyW(DWORD dwIndex, WCHAR *lpName, ULONG cchName)
  395. {
  396. LONG lResult;
  397. ULONG cchNameIn;
  398. Assert(IsOnNT()); // we don't support win9x anymore
  399. cchNameIn = cchName;
  400. lResult = RegEnumKeyExW(m_hKey, dwIndex, lpName, &cchName, NULL, NULL,
  401. NULL, NULL);
  402. // null-terminate
  403. if (cchNameIn > 0)
  404. {
  405. lpName[lResult == ERROR_SUCCESS ? cchNameIn-1 : 0] = '\0';
  406. }
  407. return lResult;
  408. }
  409. #endif // UNICODE
  410. #endif // CREGKEY_H