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
7.4 KiB

  1. /*****************************************************************/
  2. /** Microsoft Windows for Workgroups **/
  3. /** Copyright (C) Microsoft Corp., 1991-1992 **/
  4. /*****************************************************************/
  5. /*
  6. regentry.cxx
  7. registry access
  8. This file contains those routines which enable net providers to
  9. conveniently access the registry for their entries.
  10. FILE HISTORY:
  11. lens 03/15/94 Created
  12. */
  13. #include "npcommon.h"
  14. #if defined(DEBUG)
  15. static const CHAR szFileName[] = __FILE__;
  16. #define _FILENAME_DEFINED_ONCE szFileName
  17. #endif
  18. #include <npassert.h>
  19. #include <npstring.h>
  20. #include <regentry.h>
  21. RegEntry::RegEntry(const char *pszSubKey, HKEY hkey)
  22. {
  23. _error = RegCreateKey(hkey, pszSubKey, &_hkey);
  24. if (_error) {
  25. bhkeyValid = FALSE;
  26. }
  27. else {
  28. bhkeyValid = TRUE;
  29. }
  30. }
  31. RegEntry::~RegEntry()
  32. {
  33. if (bhkeyValid) {
  34. RegCloseKey(_hkey);
  35. }
  36. }
  37. long RegEntry::SetValue(const char *pszValue, const char *string)
  38. {
  39. if (bhkeyValid) {
  40. _error = RegSetValueEx(_hkey, pszValue, 0, REG_SZ,
  41. (unsigned char *)string, lstrlen(string)+1);
  42. }
  43. return _error;
  44. }
  45. long RegEntry::SetValue(const char *pszValue, unsigned long dwNumber)
  46. {
  47. if (bhkeyValid) {
  48. _error = RegSetValueEx(_hkey, pszValue, 0, REG_BINARY,
  49. (unsigned char *)&dwNumber, sizeof(dwNumber));
  50. }
  51. return _error;
  52. }
  53. long RegEntry::DeleteValue(const char *pszValue)
  54. {
  55. if (bhkeyValid) {
  56. _error = RegDeleteValue(_hkey, (LPTSTR) pszValue);
  57. }
  58. return _error;
  59. }
  60. char *RegEntry::GetString(const char *pszValue, char *string, unsigned long length)
  61. {
  62. DWORD dwType = REG_SZ;
  63. if (bhkeyValid) {
  64. _error = RegQueryValueEx(_hkey, (LPTSTR) pszValue, 0, &dwType, (LPBYTE)string,
  65. &length);
  66. }
  67. if (_error)
  68. *string = '\0';
  69. return string;
  70. }
  71. long RegEntry::GetNumber(const char *pszValue, long dwDefault)
  72. {
  73. DWORD dwType = REG_BINARY;
  74. long dwNumber = 0L;
  75. DWORD dwSize = sizeof(dwNumber);
  76. if (bhkeyValid) {
  77. _error = RegQueryValueEx(_hkey, (LPTSTR) pszValue, 0, &dwType, (LPBYTE)&dwNumber,
  78. &dwSize);
  79. }
  80. if (_error)
  81. dwNumber = dwDefault;
  82. return dwNumber;
  83. }
  84. VOID RegEntry::GetValue(const char *pszValueName, NLS_STR *pnlsString)
  85. {
  86. DWORD dwType = REG_SZ;
  87. DWORD length = 0;
  88. CHAR * string = NULL;
  89. BOOL bReallocDoneOK = FALSE;
  90. if (bhkeyValid) {
  91. _error = RegQueryValueEx( _hkey,
  92. (LPTSTR) pszValueName,
  93. 0,
  94. &dwType,
  95. NULL,
  96. &length );
  97. if (_error == ERROR_SUCCESS) {
  98. if (!pnlsString->IsOwnerAlloc()) {
  99. bReallocDoneOK = pnlsString->realloc(length);
  100. }
  101. else if (length <= (UINT)pnlsString->QueryAllocSize()) {
  102. bReallocDoneOK = TRUE;
  103. }
  104. else {
  105. _error = ERROR_MORE_DATA;
  106. }
  107. }
  108. string = pnlsString->Party();
  109. if (bReallocDoneOK) {
  110. _error = RegQueryValueEx( _hkey,
  111. (LPTSTR) pszValueName,
  112. 0,
  113. &dwType,
  114. (LPBYTE) string,
  115. &length );
  116. if (_error == ERROR_SUCCESS) {
  117. if ((dwType != REG_SZ) && (dwType != REG_EXPAND_SZ)) {
  118. _error = ERROR_INVALID_PARAMETER;
  119. }
  120. }
  121. }
  122. else {
  123. _error = ERROR_NOT_ENOUGH_MEMORY;
  124. }
  125. }
  126. if (_error != ERROR_SUCCESS) {
  127. if (string != NULL) {
  128. *string = '\0';
  129. }
  130. }
  131. pnlsString->DonePartying();
  132. }
  133. VOID RegEntry::MoveToSubKey(const char *pszSubKeyName)
  134. {
  135. HKEY _hNewKey;
  136. if (bhkeyValid) {
  137. _error = RegOpenKey ( _hkey,
  138. pszSubKeyName,
  139. &_hNewKey );
  140. if (_error == ERROR_SUCCESS) {
  141. RegCloseKey(_hkey);
  142. _hkey = _hNewKey;
  143. }
  144. }
  145. }
  146. long RegEntry::FlushKey()
  147. {
  148. if (bhkeyValid) {
  149. _error = RegFlushKey(_hkey);
  150. }
  151. return _error;
  152. }
  153. RegEnumValues::RegEnumValues(RegEntry *pReqRegEntry)
  154. : pRegEntry(pReqRegEntry),
  155. iEnum(0),
  156. pchName(NULL),
  157. pbValue(NULL)
  158. {
  159. _error = pRegEntry->GetError();
  160. if (_error == ERROR_SUCCESS) {
  161. _error = RegQueryInfoKey ( pRegEntry->GetKey(), // Key
  162. NULL, // Buffer for class string
  163. NULL, // Size of class string buffer
  164. NULL, // Reserved
  165. NULL, // Number of subkeys
  166. NULL, // Longest subkey name
  167. NULL, // Longest class string
  168. &cEntries, // Number of value entries
  169. &cMaxValueName, // Longest value name
  170. &cMaxData, // Longest value data
  171. NULL, // Security descriptor
  172. NULL ); // Last write time
  173. }
  174. if (_error == ERROR_SUCCESS) {
  175. if (cEntries != 0) {
  176. cMaxValueName = cMaxValueName + 1; // REG_SZ needs one more for null
  177. cMaxData = cMaxData + 1; // REG_SZ needs one more for null
  178. pchName = new CHAR[cMaxValueName];
  179. if (!pchName) {
  180. _error = ERROR_NOT_ENOUGH_MEMORY;
  181. }
  182. else {
  183. if (cMaxData) {
  184. pbValue = new BYTE[cMaxData];
  185. if (!pbValue) {
  186. _error = ERROR_NOT_ENOUGH_MEMORY;
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }
  193. RegEnumValues::~RegEnumValues()
  194. {
  195. delete pchName;
  196. delete pbValue;
  197. }
  198. long RegEnumValues::Next()
  199. {
  200. if (_error != ERROR_SUCCESS) {
  201. return _error;
  202. }
  203. if (cEntries == iEnum) {
  204. return ERROR_NO_MORE_ITEMS;
  205. }
  206. DWORD cchName = cMaxValueName;
  207. dwDataLength = cMaxData;
  208. _error = RegEnumValue ( pRegEntry->GetKey(), // Key
  209. iEnum, // Index of value
  210. pchName, // Address of buffer for value name
  211. &cchName, // Address for size of buffer
  212. NULL, // Reserved
  213. &dwType, // Data type
  214. pbValue, // Address of buffer for value data
  215. &dwDataLength ); // Address for size of data
  216. iEnum++;
  217. return _error;
  218. }
  219. NPMachineEntries::NPMachineEntries(const char *pszReqSectionName)
  220. : RegEntry("System\\CurrentControlSet\\Services", HKEY_LOCAL_MACHINE),
  221. pszSectionName(pszReqSectionName)
  222. {
  223. if (GetError() == ERROR_SUCCESS) {
  224. MoveToSubKey(pszSectionName);
  225. if (GetError() == ERROR_SUCCESS) {
  226. MoveToSubKey("NetworkProvider");
  227. }
  228. }
  229. }