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.

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