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.

247 lines
4.8 KiB

  1. //***************************************************************************
  2. //
  3. // REG.CPP
  4. //
  5. // Utility CNTRegistry classes.
  6. //
  7. // a-raymcc 30-May-96 Created.
  8. //
  9. //***************************************************************************
  10. #include <windows.h>
  11. #include <stdio.h>
  12. #include "ntreg.h"
  13. //#include <dbgalloc.h>
  14. //#include "adaputil.h"
  15. CNTRegistry::CNTRegistry() : m_hPrimaryKey(0),
  16. m_hSubkey(0),
  17. m_nStatus(0),
  18. m_nLastError(no_error)
  19. {}
  20. CNTRegistry::~CNTRegistry()
  21. {
  22. if (m_hSubkey)
  23. RegCloseKey(m_hSubkey);
  24. if (m_hPrimaryKey != m_hSubkey)
  25. RegCloseKey(m_hPrimaryKey);
  26. }
  27. int CNTRegistry::Open(HKEY hStart, WCHAR *pszStartKey)
  28. {
  29. int nStatus = no_error;
  30. m_nLastError = RegOpenKeyExW(hStart, pszStartKey,
  31. 0, KEY_ALL_ACCESS, &m_hPrimaryKey );
  32. if (m_nLastError != 0)
  33. nStatus = failed;
  34. m_hSubkey = m_hPrimaryKey;
  35. return nStatus;
  36. }
  37. int CNTRegistry::MoveToSubkey(WCHAR *pszNewSubkey)
  38. {
  39. int nStatus = no_error;
  40. m_nLastError = RegOpenKeyExW(m_hPrimaryKey, pszNewSubkey, 0, KEY_ALL_ACCESS, &m_hSubkey );
  41. if (m_nLastError != 0)
  42. nStatus = failed;
  43. return nStatus;
  44. }
  45. int CNTRegistry::GetDWORD(WCHAR *pwszValueName, DWORD *pdwValue)
  46. {
  47. int nStatus = no_error;
  48. DWORD dwSize = sizeof(DWORD);
  49. DWORD dwType = 0;
  50. m_nLastError = RegQueryValueExW(m_hSubkey, pwszValueName, 0, &dwType,
  51. LPBYTE(pdwValue), &dwSize);
  52. if (m_nLastError != 0)
  53. nStatus = failed;
  54. if (dwType != REG_DWORD)
  55. nStatus = failed;
  56. return nStatus;
  57. }
  58. int CNTRegistry::GetStr(WCHAR *pwszValueName, WCHAR **pwszValue)
  59. {
  60. *pwszValue = 0;
  61. DWORD dwSize = 0;
  62. DWORD dwType = 0;
  63. m_nLastError = RegQueryValueExW(m_hSubkey, pwszValueName, 0, &dwType,
  64. 0, &dwSize);
  65. if (m_nLastError != 0)
  66. return failed;
  67. if ( ( dwType != REG_SZ ) && ( dwType != REG_EXPAND_SZ ) )
  68. return failed;
  69. WCHAR *p = new WCHAR[dwSize];
  70. m_nLastError = RegQueryValueExW(m_hSubkey, pwszValueName, 0, &dwType,
  71. LPBYTE(p), &dwSize);
  72. if (m_nLastError != 0)
  73. {
  74. delete [] p;
  75. return failed;
  76. }
  77. if(dwType == REG_EXPAND_SZ)
  78. {
  79. WCHAR* wszTemp = NULL;
  80. // Get the initial length
  81. DWORD nSize = ExpandEnvironmentStringsW( (WCHAR *)p, wszTemp, 0 ) + 10;
  82. wszTemp = new WCHAR[ nSize ];
  83. ExpandEnvironmentStringsW( (WCHAR *)p, wszTemp, nSize - 1 );
  84. delete [] p;
  85. *pwszValue = wszTemp;
  86. }
  87. else
  88. *pwszValue = p;
  89. return no_error;
  90. }
  91. int CNTRegistry::Enum( DWORD dwIndex, WCHAR **pwszValue, DWORD& dwSize )
  92. {
  93. DWORD dwBuffSize = dwSize;
  94. m_nLastError = RegEnumKeyExW(m_hSubkey, dwIndex, *pwszValue, &dwBuffSize,
  95. NULL, NULL, NULL, NULL );
  96. while ( m_nLastError == ERROR_MORE_DATA )
  97. {
  98. // Grow in 256 byte chunks
  99. dwBuffSize += 256;
  100. try
  101. {
  102. // Reallocate the buffer and retry
  103. WCHAR* p = new WCHAR[dwBuffSize];
  104. if ( NULL != *pwszValue )
  105. {
  106. delete *pwszValue;
  107. }
  108. *pwszValue = p;
  109. dwSize = dwBuffSize;
  110. m_nLastError = RegEnumKeyExW(m_hSubkey, dwIndex, *pwszValue, &dwBuffSize,
  111. NULL, NULL, NULL, NULL );
  112. }
  113. catch (...)
  114. {
  115. return out_of_memory;
  116. }
  117. }
  118. if ( ERROR_SUCCESS != m_nLastError )
  119. {
  120. if ( ERROR_NO_MORE_ITEMS == m_nLastError )
  121. return no_more_items;
  122. else
  123. return failed;
  124. }
  125. return no_error;
  126. }
  127. int CNTRegistry::GetMultiStr(WCHAR *pwszValueName, WCHAR** pwszValue, DWORD &dwSize)
  128. {
  129. //Find out the size of the buffer required
  130. DWORD dwType;
  131. m_nLastError = RegQueryValueExW(m_hSubkey, pwszValueName, 0, &dwType, NULL, &dwSize);
  132. //If the error is an unexpected one bail out
  133. if ((m_nLastError != ERROR_SUCCESS) || (dwType != REG_MULTI_SZ))
  134. {
  135. dwSize = 0;
  136. return failed;
  137. }
  138. if (dwSize == 0)
  139. {
  140. dwSize = 0;
  141. return failed;
  142. }
  143. //allocate the buffer required
  144. WCHAR *pData = new WCHAR[dwSize];
  145. //get the values
  146. m_nLastError = RegQueryValueExW(m_hSubkey,
  147. pwszValueName,
  148. 0,
  149. &dwType,
  150. LPBYTE(pData),
  151. &dwSize);
  152. //if an error bail out
  153. if (m_nLastError != 0)
  154. {
  155. delete [] pData;
  156. dwSize = 0;
  157. return failed;
  158. }
  159. *pwszValue = pData;
  160. return no_error;
  161. }
  162. int CNTRegistry::SetDWORD(WCHAR *pwszValueName, DWORD dwValue)
  163. {
  164. int nStatus = no_error;
  165. m_nLastError = RegSetValueExW( m_hSubkey,
  166. pwszValueName,
  167. 0,
  168. REG_DWORD,
  169. (BYTE*)&dwValue,
  170. sizeof( dwValue ) );
  171. if ( m_nLastError != ERROR_SUCCESS )
  172. {
  173. nStatus = failed;
  174. }
  175. return nStatus;
  176. }
  177. int CNTRegistry::SetStr(WCHAR *pwszValueName, WCHAR *wszValue)
  178. {
  179. int nStatus = no_error;
  180. m_nLastError = RegSetValueExW( m_hSubkey,
  181. pwszValueName,
  182. 0,
  183. REG_SZ,
  184. (BYTE*)wszValue,
  185. sizeof(WCHAR) * (wcslen(wszValue) + 1) );
  186. if ( m_nLastError != ERROR_SUCCESS )
  187. {
  188. nStatus = failed;
  189. }
  190. return nStatus;
  191. }