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.

418 lines
9.2 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. BOOL bRet = FALSE;
  78. TCHAR szRoot[MAX_ROOT];
  79. if (_hkeyInit)
  80. {
  81. if (SHDeleteKey(_hkeyInit, _GetRoot(szRoot, ARRAYSIZE(szRoot))) == ERROR_SUCCESS)
  82. {
  83. bRet = TRUE;
  84. }
  85. }
  86. return bRet;
  87. }
  88. BOOL CRegSupport::RSDeleteSubKey(LPCTSTR pszSubKey)
  89. {
  90. BOOL fRet = FALSE;
  91. _EnterCSKeyRoot();
  92. HKEY hkey = _GetRootKey(FALSE);
  93. if (hkey)
  94. {
  95. if (ERROR_SUCCESS == SHDeleteKey(hkey, pszSubKey))
  96. {
  97. fRet = TRUE;
  98. }
  99. _CloseRegSubKey(hkey);
  100. }
  101. _LeaveCSKeyRoot();
  102. return fRet;
  103. }
  104. BOOL CRegSupport::RSSetBinaryValue(LPCTSTR pszSubKey, LPCTSTR pszValueName,
  105. PBYTE pb, DWORD cb,
  106. DWORD dwOptions)
  107. {
  108. return _SetGeneric(pszSubKey, pszValueName, pb, cb, REG_BINARY, dwOptions);
  109. }
  110. BOOL CRegSupport::RSSetTextValue(LPCTSTR pszSubKey, LPCTSTR pszValueName,
  111. LPCTSTR pszValue,
  112. DWORD dwOptions)
  113. {
  114. return _SetGeneric(pszSubKey, pszValueName, (PBYTE)pszValue,
  115. (lstrlen(pszValue) + 1) * sizeof(TCHAR), REG_SZ, dwOptions);
  116. }
  117. BOOL CRegSupport::RSSetDWORDValue(LPCTSTR pszSubKey, LPCTSTR pszValueName,
  118. DWORD dwValue,
  119. DWORD dwOptions)
  120. {
  121. return _SetGeneric(pszSubKey, pszValueName, (PBYTE)&dwValue,
  122. sizeof(DWORD), REG_DWORD, dwOptions);
  123. }
  124. BOOL CRegSupport::RSGetBinaryValue(LPCTSTR pszSubKey, LPCTSTR pszValueName,
  125. PBYTE pb, DWORD* pcb)
  126. {
  127. return _GetGeneric(pszSubKey, pszValueName, pb, pcb);
  128. }
  129. BOOL CRegSupport::RSGetTextValue(LPCTSTR pszSubKey, LPCTSTR pszValueName,
  130. LPTSTR pszValue, DWORD* pcchValue)
  131. {
  132. DWORD cbData = *pcchValue * sizeof(TCHAR);
  133. return _GetGeneric(pszSubKey, pszValueName, (PBYTE)pszValue, &cbData);
  134. }
  135. BOOL CRegSupport::RSGetDWORDValue(LPCTSTR pszSubKey, LPCTSTR pszValueName, DWORD* pdwValue)
  136. {
  137. DWORD cbData = sizeof(DWORD);
  138. return _GetGeneric(pszSubKey, pszValueName, (PBYTE)pdwValue, &cbData);
  139. }
  140. BOOL CRegSupport::_SetGeneric(LPCTSTR pszSubKey, LPCTSTR pszValueName,
  141. PBYTE pb, DWORD cb, DWORD dwType,
  142. DWORD dwOptions)
  143. {
  144. BOOL fRet = FALSE;
  145. HKEY hkeySubKey = NULL;
  146. _EnterCSKeyRoot();
  147. if (pszSubKey && *pszSubKey)
  148. hkeySubKey = _GetSubKey(pszSubKey, TRUE, dwOptions);
  149. else
  150. hkeySubKey = _GetRootKey(TRUE, dwOptions);
  151. if (hkeySubKey)
  152. {
  153. if (ERROR_SUCCESS == RegSetValueEx(hkeySubKey, pszValueName, 0,
  154. dwType, pb, cb))
  155. {
  156. fRet = TRUE;
  157. }
  158. _CloseRegSubKey(hkeySubKey);
  159. }
  160. _LeaveCSKeyRoot();
  161. return fRet;
  162. }
  163. BOOL CRegSupport::_GetGeneric(LPCTSTR pszSubKey, LPCTSTR pszValueName,
  164. PBYTE pb, DWORD* pcb)
  165. {
  166. BOOL fRet = FALSE;
  167. HKEY hkeySubKey = NULL;
  168. _EnterCSKeyRoot();
  169. if (pszSubKey && *pszSubKey)
  170. hkeySubKey = _GetSubKey(pszSubKey, FALSE);
  171. else
  172. hkeySubKey = _GetRootKey(FALSE);
  173. if (hkeySubKey)
  174. {
  175. if (ERROR_SUCCESS == SHQueryValueEx(hkeySubKey, pszValueName, 0,
  176. NULL, pb, pcb))
  177. {
  178. fRet = TRUE;
  179. }
  180. _CloseRegSubKey(hkeySubKey);
  181. }
  182. _LeaveCSKeyRoot();
  183. return fRet;
  184. }
  185. HKEY CRegSupport::RSDuplicateRootKey()
  186. {
  187. RIP(_fInited);
  188. #ifdef DEBUG
  189. // we need to decrement here since it will be icnremented inside this fct
  190. // and the key will not be close by this object
  191. --_cRefHKEY;
  192. ++_cRefExternalHKEY;
  193. #endif
  194. TCHAR szRoot[MAX_ROOT];
  195. return _RegCreateKeyExHelper(_hkeyInit, _GetRoot(szRoot, ARRAYSIZE(szRoot)), _dwRootOptions);
  196. }
  197. HKEY CRegSupport::RSDuplicateSubKey(LPCTSTR pszSubKey, BOOL fCreate, BOOL fVolatile)
  198. {
  199. return _GetSubKey(pszSubKey, fCreate, fVolatile ? REG_OPTION_VOLATILE : REG_OPTION_NON_VOLATILE);
  200. }
  201. HKEY CRegSupport::_GetRootKey(BOOL fCreate, DWORD dwOptions)
  202. {
  203. RIP(_fInited);
  204. HKEY hkey;
  205. TCHAR szRoot[MAX_ROOT];
  206. if (REG_OPTION_INVALID == dwOptions)
  207. dwOptions = _dwRootOptions;
  208. if (fCreate)
  209. hkey = _RegCreateKeyExHelper(_hkeyInit, _GetRoot(szRoot, ARRAYSIZE(szRoot)), dwOptions);
  210. else
  211. hkey = _RegOpenKeyExHelper(_hkeyInit, _GetRoot(szRoot, ARRAYSIZE(szRoot)));
  212. return hkey;
  213. }
  214. void CRegSupport::_CloseRegSubKey(HKEY hkeySubKey)
  215. {
  216. RegCloseKey(hkeySubKey);
  217. #ifdef DEBUG
  218. --_cRefHKEY;
  219. #endif
  220. }
  221. // Always need to be called from within the _csRootKey critical section (when critical section
  222. // stuff is enabled)
  223. HKEY CRegSupport::_GetSubKey(LPCTSTR pszSubKey, BOOL fCreate, DWORD dwOptions)
  224. {
  225. HKEY hkey = NULL;
  226. HKEY hRootKey = _GetRootKey(fCreate, dwOptions);
  227. if (REG_OPTION_INVALID == dwOptions)
  228. dwOptions = _dwDefaultOptions;
  229. if (hRootKey)
  230. {
  231. if (fCreate)
  232. hkey = _RegCreateKeyExHelper(hRootKey, pszSubKey, dwOptions);
  233. else
  234. hkey = _RegOpenKeyExHelper(hRootKey, pszSubKey);
  235. _CloseRegSubKey(hRootKey);
  236. }
  237. return hkey;
  238. }
  239. //static
  240. HKEY CRegSupport::_RegCreateKeyExHelper(HKEY hkey, LPCTSTR pszSubKey, DWORD dwOptions)
  241. {
  242. HKEY hkeyTmp;
  243. DWORD dwDisp;
  244. ASSERT(REG_OPTION_INVALID != dwOptions);
  245. if (ERROR_SUCCESS != RegCreateKeyEx(hkey, pszSubKey, 0, NULL,
  246. dwOptions, MAXIMUM_ALLOWED, NULL, &hkeyTmp, &dwDisp))
  247. {
  248. hkeyTmp = NULL;
  249. }
  250. #ifdef DEBUG
  251. else
  252. {
  253. ++_cRefHKEY;
  254. }
  255. #endif
  256. return hkeyTmp;
  257. }
  258. //static
  259. HKEY CRegSupport::_RegOpenKeyExHelper(HKEY hkey, LPCTSTR pszSubKey)
  260. {
  261. HKEY hkeyTmp;
  262. if (ERROR_SUCCESS != RegOpenKeyEx(hkey, pszSubKey, 0,
  263. MAXIMUM_ALLOWED, &hkeyTmp))
  264. {
  265. hkeyTmp = NULL;
  266. }
  267. #ifdef DEBUG
  268. else
  269. {
  270. ++_cRefHKEY;
  271. }
  272. #endif
  273. return hkeyTmp;
  274. }
  275. BOOL CRegSupport::_InitSetRoot(LPCTSTR pszSubKey1, LPCTSTR pszSubKey2)
  276. {
  277. _pszSubKey1 = pszSubKey1;
  278. _pszSubKey2 = pszSubKey2;
  279. return TRUE;
  280. }
  281. LPCTSTR CRegSupport::_GetRoot(LPTSTR pszRoot, DWORD cchRoot)
  282. {
  283. ASSERT(cchRoot > 0);
  284. LPTSTR pszNext;
  285. size_t cchLeft;
  286. HRESULT hr;
  287. hr = StringCchCopyEx(pszRoot, cchRoot, _pszSubKey1, &pszNext, &cchLeft, 0);
  288. if (SUCCEEDED(hr))
  289. {
  290. if (_pszSubKey2)
  291. {
  292. hr = StringCchCopyEx(pszNext, cchLeft, TEXT("\\"), &pszNext, &cchLeft, 0);
  293. if (SUCCEEDED(hr))
  294. {
  295. hr = StringCchCopy(pszNext, cchLeft, _pszSubKey2);
  296. }
  297. }
  298. }
  299. if (FAILED(hr))
  300. {
  301. *pszRoot = 0;
  302. }
  303. return pszRoot;
  304. }
  305. void CRegSupport::_EnterCSKeyRoot()
  306. {
  307. if (_fcsKeyRoot)
  308. {
  309. EnterCriticalSection(&_csKeyRoot);
  310. }
  311. }
  312. void CRegSupport::_LeaveCSKeyRoot()
  313. {
  314. if (_fcsKeyRoot)
  315. {
  316. LeaveCriticalSection(&_csKeyRoot);
  317. }
  318. }
  319. CRegSupport::CRegSupport() : _hkeyInit(NULL)
  320. {}
  321. CRegSupport::~CRegSupport()
  322. {
  323. if (_fcsKeyRoot)
  324. {
  325. DeleteCriticalSection(&_csKeyRoot);
  326. }
  327. }