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.

364 lines
9.2 KiB

  1. /*++
  2. Copyright (C) 1999-2001 Microsoft Corporation
  3. Module Name:
  4. NTREG.CPP
  5. Abstract:
  6. History:
  7. --*/
  8. #include "precomp.h"
  9. #include <stdio.h>
  10. #include <oaidl.h>
  11. #include "ntreg.h"
  12. #include "adaputil.h"
  13. CNTRegistry::CNTRegistry() : m_hPrimaryKey(0),
  14. m_hSubkey(0),
  15. m_nStatus(0),
  16. m_nLastError(no_error)
  17. {
  18. }
  19. CNTRegistry::~CNTRegistry()
  20. {
  21. if (m_hSubkey)
  22. RegCloseKey(m_hSubkey);
  23. if (m_hPrimaryKey != m_hSubkey)
  24. RegCloseKey(m_hPrimaryKey);
  25. }
  26. int CNTRegistry::Open(HKEY hStart, WCHAR *pszStartKey)
  27. {
  28. int nStatus = no_error;
  29. m_nLastError = RegOpenKeyExW(hStart, pszStartKey,
  30. 0, KEY_ALL_ACCESS, &m_hPrimaryKey );
  31. switch( m_nLastError )
  32. {
  33. case ERROR_SUCCESS:
  34. nStatus = no_error; break;
  35. case ERROR_ACCESS_DENIED:
  36. nStatus = access_denied; break;
  37. case ERROR_FILE_NOT_FOUND:
  38. nStatus = not_found; break;
  39. default:
  40. nStatus = failed; break;
  41. }
  42. m_hSubkey = m_hPrimaryKey;
  43. return nStatus;
  44. }
  45. int CNTRegistry::MoveToSubkey(WCHAR *pszNewSubkey)
  46. {
  47. int nStatus = no_error;
  48. m_nLastError = RegOpenKeyExW(m_hPrimaryKey, pszNewSubkey, 0, KEY_ALL_ACCESS, &m_hSubkey );
  49. switch( m_nLastError )
  50. {
  51. case ERROR_SUCCESS:
  52. nStatus = no_error; break;
  53. case ERROR_ACCESS_DENIED:
  54. nStatus = access_denied; break;
  55. default:
  56. nStatus = failed; break;
  57. }
  58. return nStatus;
  59. }
  60. int CNTRegistry::DeleteValue(WCHAR *pwszValueName)
  61. {
  62. int nStatus = no_error;
  63. m_nLastError = RegDeleteValueW(m_hSubkey, pwszValueName);
  64. switch( m_nLastError )
  65. {
  66. case ERROR_SUCCESS:
  67. nStatus = no_error; break;
  68. case ERROR_ACCESS_DENIED:
  69. nStatus = access_denied; break;
  70. case ERROR_FILE_NOT_FOUND:
  71. nStatus = not_found; break;
  72. default:
  73. nStatus = failed; break;
  74. }
  75. return nStatus;
  76. }
  77. int CNTRegistry::GetDWORD(WCHAR *pwszValueName, DWORD *pdwValue)
  78. {
  79. int nStatus = no_error;
  80. DWORD dwSize = sizeof(DWORD);
  81. DWORD dwType = 0;
  82. m_nLastError = RegQueryValueExW(m_hSubkey, pwszValueName, 0, &dwType,
  83. LPBYTE(pdwValue), &dwSize);
  84. switch( m_nLastError )
  85. {
  86. case ERROR_SUCCESS:
  87. {
  88. if (dwType != REG_DWORD)
  89. nStatus = failed;
  90. else
  91. nStatus = no_error;
  92. }break;
  93. case ERROR_ACCESS_DENIED:
  94. nStatus = access_denied; break;
  95. case ERROR_FILE_NOT_FOUND:
  96. nStatus = not_found; break;
  97. default:
  98. nStatus = failed; break;
  99. }
  100. return nStatus;
  101. }
  102. int CNTRegistry::GetStr(WCHAR *pwszValueName, WCHAR **pwszValue)
  103. {
  104. *pwszValue = 0;
  105. DWORD dwSize = 0;
  106. DWORD dwType = 0;
  107. m_nLastError = RegQueryValueExW(m_hSubkey, pwszValueName, 0, &dwType,
  108. 0, &dwSize);
  109. if (m_nLastError != 0)
  110. {
  111. DEBUGTRACE( ( LOG_WMIADAP, "CNTRegistry::GetStr() failed: %X.\n", m_nLastError ) );
  112. return failed;
  113. }
  114. if ( ( dwType != REG_SZ ) && ( dwType != REG_EXPAND_SZ ) )
  115. {
  116. DEBUGTRACE( ( LOG_WMIADAP, "CNTRegistry::GetStr() failed due to an invalid registry data type.\n" ) );
  117. return failed;
  118. }
  119. BYTE *p = new BYTE[dwSize+sizeof(WCHAR)];
  120. if (NULL == p) return out_of_memory;
  121. p[dwSize] = 0;
  122. p[dwSize+1] = 0;
  123. m_nLastError = RegQueryValueExW(m_hSubkey, pwszValueName, 0, &dwType,
  124. LPBYTE(p), &dwSize);
  125. if (m_nLastError != 0)
  126. {
  127. delete [] p;
  128. DEBUGTRACE( ( LOG_WMIADAP, "CNTRegistry::GetStr() failed: %X.\n", m_nLastError ) );
  129. return failed;
  130. }
  131. if(dwType == REG_EXPAND_SZ)
  132. {
  133. // Get the initial length
  134. DWORD nSize = ExpandEnvironmentStringsW( (WCHAR *)p, NULL, 0 ) + 1;
  135. WCHAR* wszTemp = new WCHAR[ nSize + sizeof(WCHAR) ];
  136. if (NULL == wszTemp) { delete [] p; return out_of_memory; };
  137. wszTemp[nSize] = 0;
  138. ExpandEnvironmentStringsW( (WCHAR *)p, wszTemp, nSize - 1 );
  139. delete [] p;
  140. *pwszValue = wszTemp;
  141. }
  142. else
  143. *pwszValue = (WCHAR *)p;
  144. return no_error;
  145. }
  146. int CNTRegistry::GetBinary(WCHAR *pwszValueName, BYTE **ppBuffer, DWORD * pdwSize)
  147. {
  148. int nStatus = no_error;
  149. DWORD dwSize = 0;
  150. DWORD dwType = 0;
  151. m_nLastError = RegQueryValueExW(m_hSubkey, pwszValueName, 0, &dwType, 0, &dwSize );
  152. switch( m_nLastError )
  153. {
  154. case ERROR_SUCCESS:
  155. nStatus = no_error; break;
  156. case ERROR_ACCESS_DENIED:
  157. nStatus = access_denied; break;
  158. case ERROR_FILE_NOT_FOUND:
  159. nStatus = not_found; break;
  160. default:
  161. nStatus = failed; break;
  162. }
  163. if ( no_error == nStatus )
  164. {
  165. if ( dwType != REG_BINARY ) return failed;
  166. BYTE* pBuffer = new BYTE[dwSize];
  167. if (NULL == pBuffer) return out_of_memory;
  168. m_nLastError = RegQueryValueExW(m_hSubkey, pwszValueName, 0, &dwType, pBuffer, &dwSize );
  169. if ( ERROR_SUCCESS != m_nLastError )
  170. {
  171. delete [] pBuffer;
  172. nStatus = failed;
  173. }
  174. else
  175. {
  176. *ppBuffer = pBuffer;
  177. *pdwSize = dwSize;
  178. }
  179. }
  180. return nStatus;
  181. }
  182. int CNTRegistry::Enum( DWORD dwIndex, wmilib::auto_buffer<WCHAR> & pwszValue, DWORD& dwSize )
  183. {
  184. DWORD dwBuffSize = dwSize;
  185. m_nLastError = RegEnumKeyExW(m_hSubkey, dwIndex, pwszValue.get(), &dwBuffSize,
  186. NULL, NULL, NULL, NULL );
  187. while ( m_nLastError == ERROR_MORE_DATA )
  188. {
  189. // Grow in 256 byte chunks
  190. dwBuffSize += 256;
  191. // Reallocate the buffer and retry
  192. pwszValue.reset(new WCHAR[dwBuffSize]);
  193. if (NULL == pwszValue.get()) return out_of_memory;
  194. dwSize = dwBuffSize;
  195. m_nLastError = RegEnumKeyExW(m_hSubkey, dwIndex, pwszValue.get(), &dwBuffSize,
  196. NULL, NULL, NULL, NULL );
  197. }
  198. if ( ERROR_SUCCESS != m_nLastError )
  199. {
  200. if ( ERROR_NO_MORE_ITEMS == m_nLastError )
  201. {
  202. return no_more_items;
  203. }
  204. else
  205. {
  206. return failed;
  207. }
  208. }
  209. return no_error;
  210. }
  211. int CNTRegistry::GetMultiStr(WCHAR *pwszValueName, WCHAR** pwszValue, DWORD &dwSize)
  212. {
  213. //Find out the size of the buffer required
  214. DWORD dwType;
  215. m_nLastError = RegQueryValueExW(m_hSubkey, pwszValueName, 0, &dwType, NULL, &dwSize);
  216. //If the error is an unexpected one bail out
  217. if ((m_nLastError != ERROR_SUCCESS) || (dwType != REG_MULTI_SZ))
  218. {
  219. dwSize = 0;
  220. DEBUGTRACE( ( LOG_WMIADAP, "CNTRegistry::GetMultiStr() failed: %X.\n", m_nLastError ) );
  221. return failed;
  222. }
  223. if (dwSize == 0)
  224. {
  225. dwSize = 0;
  226. DEBUGTRACE( ( LOG_WMIADAP, "CNTRegistry::GetMultiStr() failed due to null string.\n" ) );
  227. return failed;
  228. }
  229. //allocate the buffer required
  230. BYTE *pData = new BYTE[dwSize];
  231. if (NULL == pData) return out_of_memory;
  232. //get the values
  233. m_nLastError = RegQueryValueExW(m_hSubkey,
  234. pwszValueName,
  235. 0,
  236. &dwType,
  237. LPBYTE(pData),
  238. &dwSize);
  239. //if an error bail out
  240. if (m_nLastError != 0)
  241. {
  242. delete [] pData;
  243. dwSize = 0;
  244. DEBUGTRACE( ( LOG_WMIADAP, "CNTRegistry::GetMultiStr() failed: %X.\n", m_nLastError ) );
  245. return failed;
  246. }
  247. *pwszValue = (WCHAR *)pData;
  248. return no_error;
  249. }
  250. int CNTRegistry::SetDWORD(WCHAR *pwszValueName, DWORD dwValue)
  251. {
  252. int nStatus = no_error;
  253. m_nLastError = RegSetValueExW( m_hSubkey,
  254. pwszValueName,
  255. 0,
  256. REG_DWORD,
  257. (BYTE*)&dwValue,
  258. sizeof( dwValue ) );
  259. if ( m_nLastError != ERROR_SUCCESS )
  260. {
  261. nStatus = failed;
  262. }
  263. return nStatus;
  264. }
  265. int CNTRegistry::SetStr(WCHAR *pwszValueName, WCHAR *wszValue)
  266. {
  267. int nStatus = no_error;
  268. m_nLastError = RegSetValueExW( m_hSubkey,
  269. pwszValueName,
  270. 0,
  271. REG_SZ,
  272. (BYTE*)wszValue,
  273. sizeof(WCHAR) * (wcslen(wszValue) + 1) );
  274. if ( m_nLastError != ERROR_SUCCESS )
  275. {
  276. nStatus = failed;
  277. }
  278. return nStatus;
  279. }
  280. int CNTRegistry::SetBinary(WCHAR *pwszValueName, BYTE* pBuffer, DWORD dwSize )
  281. {
  282. int nStatus = no_error;
  283. m_nLastError = RegSetValueExW( m_hSubkey, pwszValueName, 0, REG_BINARY, pBuffer, dwSize );
  284. if ( ERROR_SUCCESS != m_nLastError )
  285. {
  286. nStatus = failed;
  287. }
  288. return nStatus;
  289. }