regsuprt.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 9k
Category:

Windows Kernel

Development Platform:

Visual C++

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