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.

402 lines
8.5 KiB

  1. #include "shellprv.h"
  2. #pragma hdrstop
  3. #include "regsuprt.h"
  4. #ifdef DEBUG
  5. UINT CRegSupport::_cRefHKEY = 0;
  6. UINT CRegSupport::_cRefExternalHKEY = 0;
  7. #endif
  8. void CRegSupport::RSInitRoot(HKEY hkey, LPCTSTR pszSubKey1, LPCTSTR pszSubKey2,
  9. DWORD dwRootOptions, DWORD dwDefaultOptions)
  10. {
  11. _dwRootOptions = dwRootOptions;
  12. _dwDefaultOptions = dwDefaultOptions;
  13. _hkeyInit = hkey;
  14. _InitSetRoot(pszSubKey1, pszSubKey2);
  15. #ifdef DEBUG
  16. ASSERT(!_fInited);
  17. _fInited = TRUE;
  18. #endif
  19. }
  20. BOOL CRegSupport::RSSubKeyExist(LPCTSTR pszSubKey)
  21. {
  22. BOOL fRet = FALSE;
  23. HKEY hkeySubKey = NULL;
  24. _EnterCSKeyRoot();
  25. if (pszSubKey && *pszSubKey)
  26. hkeySubKey = _GetSubKey(pszSubKey, FALSE);
  27. else
  28. hkeySubKey = _GetRootKey(FALSE);
  29. if (hkeySubKey)
  30. {
  31. fRet = TRUE;
  32. _CloseRegSubKey(hkeySubKey);
  33. }
  34. _LeaveCSKeyRoot();
  35. return fRet;
  36. }
  37. BOOL CRegSupport::RSValueExist(LPCTSTR pszSubKey, LPCTSTR pszValueName)
  38. {
  39. BOOL fRet = FALSE;
  40. HKEY hkeySubKey = NULL;
  41. _EnterCSKeyRoot();
  42. if (pszSubKey && *pszSubKey)
  43. hkeySubKey = _GetSubKey(pszSubKey, FALSE);
  44. else
  45. hkeySubKey = _GetRootKey(FALSE);
  46. if (hkeySubKey)
  47. {
  48. fRet = (RegQueryValueEx(hkeySubKey, pszValueName, 0, NULL, NULL, NULL) ==
  49. ERROR_SUCCESS);
  50. _CloseRegSubKey(hkeySubKey);
  51. }
  52. _LeaveCSKeyRoot();
  53. return fRet;
  54. }
  55. BOOL CRegSupport::RSDeleteValue(LPCTSTR pszSubKey, LPCTSTR pszValueName)
  56. {
  57. BOOL fRet = FALSE;
  58. HKEY hkeySubKey = NULL;
  59. _EnterCSKeyRoot();
  60. if (pszSubKey && *pszSubKey)
  61. hkeySubKey = _GetSubKey(pszSubKey, FALSE);
  62. else
  63. hkeySubKey = _GetRootKey(FALSE);
  64. if (hkeySubKey)
  65. {
  66. if (ERROR_SUCCESS == RegDeleteValue(hkeySubKey, pszValueName))
  67. {
  68. fRet = TRUE;
  69. }
  70. _CloseRegSubKey(hkeySubKey);
  71. }
  72. _LeaveCSKeyRoot();
  73. return fRet;
  74. }
  75. BOOL CRegSupport::RSDeleteKey()
  76. {
  77. TCHAR szRoot[MAX_ROOT];
  78. return (ERROR_SUCCESS == SHDeleteKey(_hkeyInit, _GetRoot(szRoot, ARRAYSIZE(szRoot))));
  79. }
  80. BOOL CRegSupport::RSDeleteSubKey(LPCTSTR pszSubKey)
  81. {
  82. BOOL fRet = FALSE;
  83. _EnterCSKeyRoot();
  84. HKEY hkey = _GetRootKey(FALSE);
  85. if (hkey)
  86. {
  87. if (ERROR_SUCCESS == SHDeleteKey(hkey, pszSubKey))
  88. {
  89. fRet = TRUE;
  90. }
  91. _CloseRegSubKey(hkey);
  92. }
  93. _LeaveCSKeyRoot();
  94. return fRet;
  95. }
  96. BOOL CRegSupport::RSSetBinaryValue(LPCTSTR pszSubKey, LPCTSTR pszValueName,
  97. PBYTE pb, DWORD cb,
  98. DWORD dwOptions)
  99. {
  100. return _SetGeneric(pszSubKey, pszValueName, pb, cb, REG_BINARY, dwOptions);
  101. }
  102. BOOL CRegSupport::RSSetTextValue(LPCTSTR pszSubKey, LPCTSTR pszValueName,
  103. LPCTSTR pszValue,
  104. DWORD dwOptions)
  105. {
  106. return _SetGeneric(pszSubKey, pszValueName, (PBYTE)pszValue,
  107. (lstrlen(pszValue) + 1) * sizeof(TCHAR), REG_SZ, dwOptions);
  108. }
  109. BOOL CRegSupport::RSSetDWORDValue(LPCTSTR pszSubKey, LPCTSTR pszValueName,
  110. DWORD dwValue,
  111. DWORD dwOptions)
  112. {
  113. return _SetGeneric(pszSubKey, pszValueName, (PBYTE)&dwValue,
  114. sizeof(DWORD), REG_DWORD, dwOptions);
  115. }
  116. BOOL CRegSupport::RSGetBinaryValue(LPCTSTR pszSubKey, LPCTSTR pszValueName,
  117. PBYTE pb, DWORD* pcb)
  118. {
  119. return _GetGeneric(pszSubKey, pszValueName, pb, pcb);
  120. }
  121. BOOL CRegSupport::RSGetTextValue(LPCTSTR pszSubKey, LPCTSTR pszValueName,
  122. LPTSTR pszValue, DWORD* pcchValue)
  123. {
  124. DWORD cbData = *pcchValue * sizeof(TCHAR);
  125. return _GetGeneric(pszSubKey, pszValueName, (PBYTE)pszValue, &cbData);
  126. }
  127. BOOL CRegSupport::RSGetDWORDValue(LPCTSTR pszSubKey, LPCTSTR pszValueName, DWORD* pdwValue)
  128. {
  129. DWORD cbData = sizeof(DWORD);
  130. return _GetGeneric(pszSubKey, pszValueName, (PBYTE)pdwValue, &cbData);
  131. }
  132. BOOL CRegSupport::_SetGeneric(LPCTSTR pszSubKey, LPCTSTR pszValueName,
  133. PBYTE pb, DWORD cb, DWORD dwType,
  134. DWORD dwOptions)
  135. {
  136. BOOL fRet = FALSE;
  137. HKEY hkeySubKey = NULL;
  138. _EnterCSKeyRoot();
  139. if (pszSubKey && *pszSubKey)
  140. hkeySubKey = _GetSubKey(pszSubKey, TRUE, dwOptions);
  141. else
  142. hkeySubKey = _GetRootKey(TRUE, dwOptions);
  143. if (hkeySubKey)
  144. {
  145. if (ERROR_SUCCESS == RegSetValueEx(hkeySubKey, pszValueName, 0,
  146. dwType, pb, cb))
  147. {
  148. fRet = TRUE;
  149. }
  150. _CloseRegSubKey(hkeySubKey);
  151. }
  152. _LeaveCSKeyRoot();
  153. return fRet;
  154. }
  155. BOOL CRegSupport::_GetGeneric(LPCTSTR pszSubKey, LPCTSTR pszValueName,
  156. PBYTE pb, DWORD* pcb)
  157. {
  158. BOOL fRet = FALSE;
  159. HKEY hkeySubKey = NULL;
  160. _EnterCSKeyRoot();
  161. if (pszSubKey && *pszSubKey)
  162. hkeySubKey = _GetSubKey(pszSubKey, FALSE);
  163. else
  164. hkeySubKey = _GetRootKey(FALSE);
  165. if (hkeySubKey)
  166. {
  167. if (ERROR_SUCCESS == SHQueryValueEx(hkeySubKey, pszValueName, 0,
  168. NULL, pb, pcb))
  169. {
  170. fRet = TRUE;
  171. }
  172. _CloseRegSubKey(hkeySubKey);
  173. }
  174. _LeaveCSKeyRoot();
  175. return fRet;
  176. }
  177. HKEY CRegSupport::RSDuplicateRootKey()
  178. {
  179. RIP(_fInited);
  180. #ifdef DEBUG
  181. // we need to decrement here since it will be icnremented inside this fct
  182. // and the key will not be close by this object
  183. --_cRefHKEY;
  184. ++_cRefExternalHKEY;
  185. #endif
  186. TCHAR szRoot[MAX_ROOT];
  187. return _RegCreateKeyExHelper(_hkeyInit, _GetRoot(szRoot, ARRAYSIZE(szRoot)), _dwRootOptions);
  188. }
  189. HKEY CRegSupport::RSDuplicateSubKey(LPCTSTR pszSubKey, BOOL fCreate, BOOL fVolatile)
  190. {
  191. return _GetSubKey(pszSubKey, fCreate, fVolatile ? REG_OPTION_VOLATILE : REG_OPTION_NON_VOLATILE);
  192. }
  193. HKEY CRegSupport::_GetRootKey(BOOL fCreate, DWORD dwOptions)
  194. {
  195. RIP(_fInited);
  196. HKEY hkey;
  197. TCHAR szRoot[MAX_ROOT];
  198. if (REG_OPTION_INVALID == dwOptions)
  199. dwOptions = _dwRootOptions;
  200. if (fCreate)
  201. hkey = _RegCreateKeyExHelper(_hkeyInit, _GetRoot(szRoot, ARRAYSIZE(szRoot)), dwOptions);
  202. else
  203. hkey = _RegOpenKeyExHelper(_hkeyInit, _GetRoot(szRoot, ARRAYSIZE(szRoot)));
  204. return hkey;
  205. }
  206. void CRegSupport::_CloseRegSubKey(HKEY hkeySubKey)
  207. {
  208. RegCloseKey(hkeySubKey);
  209. #ifdef DEBUG
  210. --_cRefHKEY;
  211. #endif
  212. }
  213. // Always need to be called from within the _csRootKey critical section (when critical section
  214. // stuff is enabled)
  215. HKEY CRegSupport::_GetSubKey(LPCTSTR pszSubKey, BOOL fCreate, DWORD dwOptions)
  216. {
  217. HKEY hkey = NULL;
  218. HKEY hRootKey = _GetRootKey(fCreate, dwOptions);
  219. if (REG_OPTION_INVALID == dwOptions)
  220. dwOptions = _dwDefaultOptions;
  221. if (hRootKey)
  222. {
  223. if (fCreate)
  224. hkey = _RegCreateKeyExHelper(hRootKey, pszSubKey, dwOptions);
  225. else
  226. hkey = _RegOpenKeyExHelper(hRootKey, pszSubKey);
  227. _CloseRegSubKey(hRootKey);
  228. }
  229. return hkey;
  230. }
  231. //static
  232. HKEY CRegSupport::_RegCreateKeyExHelper(HKEY hkey, LPCTSTR pszSubKey, DWORD dwOptions)
  233. {
  234. HKEY hkeyTmp;
  235. DWORD dwDisp;
  236. ASSERT(REG_OPTION_INVALID != dwOptions);
  237. if (ERROR_SUCCESS != RegCreateKeyEx(hkey, pszSubKey, 0, NULL,
  238. dwOptions, MAXIMUM_ALLOWED, NULL, &hkeyTmp, &dwDisp))
  239. {
  240. hkeyTmp = NULL;
  241. }
  242. #ifdef DEBUG
  243. else
  244. {
  245. ++_cRefHKEY;
  246. }
  247. #endif
  248. return hkeyTmp;
  249. }
  250. //static
  251. HKEY CRegSupport::_RegOpenKeyExHelper(HKEY hkey, LPCTSTR pszSubKey)
  252. {
  253. HKEY hkeyTmp;
  254. if (ERROR_SUCCESS != RegOpenKeyEx(hkey, pszSubKey, 0,
  255. MAXIMUM_ALLOWED, &hkeyTmp))
  256. {
  257. hkeyTmp = NULL;
  258. }
  259. #ifdef DEBUG
  260. else
  261. {
  262. ++_cRefHKEY;
  263. }
  264. #endif
  265. return hkeyTmp;
  266. }
  267. BOOL CRegSupport::_InitSetRoot(LPCTSTR pszSubKey1, LPCTSTR pszSubKey2)
  268. {
  269. _pszSubKey1 = pszSubKey1;
  270. _pszSubKey2 = pszSubKey2;
  271. return TRUE;
  272. }
  273. LPCTSTR CRegSupport::_GetRoot(LPTSTR pszRoot, DWORD cchRoot)
  274. {
  275. ASSERT(cchRoot > 0);
  276. lstrcpyn(pszRoot, _pszSubKey1, cchRoot);
  277. if (_pszSubKey2)
  278. {
  279. lstrcatn(pszRoot, TEXT("\\"), cchRoot);
  280. lstrcatn(pszRoot, _pszSubKey2, cchRoot);
  281. }
  282. return pszRoot;
  283. }
  284. void CRegSupport::_InitCSKeyRoot()
  285. {
  286. ASSERT(!_fcsKeyRoot);
  287. _fcsKeyRoot = TRUE;
  288. InitializeCriticalSection(&_csKeyRoot);
  289. }
  290. void CRegSupport::_EnterCSKeyRoot()
  291. {
  292. if (_fcsKeyRoot)
  293. {
  294. EnterCriticalSection(&_csKeyRoot);
  295. }
  296. }
  297. void CRegSupport::_LeaveCSKeyRoot()
  298. {
  299. if (_fcsKeyRoot)
  300. {
  301. LeaveCriticalSection(&_csKeyRoot);
  302. }
  303. }
  304. CRegSupport::CRegSupport()
  305. {}
  306. CRegSupport::~CRegSupport()
  307. {
  308. if (_fcsKeyRoot)
  309. {
  310. DeleteCriticalSection(&_csKeyRoot);
  311. }
  312. }