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.

257 lines
6.4 KiB

  1. //*********************************************************************
  2. //* Microsoft Windows **
  3. //* Copyright (c) 1994-1999 Microsoft Corporation
  4. //*********************************************************************
  5. //
  6. // CLSUTIL.C - some small, useful C++ classes to wrap memory allocation,
  7. // registry access, etc.
  8. //
  9. // HISTORY:
  10. //
  11. // 12/07/94 jeremys Borrowed from WNET common library
  12. //
  13. #include "wizard.h"
  14. BOOL BUFFER::Alloc( UINT cbBuffer )
  15. {
  16. _lpBuffer = (LPSTR)::GlobalAlloc(GPTR,cbBuffer);
  17. if (_lpBuffer != NULL) {
  18. _cb = cbBuffer;
  19. return TRUE;
  20. }
  21. return FALSE;
  22. }
  23. BOOL BUFFER::Realloc( UINT cbNew )
  24. {
  25. LPVOID lpNew = ::GlobalReAlloc((HGLOBAL)_lpBuffer, cbNew,
  26. GMEM_MOVEABLE | GMEM_ZEROINIT);
  27. if (lpNew == NULL)
  28. return FALSE;
  29. _lpBuffer = (LPSTR)lpNew;
  30. _cb = cbNew;
  31. return TRUE;
  32. }
  33. BUFFER::BUFFER( UINT cbInitial /* =0 */ )
  34. : BUFFER_BASE(),
  35. _lpBuffer( NULL )
  36. {
  37. if (cbInitial)
  38. Alloc( cbInitial );
  39. }
  40. BUFFER::~BUFFER()
  41. {
  42. if (_lpBuffer != NULL) {
  43. GlobalFree((HGLOBAL) _lpBuffer);
  44. _lpBuffer = NULL;
  45. }
  46. }
  47. BOOL BUFFER::Resize( UINT cbNew )
  48. {
  49. BOOL fSuccess;
  50. if (QuerySize() == 0)
  51. fSuccess = Alloc( cbNew );
  52. else {
  53. fSuccess = Realloc( cbNew );
  54. }
  55. if (fSuccess)
  56. _cb = cbNew;
  57. return fSuccess;
  58. }
  59. RegEntry::RegEntry(const char *pszSubKey, HKEY hkey)
  60. {
  61. _error = RegCreateKey(hkey, pszSubKey, &_hkey);
  62. if (_error) {
  63. bhkeyValid = FALSE;
  64. }
  65. else {
  66. bhkeyValid = TRUE;
  67. }
  68. }
  69. RegEntry::~RegEntry()
  70. {
  71. if (bhkeyValid) {
  72. RegCloseKey(_hkey);
  73. }
  74. }
  75. long RegEntry::SetValue(const char *pszValue, const char *string)
  76. {
  77. if (bhkeyValid) {
  78. _error = RegSetValueEx(_hkey, pszValue, 0, REG_SZ,
  79. (unsigned char *)string, lstrlen(string)+1);
  80. }
  81. return _error;
  82. }
  83. long RegEntry::SetValue(const char *pszValue, unsigned long dwNumber)
  84. {
  85. if (bhkeyValid) {
  86. _error = RegSetValueEx(_hkey, pszValue, 0, REG_BINARY,
  87. (unsigned char *)&dwNumber, sizeof(dwNumber));
  88. }
  89. return _error;
  90. }
  91. long RegEntry::DeleteValue(const char *pszValue)
  92. {
  93. if (bhkeyValid) {
  94. _error = RegDeleteValue(_hkey, (LPTSTR) pszValue);
  95. }
  96. return _error;
  97. }
  98. char *RegEntry::GetString(const char *pszValue, char *string, unsigned long length)
  99. {
  100. DWORD dwType = REG_SZ;
  101. if (bhkeyValid) {
  102. _error = RegQueryValueEx(_hkey, (LPTSTR) pszValue, 0, &dwType, (LPBYTE)string,
  103. &length);
  104. }
  105. if (_error) {
  106. *string = '\0';
  107. return NULL;
  108. }
  109. return string;
  110. }
  111. long RegEntry::GetNumber(const char *pszValue, long dwDefault)
  112. {
  113. DWORD dwType = REG_BINARY;
  114. long dwNumber = 0L;
  115. DWORD dwSize = sizeof(dwNumber);
  116. if (bhkeyValid) {
  117. _error = RegQueryValueEx(_hkey, (LPTSTR) pszValue, 0, &dwType, (LPBYTE)&dwNumber,
  118. &dwSize);
  119. }
  120. if (_error)
  121. dwNumber = dwDefault;
  122. return dwNumber;
  123. }
  124. long RegEntry::MoveToSubKey(const char *pszSubKeyName)
  125. {
  126. HKEY _hNewKey;
  127. if (bhkeyValid) {
  128. _error = RegOpenKey ( _hkey,
  129. pszSubKeyName,
  130. &_hNewKey );
  131. if (_error == ERROR_SUCCESS) {
  132. RegCloseKey(_hkey);
  133. _hkey = _hNewKey;
  134. }
  135. }
  136. return _error;
  137. }
  138. long RegEntry::FlushKey()
  139. {
  140. if (bhkeyValid) {
  141. _error = RegFlushKey(_hkey);
  142. }
  143. return _error;
  144. }
  145. RegEnumValues::RegEnumValues(RegEntry *pReqRegEntry)
  146. : pRegEntry(pReqRegEntry),
  147. iEnum(0),
  148. pchName(NULL),
  149. pbValue(NULL)
  150. {
  151. _error = pRegEntry->GetError();
  152. if (_error == ERROR_SUCCESS) {
  153. _error = RegQueryInfoKey ( pRegEntry->GetKey(), // Key
  154. NULL, // Buffer for class string
  155. NULL, // Size of class string buffer
  156. NULL, // Reserved
  157. NULL, // Number of subkeys
  158. NULL, // Longest subkey name
  159. NULL, // Longest class string
  160. &cEntries, // Number of value entries
  161. &cMaxValueName, // Longest value name
  162. &cMaxData, // Longest value data
  163. NULL, // Security descriptor
  164. NULL ); // Last write time
  165. }
  166. if (_error == ERROR_SUCCESS) {
  167. if (cEntries != 0) {
  168. cMaxValueName = cMaxValueName + 1; // REG_SZ needs one more for null
  169. cMaxData = cMaxData + 1; // REG_SZ needs one more for null
  170. pchName = new CHAR[cMaxValueName];
  171. if (!pchName) {
  172. _error = ERROR_NOT_ENOUGH_MEMORY;
  173. }
  174. else {
  175. if (cMaxData) {
  176. pbValue = new BYTE[cMaxData];
  177. if (!pbValue) {
  178. _error = ERROR_NOT_ENOUGH_MEMORY;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. RegEnumValues::~RegEnumValues()
  186. {
  187. delete pchName;
  188. delete pbValue;
  189. }
  190. long RegEnumValues::Next()
  191. {
  192. if (_error != ERROR_SUCCESS) {
  193. return _error;
  194. }
  195. if (cEntries == iEnum) {
  196. return ERROR_NO_MORE_ITEMS;
  197. }
  198. DWORD cchName = cMaxValueName;
  199. dwDataLength = cMaxData;
  200. _error = RegEnumValue ( pRegEntry->GetKey(), // Key
  201. iEnum, // Index of value
  202. pchName, // Address of buffer for value name
  203. &cchName, // Address for size of buffer
  204. NULL, // Reserved
  205. &dwType, // Data type
  206. pbValue, // Address of buffer for value data
  207. &dwDataLength ); // Address for size of data
  208. iEnum++;
  209. return _error;
  210. }
  211. int __cdecl _purecall(void)
  212. {
  213. return(0);
  214. }
  215. void * _cdecl operator new(size_t size)
  216. {
  217. return (void *)::GlobalAlloc(GPTR,size);
  218. }
  219. void _cdecl operator delete(void *ptr)
  220. {
  221. GlobalFree(ptr);
  222. }