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.

173 lines
4.2 KiB

  1. #include "shellprv.h"
  2. #pragma hdrstop
  3. #include "rgsprtc.h"
  4. BOOL CRegSupportCached::_fUseCaching = FALSE;
  5. CRegSupportCached::CRegSupportCached()
  6. {
  7. _InitCSKeyRoot();
  8. }
  9. CRegSupportCached::~CRegSupportCached()
  10. {
  11. // need to close cached key
  12. _CloseCachedRootKey();
  13. }
  14. void CRegSupportCached::_CloseRegSubKey(HKEY hkey)
  15. {
  16. if (!_fUseCaching)
  17. {
  18. CRegSupport::_CloseRegSubKey(hkey);
  19. if (_hkeyRoot == hkey)
  20. {
  21. _hkeyRoot = NULL;
  22. }
  23. }
  24. else
  25. {
  26. if (_hkeyRoot != hkey)
  27. {
  28. CRegSupport::_CloseRegSubKey(hkey);
  29. }
  30. }
  31. }
  32. HKEY CRegSupportCached::_GetRootKey(BOOL fCreate, DWORD dwOptions)
  33. {
  34. if (!_hkeyRoot)
  35. {
  36. _hkeyRoot = CRegSupport::_GetRootKey(fCreate, dwOptions);
  37. }
  38. return _hkeyRoot;
  39. }
  40. BOOL CRegSupportCached::_SetGeneric(LPCTSTR pszSubKey, LPCTSTR pszValueName,
  41. PBYTE pb, DWORD cb, DWORD dwType,
  42. DWORD dwOptions)
  43. {
  44. int cTry = 0;
  45. BOOL fRet = FALSE;
  46. do
  47. {
  48. ++cTry;
  49. fRet = CRegSupport::_SetGeneric(pszSubKey, pszValueName,
  50. pb, cb, dwType, dwOptions);
  51. // Did we fail?
  52. if (!fRet)
  53. {
  54. // Yes, maybe some other process deleted the key, so we'll close
  55. // the cached one and try to reopen it, and retry the operation
  56. // (for a maximum of two times)
  57. _CloseCachedRootKey();
  58. }
  59. }
  60. while (!fRet && (cTry < 2));
  61. return fRet;
  62. }
  63. BOOL CRegSupportCached::_GetGeneric(LPCTSTR pszSubKey, LPCTSTR pszValueName,
  64. PBYTE pb, DWORD* pcb)
  65. {
  66. int cTry = 0;
  67. BOOL fRet = FALSE;
  68. DWORD cbLocal;
  69. do
  70. {
  71. cbLocal = *pcb;
  72. ++cTry;
  73. fRet = CRegSupport::_GetGeneric(pszSubKey, pszValueName,
  74. pb, &cbLocal);
  75. // Did we fail?
  76. if (!fRet)
  77. {
  78. // Yes, maybe some other process deleted the key, so we'll close
  79. // the cached one and try to reopen it, and retry the operation
  80. // (for a maximum of two times)
  81. _CloseCachedRootKey();
  82. }
  83. }
  84. while (!fRet && (cTry < 2));
  85. *pcb = cbLocal;
  86. return fRet;
  87. }
  88. ///////////////////////////////////////////////////////////////////////////////
  89. // Reset the cache in the next fcts, to increase our chances of returning the
  90. // true answer, this only increase the chances, there is still na opportunity
  91. // to miss. Does not really matter for these fcts.
  92. ///////////////////////////////////////////////////////////////////////////////
  93. BOOL CRegSupportCached::RSValueExist(LPCTSTR pszSubKey, LPCTSTR pszValueName)
  94. {
  95. // Close the cache one, or else we might get wrong results
  96. _CloseCachedRootKey();
  97. return CRegSupport::RSValueExist(pszSubKey, pszValueName);
  98. }
  99. BOOL CRegSupportCached::RSSubKeyExist(LPCTSTR pszSubKey)
  100. {
  101. // Close the cache one, or else we might get wrong results
  102. _CloseCachedRootKey();
  103. return CRegSupport::RSSubKeyExist(pszSubKey);
  104. }
  105. HKEY CRegSupportCached::RSDuplicateRootKey()
  106. {
  107. // Close the cache one, or else we might get wrong results
  108. _CloseCachedRootKey();
  109. return CRegSupport::RSDuplicateRootKey();
  110. }
  111. BOOL CRegSupportCached::RSDeleteSubKey(LPCTSTR pszSubKey)
  112. {
  113. // Close the cache one, or else we might get wrong results
  114. _CloseCachedRootKey();
  115. return CRegSupport::RSDeleteSubKey(pszSubKey);
  116. }
  117. BOOL CRegSupportCached::RSDeleteValue(LPCTSTR pszSubKey, LPCTSTR pszValueName)
  118. {
  119. // Close the cache one, or else we might get wrong results
  120. _CloseCachedRootKey();
  121. return CRegSupport::RSDeleteValue(pszSubKey, pszValueName);
  122. }
  123. STDAPI_(void) CRegSupportCached_RSEnableKeyCaching(BOOL fEnable)
  124. {
  125. CRegSupportCached::_fUseCaching = fEnable;
  126. }
  127. ///////////////////////////////////////////////////////////////////////////////
  128. ///////////////////////////////////////////////////////////////////////////////
  129. ///////////////////////////////////////////////////////////////////////////////
  130. void CRegSupportCached::_CloseCachedRootKey()
  131. {
  132. _EnterCSKeyRoot();
  133. if (_hkeyRoot)
  134. {
  135. RegCloseKey(_hkeyRoot);
  136. _hkeyRoot = NULL;
  137. }
  138. _LeaveCSKeyRoot();
  139. }