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.

258 lines
6.5 KiB

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