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.

382 lines
9.2 KiB

  1. //////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // RegistryKey.cpp
  4. //
  5. // Copyright (C) 2000 Microsoft Corporation. All rights reserved.
  6. //
  7. // Abstract :
  8. //
  9. // This is the implementation of the CRegistryKey class.
  10. //
  11. // History :
  12. //
  13. // 05/05/2000 luish Created
  14. //
  15. //////////////////////////////////////////////////////////////////////////////////////////////
  16. #include <windows.h>
  17. #include "RegistryKey.h"
  18. #include "Global.h"
  19. //////////////////////////////////////////////////////////////////////////////////////////////
  20. //
  21. //////////////////////////////////////////////////////////////////////////////////////////////
  22. CRegistryKey::CRegistryKey(void)
  23. {
  24. m_fKeyOpen = FALSE;
  25. }
  26. //////////////////////////////////////////////////////////////////////////////////////////////
  27. //
  28. //////////////////////////////////////////////////////////////////////////////////////////////
  29. CRegistryKey::~CRegistryKey(void)
  30. {
  31. CloseKey();
  32. }
  33. //////////////////////////////////////////////////////////////////////////////////////////////
  34. //
  35. //////////////////////////////////////////////////////////////////////////////////////////////
  36. STDMETHODIMP CRegistryKey::EnumKeys(const DWORD dwIndex, LPTSTR strKeyName, LPDWORD lpdwKeyNameLen)
  37. {
  38. FILETIME sFileTime;
  39. HRESULT hResult = S_OK;
  40. //
  41. // If there is no currently opened registry key, then GetValue was called unexpectedly
  42. //
  43. if (m_fKeyOpen)
  44. {
  45. if (ERROR_SUCCESS != RegEnumKeyEx(m_hRegistryKey, dwIndex, strKeyName, lpdwKeyNameLen, NULL, NULL, NULL, &sFileTime))
  46. {
  47. hResult = S_FALSE;
  48. }
  49. }
  50. else
  51. {
  52. hResult = E_FAIL;
  53. }
  54. return hResult;
  55. }
  56. //////////////////////////////////////////////////////////////////////////////////////////////
  57. //
  58. //////////////////////////////////////////////////////////////////////////////////////////////
  59. STDMETHODIMP CRegistryKey::CheckForExistingKey(HKEY hKey, LPCTSTR strKeyName)
  60. {
  61. HKEY hRegistryKey;
  62. HRESULT hResult = S_OK;
  63. if (ERROR_SUCCESS != RegOpenKeyEx(hKey, strKeyName, 0, KEY_READ, &hRegistryKey))
  64. {
  65. hResult = S_FALSE;
  66. }
  67. else
  68. {
  69. RegCloseKey(hRegistryKey);
  70. }
  71. return hResult;
  72. }
  73. //////////////////////////////////////////////////////////////////////////////////////////////
  74. //
  75. //////////////////////////////////////////////////////////////////////////////////////////////
  76. STDMETHODIMP CRegistryKey::CreateKey(HKEY hKey, LPCTSTR strKeyName, const DWORD dwOptions, const REGSAM samDesired, LPDWORD lpdwDisposition)
  77. {
  78. HRESULT hResult = S_OK;
  79. SECURITY_ATTRIBUTES sSecurityAttributes = {0};
  80. SECURITY_DESCRIPTOR sSecurityDescriptor = {0};
  81. //
  82. // Make sure that current registry is closed before proceeding
  83. //
  84. CloseKey();
  85. //
  86. // Set the security attributes
  87. //
  88. sSecurityAttributes.nLength = sizeof sSecurityAttributes;
  89. sSecurityAttributes.lpSecurityDescriptor = &sSecurityDescriptor;
  90. InitializeSecurityDescriptor(&sSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
  91. SetSecurityDescriptorDacl(&sSecurityDescriptor, TRUE, NULL, FALSE);
  92. //
  93. // Create the new key
  94. //
  95. if (ERROR_SUCCESS != RegCreateKeyEx(hKey, strKeyName, 0, NULL, dwOptions, samDesired, &sSecurityAttributes, &m_hRegistryKey, lpdwDisposition))
  96. {
  97. hResult = E_FAIL;
  98. }
  99. else
  100. {
  101. m_fKeyOpen = TRUE;
  102. }
  103. return hResult;
  104. }
  105. //////////////////////////////////////////////////////////////////////////////////////////////
  106. //
  107. //////////////////////////////////////////////////////////////////////////////////////////////
  108. STDMETHODIMP CRegistryKey::OpenKey(HKEY hKey, LPCTSTR strKeyName, const REGSAM samDesired)
  109. {
  110. HRESULT hResult = S_OK;
  111. //
  112. // Make sure that current registry is closed before proceeding
  113. //
  114. CloseKey();
  115. //
  116. // Open the existing key
  117. //
  118. if (ERROR_SUCCESS != RegOpenKeyEx(hKey, strKeyName, 0, samDesired, &m_hRegistryKey))
  119. {
  120. hResult = E_FAIL;
  121. }
  122. else
  123. {
  124. m_fKeyOpen = TRUE;
  125. }
  126. return hResult;
  127. }
  128. //////////////////////////////////////////////////////////////////////////////////////////////
  129. //
  130. //////////////////////////////////////////////////////////////////////////////////////////////
  131. STDMETHODIMP CRegistryKey::CloseKey(void)
  132. {
  133. HRESULT hResult = E_FAIL;
  134. if (m_fKeyOpen)
  135. {
  136. if (ERROR_SUCCESS == RegCloseKey(m_hRegistryKey))
  137. {
  138. m_fKeyOpen = FALSE;
  139. hResult = S_OK;
  140. }
  141. }
  142. return hResult;
  143. }
  144. //////////////////////////////////////////////////////////////////////////////////////////////
  145. //
  146. //////////////////////////////////////////////////////////////////////////////////////////////
  147. STDMETHODIMP CRegistryKey::DeleteKey(HKEY hKey, LPCTSTR strKeyName)
  148. {
  149. HRESULT hResult = S_OK;
  150. DWORD dwSubKeyNameLen;
  151. HKEY hRegistryKey;
  152. FILETIME sFileTime;
  153. TCHAR strSubKeyName[MAX_PATH];
  154. TCHAR strFullSubKeyName[MAX_PATH];
  155. //
  156. // Recursively delete all subkeys
  157. //
  158. if (ERROR_SUCCESS == RegOpenKeyEx(hKey, strKeyName, 0, KEY_ALL_ACCESS, &hRegistryKey))
  159. {
  160. dwSubKeyNameLen = MAX_PATH;
  161. while ((S_OK == hResult)&&(ERROR_SUCCESS == RegEnumKeyEx(hRegistryKey, 0, strSubKeyName, &dwSubKeyNameLen, NULL, NULL, NULL, &sFileTime)))
  162. {
  163. wsprintf(strFullSubKeyName, TEXT("%s\\%s"), strKeyName, strSubKeyName);
  164. hResult = DeleteKey(hKey, strFullSubKeyName);
  165. dwSubKeyNameLen = MAX_PATH;
  166. }
  167. //
  168. // Close the key
  169. //
  170. RegCloseKey(hRegistryKey);
  171. //
  172. // If all the subkeys were successfully deleted
  173. //
  174. if (S_OK == hResult)
  175. {
  176. //
  177. // Delete this subkey
  178. //
  179. if (ERROR_SUCCESS != RegDeleteKey(hKey, strKeyName))
  180. {
  181. hResult = E_FAIL;
  182. }
  183. }
  184. }
  185. return hResult;
  186. }
  187. //////////////////////////////////////////////////////////////////////////////////////////////
  188. //
  189. //////////////////////////////////////////////////////////////////////////////////////////////
  190. STDMETHODIMP CRegistryKey::EnumValues(const DWORD dwIndex, LPTSTR strValueName, LPDWORD lpdwValueNameLen, LPDWORD lpdwType, LPBYTE lpData, LPDWORD lpdwDataLen)
  191. {
  192. HRESULT hResult = S_OK;
  193. //
  194. // If there is no currently opened registry key, then GetValue was called unexpectedly
  195. //
  196. if (m_fKeyOpen)
  197. {
  198. if (ERROR_SUCCESS != RegEnumValue(m_hRegistryKey, dwIndex, strValueName, lpdwValueNameLen, NULL, lpdwType, lpData, lpdwDataLen))
  199. {
  200. hResult = S_FALSE;
  201. }
  202. }
  203. else
  204. {
  205. hResult = E_FAIL;
  206. }
  207. return hResult;
  208. }
  209. //////////////////////////////////////////////////////////////////////////////////////////////
  210. //
  211. //////////////////////////////////////////////////////////////////////////////////////////////
  212. STDMETHODIMP CRegistryKey::CheckForExistingValue(LPCTSTR strValueName)
  213. {
  214. HRESULT hResult = E_FAIL;
  215. //
  216. // If there is no currently opened registry key, then CheckForExistingValue was
  217. // called unexpectedly
  218. //
  219. if (m_fKeyOpen)
  220. {
  221. //
  222. // Check to see whether we can RegQueryValueEx() on the target. If we can then the value
  223. // exists, othersize it does not
  224. //
  225. if (ERROR_SUCCESS != RegQueryValueEx(m_hRegistryKey, strValueName, NULL, NULL, NULL, NULL))
  226. {
  227. hResult = S_FALSE;
  228. }
  229. else
  230. {
  231. hResult = S_OK;
  232. }
  233. }
  234. return hResult;
  235. }
  236. //////////////////////////////////////////////////////////////////////////////////////////////
  237. //
  238. //////////////////////////////////////////////////////////////////////////////////////////////
  239. STDMETHODIMP CRegistryKey::GetValue(LPCTSTR strValueName, LPDWORD lpdwType, LPBYTE lpData, LPDWORD lpdwDataLen)
  240. {
  241. HRESULT hResult = E_FAIL;
  242. //
  243. // If there is no currently opened registry key, then GetValue was called unexpectedly
  244. //
  245. if (m_fKeyOpen)
  246. {
  247. if (ERROR_SUCCESS == RegQueryValueEx(m_hRegistryKey, strValueName, NULL, lpdwType, lpData, lpdwDataLen))
  248. {
  249. hResult = S_OK;
  250. }
  251. }
  252. return hResult;
  253. }
  254. //////////////////////////////////////////////////////////////////////////////////////////////
  255. //
  256. //////////////////////////////////////////////////////////////////////////////////////////////
  257. STDMETHODIMP CRegistryKey::SetValue(LPCTSTR strValueName, const DWORD dwType, const BYTE * lpData, const DWORD dwDataLen)
  258. {
  259. HRESULT hResult = E_FAIL;
  260. DWORD dwActualDataLen;
  261. //
  262. // If there is no currently opened registry key, then GetValue was called unexpectedly
  263. //
  264. if (m_fKeyOpen)
  265. {
  266. //
  267. // If this value is of type REG_SZ, define the lenght of the string
  268. //
  269. if (REG_SZ == dwType)
  270. {
  271. #ifdef _UNICODE
  272. dwActualDataLen = ((StrLen((LPCTSTR) lpData) + 1) * 2);
  273. #else
  274. dwActualDataLen = (StrLen((LPCTSTR) lpData) + 1);
  275. #endif // _UNICODE
  276. }
  277. else
  278. {
  279. dwActualDataLen = dwDataLen;
  280. }
  281. //
  282. // Set the registry value
  283. //
  284. if (ERROR_SUCCESS == RegSetValueEx(m_hRegistryKey, strValueName, 0, dwType, lpData, dwActualDataLen))
  285. {
  286. hResult = S_OK;
  287. }
  288. }
  289. return hResult;
  290. }
  291. //////////////////////////////////////////////////////////////////////////////////////////////
  292. //
  293. //////////////////////////////////////////////////////////////////////////////////////////////
  294. STDMETHODIMP CRegistryKey::DeleteValue(LPCTSTR strValueName)
  295. {
  296. HRESULT hResult = E_FAIL;
  297. //
  298. // If there is no currently opened registry key, then GetValue was called unexpectedly
  299. //
  300. if (m_fKeyOpen)
  301. {
  302. if (ERROR_SUCCESS == RegDeleteValue(m_hRegistryKey, strValueName))
  303. {
  304. hResult = S_OK;
  305. }
  306. }
  307. return hResult;
  308. }