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.

262 lines
6.9 KiB

  1. //@doc
  2. /******************************************************
  3. **
  4. ** @module REGISTRY.CPP | Implementation of Registry class
  5. **
  6. ** Description:
  7. **
  8. ** History:
  9. ** Created 12/16/97 Matthew L. Coill (mlc)
  10. **
  11. ** (c) 1986-1997 Microsoft Corporation. All Rights Reserved.
  12. ******************************************************/
  13. #include "Registry.h"
  14. #include <TCHAR.h>
  15. UnassignableRegistryKey c_InvalidKey(NULL);
  16. /***************** RegistryKey class ********************/
  17. /******************************************************
  18. **
  19. ** RegistryKey::RegistryKey(RegistryKey& rkey)
  20. **
  21. ** @mfunc Constructor.
  22. **
  23. ******************************************************/
  24. RegistryKey::RegistryKey(RegistryKey& rkey)
  25. {
  26. if (rkey.m_pReferenceCount == NULL) {
  27. rkey.m_pReferenceCount = new UINT;
  28. }
  29. if (rkey.m_pReferenceCount != NULL) {
  30. *(rkey.m_pReferenceCount) = 1;
  31. m_pReferenceCount = rkey.m_pReferenceCount;
  32. ++(*m_pReferenceCount);
  33. }
  34. m_OSRegistryKey = rkey.m_OSRegistryKey;
  35. m_ShouldClose = rkey.m_ShouldClose;
  36. }
  37. /******************************************************
  38. **
  39. ** RegistryKey::~RegistryKey()
  40. **
  41. ** @mfunc Destructor.
  42. **
  43. ******************************************************/
  44. RegistryKey::~RegistryKey()
  45. {
  46. if (m_pReferenceCount != NULL) {
  47. if (--(*m_pReferenceCount) == 0) {
  48. delete m_pReferenceCount;
  49. m_pReferenceCount = NULL;
  50. }
  51. }
  52. if ((m_OSRegistryKey != NULL) && (m_ShouldClose) && (m_pReferenceCount == NULL)) {
  53. ::RegCloseKey(m_OSRegistryKey);
  54. }
  55. m_OSRegistryKey = NULL;
  56. m_pReferenceCount = NULL;
  57. }
  58. /******************************************************
  59. **
  60. ** RegistryKey::operator=(RegistryKey& rhs)
  61. **
  62. ** @mfunc operator=.
  63. **
  64. ******************************************************/
  65. RegistryKey& RegistryKey::operator=(RegistryKey& rhs)
  66. {
  67. if (&rhs == this) {
  68. return *this;
  69. }
  70. if (rhs.m_pReferenceCount == NULL) {
  71. rhs.m_pReferenceCount = new UINT;
  72. }
  73. if (m_pReferenceCount != NULL) {
  74. if (--(*m_pReferenceCount) == 0) {
  75. delete m_pReferenceCount;
  76. m_pReferenceCount = NULL;
  77. }
  78. }
  79. if ((m_OSRegistryKey != NULL) && (m_ShouldClose) && (m_pReferenceCount == NULL)) {
  80. ::RegCloseKey(m_OSRegistryKey);
  81. }
  82. m_OSRegistryKey = rhs.m_OSRegistryKey;
  83. m_ShouldClose = rhs.m_ShouldClose;
  84. if (rhs.m_pReferenceCount != NULL)
  85. {
  86. *(rhs.m_pReferenceCount) = 1;
  87. m_pReferenceCount = rhs.m_pReferenceCount;
  88. ++(*m_pReferenceCount);
  89. }
  90. return *this;
  91. }
  92. /******************************************************
  93. **
  94. ** RegistryKey::operator==(RegistryKey& comparee)
  95. **
  96. ** @mfunc operator==.
  97. **
  98. ******************************************************/
  99. BOOL RegistryKey::operator==(const RegistryKey& comparee)
  100. {
  101. return (comparee.m_OSRegistryKey == m_OSRegistryKey);
  102. }
  103. /******************************************************
  104. **
  105. ** RegistryKey::operator!=(RegistryKey& comparee)
  106. **
  107. ** @mfunc operator!=.
  108. **
  109. ******************************************************/
  110. BOOL RegistryKey::operator!=(const RegistryKey& comparee)
  111. {
  112. return (comparee.m_OSRegistryKey != m_OSRegistryKey);
  113. }
  114. /******************************************************
  115. **
  116. ** RegistryKey::CreateSubKey()
  117. **
  118. ** @mfunc CreateSubKey.
  119. **
  120. ******************************************************/
  121. RegistryKey RegistryKey::CreateSubkey(const TCHAR* subkeyName, const TCHAR* typeName)
  122. {
  123. if ((m_OSRegistryKey == NULL) || (subkeyName == NULL) || (subkeyName[0] == '\0') || (subkeyName[0] == '\\')) {
  124. return c_InvalidKey;
  125. }
  126. HKEY newKey = NULL;
  127. DWORD creationInfo;
  128. HRESULT hr = ::RegCreateKeyEx(m_OSRegistryKey, subkeyName, 0, (TCHAR*)typeName, REG_OPTION_NON_VOLATILE, /*KEY_READ*/ ((KEY_ALL_ACCESS & ~WRITE_DAC) & ~WRITE_OWNER), NULL, &newKey, &creationInfo);
  129. if (newKey == NULL) {
  130. TCHAR msg[512];
  131. ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 0, msg, 512, NULL);
  132. return c_InvalidKey;
  133. }
  134. RegistryKey newRegistryKey(newKey);
  135. newRegistryKey.m_ShouldClose = TRUE;
  136. return newRegistryKey;
  137. }
  138. /******************************************************
  139. **
  140. ** RegistryKey::OpenSubkey()
  141. **
  142. ** @mfunc OpenSubkey.
  143. **
  144. ******************************************************/
  145. RegistryKey RegistryKey::OpenSubkey(const TCHAR* subkeyName, REGSAM access)
  146. {
  147. if ((m_OSRegistryKey == NULL) || (subkeyName == NULL) || (subkeyName[0] == '\0') || (subkeyName[0] == '\\')) {
  148. return c_InvalidKey;
  149. }
  150. HKEY newKey = NULL;
  151. HRESULT hr = ::RegOpenKeyEx(m_OSRegistryKey, subkeyName, 0, access, &newKey);
  152. if (newKey == NULL) {
  153. return c_InvalidKey;
  154. }
  155. RegistryKey newRegistryKey(newKey);
  156. newRegistryKey.m_ShouldClose = TRUE;
  157. return newRegistryKey;
  158. }
  159. /******************************************************
  160. **
  161. ** RegistryKey::OpenCreateSubkey()
  162. **
  163. ** @mfunc OpenCreateSubkey.
  164. **
  165. ******************************************************/
  166. RegistryKey RegistryKey::OpenCreateSubkey(const TCHAR* subkeyName)
  167. {
  168. RegistryKey key = OpenSubkey(subkeyName, KEY_READ | KEY_WRITE);
  169. if (key == c_InvalidKey) {
  170. key = CreateSubkey(subkeyName);
  171. }
  172. return key;
  173. }
  174. /******************************************************
  175. **
  176. ** RegistryKey::RemoveSubkey()
  177. **
  178. ** @mfunc RemoveSubkey.
  179. **
  180. ******************************************************/
  181. HRESULT RegistryKey::RemoveSubkey(const TCHAR* subkeyName)
  182. {
  183. if ((m_OSRegistryKey == NULL) || (subkeyName == NULL) || (subkeyName[0] == '\0') || (subkeyName[0] == '\\')) {
  184. return E_FAIL;
  185. }
  186. return ::RegDeleteKey(m_OSRegistryKey, subkeyName);
  187. }
  188. /******************************************************
  189. **
  190. ** RegistryKey::GetNumSubKeys()
  191. **
  192. ** @mfunc RemoveSubkey.
  193. **
  194. ******************************************************/
  195. DWORD RegistryKey::GetNumSubkeys() const
  196. {
  197. if (m_OSRegistryKey == NULL) {
  198. return 0;
  199. }
  200. DWORD numSubKeys = 0;
  201. ::RegQueryInfoKey(m_OSRegistryKey, NULL, NULL, NULL, &numSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  202. return numSubKeys;
  203. }
  204. /******************************************************
  205. **
  206. ** RegistryKey::QueryValue(const TCHAR* valueName, (BYTE*)& pEntryData, UINT& dataSize)
  207. **
  208. ** @mfunc QueryValue(const.
  209. **
  210. ******************************************************/
  211. HRESULT RegistryKey::QueryValue(const TCHAR* valueName, BYTE* pEntryData, DWORD& dataSize)
  212. {
  213. if ((m_OSRegistryKey == NULL) || (pEntryData == NULL)) {
  214. return E_FAIL;
  215. }
  216. DWORD dataType;
  217. HRESULT hr = ::RegQueryValueEx(m_OSRegistryKey, valueName, NULL, &dataType, pEntryData, &dataSize);
  218. return hr;
  219. }
  220. /******************************************************
  221. **
  222. ** RegistryKey::SetValue(const TCHAR* valueName, const BYTE* pData, DWORD dataSize, DWORD dataType)
  223. **
  224. ** @mfunc SetValue.
  225. **
  226. ******************************************************/
  227. HRESULT RegistryKey::SetValue(const TCHAR* valueName, const BYTE* pData, DWORD dataSize, DWORD dataType)
  228. {
  229. if (m_OSRegistryKey == NULL) {
  230. return E_FAIL;
  231. }
  232. HRESULT hr = ::RegSetValueEx(m_OSRegistryKey, valueName, 0, dataType, pData, dataSize);
  233. return hr;
  234. }