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.

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