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.

213 lines
4.1 KiB

  1. // Registry.cpp: implementation of the CRegistry class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "precomp.h"
  5. #include "Registry.h"
  6. #include "Shlwapi.h"
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10. CRegistry::CRegistry()
  11. {
  12. }
  13. CRegistry::~CRegistry()
  14. {
  15. }
  16. bool CRegistry::KeyExists(CRegDataItemPtr pItem)
  17. {
  18. HKEY temp;
  19. if (OpenKey(pItem, &temp))
  20. {
  21. RegCloseKey(temp);
  22. return true;
  23. }
  24. else
  25. return false;
  26. }
  27. bool CRegistry::AddKey(CRegDataItemPtr pItem)
  28. {
  29. CStr root, subkey;
  30. pItem->m_KeyName.SplitString(root, subkey, L'\\');
  31. //don't need to worry about creating multiple nested subkeys,
  32. //since RegCreateKeyEx handles this for us.
  33. HKEY rootKey, result;
  34. if (DecodeRootKeyStr(root, &rootKey))
  35. {
  36. if (ERROR_SUCCESS ==
  37. RegCreateKeyEx(rootKey, subkey, 0, 0, REG_OPTION_NON_VOLATILE,0,0,&result,0))
  38. {
  39. RegCloseKey(result);
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45. ValueExistsCode CRegistry::ValueExists(CRegDataItemPtr pItem)
  46. {
  47. CRegDataItemPtr temp = GetValue(pItem);
  48. if (!temp.IsNull()) //value does exist
  49. {
  50. if ((temp->m_Type == pItem->m_Type)
  51. && (temp->m_DataLen == pItem->m_DataLen)
  52. && (memcmp(temp->m_pDataBuf, pItem->m_pDataBuf, temp->m_DataLen) == 0))
  53. {
  54. //value has the same data
  55. return VALUE_EXISTS_SAME_DATA;
  56. }
  57. //value exists, but with different data
  58. return VALUE_EXISTS_DIFF_DATA;
  59. }
  60. //value doesn't exist
  61. return VALUE_DOESNT_EXIST;
  62. }
  63. bool CRegistry::AddValue(CRegDataItemPtr pItem)
  64. {
  65. HKEY temp = 0;
  66. bool result = false;
  67. if (OpenKey(pItem, &temp))
  68. {
  69. if (RegSetValueEx(temp, pItem->m_Name,0,pItem->m_Type,pItem->m_pDataBuf,pItem->m_DataLen)
  70. == ERROR_SUCCESS)
  71. result = true;
  72. RegCloseKey(temp);
  73. }
  74. return result;
  75. }
  76. bool CRegistry::SaveKey(CRegDataItemPtr pItem, CRegDiffFile &file, SectionType section)
  77. {
  78. bool result = false;
  79. HKEY temp;
  80. if (OpenKey(pItem, &temp))
  81. {
  82. m_RegAnalyzer.SaveKeyToFile(temp, pItem->m_KeyName, (CRegFile*)&file, section);
  83. return true;
  84. }
  85. return false;
  86. }
  87. bool CRegistry::DeleteKey(CRegDataItemPtr pItem)
  88. {
  89. //recursively delete the key
  90. CStr RootKeyName;
  91. CStr SubKeyName;
  92. pItem->m_KeyName.SplitString(RootKeyName, SubKeyName, TEXT('\\'));
  93. HKEY rootKey;
  94. DecodeRootKeyStr(RootKeyName, &rootKey);
  95. if (ERROR_SUCCESS == SHDeleteKey(rootKey,SubKeyName))
  96. {
  97. return true;
  98. }
  99. return false;
  100. }
  101. bool CRegistry::DeleteValue(CRegDataItemPtr pItem)
  102. {
  103. HKEY temp = 0;
  104. bool result = false;
  105. if (OpenKey(pItem, &temp))
  106. {
  107. if (RegDeleteValue(temp, pItem->m_Name)
  108. == ERROR_SUCCESS)
  109. result = true;
  110. RegCloseKey(temp);
  111. }
  112. return result;
  113. }
  114. CRegDataItemPtr CRegistry::GetValue(CRegDataItemPtr pItem)
  115. {
  116. HKEY temp = 0;
  117. if (OpenKey(pItem, &temp))
  118. {
  119. DWORD nameLen, valueLen;
  120. CRegDataItemPtr result(new CRegDataItem);
  121. if (RegQueryInfoKey(temp,0,0,0,0,0,0,0,&nameLen,&valueLen,0,0) == ERROR_SUCCESS)
  122. {
  123. result->m_KeyName = pItem->m_KeyName;
  124. result->m_Name = pItem->m_Name;
  125. result->m_NameLen = pItem->m_NameLen;
  126. BYTE* pValue = new BYTE[valueLen];
  127. TCHAR* pName = new TCHAR[nameLen];
  128. if (RegQueryValueEx(temp, result->m_Name,0,&result->m_Type,pValue,&valueLen)
  129. == ERROR_SUCCESS)
  130. {
  131. result->m_pDataBuf = pValue;
  132. result->m_DataLen = valueLen;
  133. result->m_bIsEmpty = false;
  134. RegCloseKey(temp);
  135. return result;
  136. }
  137. }
  138. RegCloseKey(temp);
  139. }
  140. return CRegDataItemPtr();
  141. }
  142. bool CRegistry::OpenKey(CRegDataItemPtr pItem, PHKEY pKey)
  143. {
  144. CStr RootKeyName;
  145. CStr SubKeyName;
  146. pItem->m_KeyName.SplitString(RootKeyName, SubKeyName, TEXT('\\'));
  147. HKEY rootKey;
  148. DecodeRootKeyStr(RootKeyName, &rootKey);
  149. if (ERROR_SUCCESS == RegOpenKeyEx(rootKey, SubKeyName, 0, KEY_ALL_ACCESS, pKey))
  150. {
  151. return true;
  152. }
  153. else
  154. {
  155. return false;
  156. }
  157. }
  158. bool CRegistry::DecodeRootKeyStr(LPCTSTR RootKeyName, PHKEY pRootKey)
  159. {
  160. *pRootKey = GetRootKey (RootKeyName);
  161. return *pRootKey != 0;
  162. }