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.

338 lines
9.3 KiB

  1. // Copyright (c) 1996-1999 Microsoft Corporation
  2. //+============================================================================
  3. //
  4. // config.cxx
  5. //
  6. // Common configuration code for both services, in the form of the
  7. // CTrkConfiguration base class (inherited by CTrkWksConfiguration and
  8. // CTrkSvrConfiguration).
  9. //
  10. //+============================================================================
  11. #include <pch.cxx>
  12. #pragma hdrstop
  13. #include "trklib.hxx"
  14. CMultiTsz CTrkConfiguration::_mtszOperationLog;
  15. //+----------------------------------------------------------------------------
  16. //
  17. // CTrkConfiguration::Initialize
  18. //
  19. // Open the main key (identified by the global s_tszKeyNameLinkTrack,
  20. // which is defined appropriately in trkwks.dll and trksvr.dll). Then
  21. // read global values that are applicable to both services: debug flags,
  22. // operation log filename, and test flags.
  23. //
  24. //+----------------------------------------------------------------------------
  25. void
  26. CTrkConfiguration::Initialize()
  27. {
  28. ULONG lResult;
  29. TCHAR tszConfigurationKey[ MAX_PATH ];
  30. _fInitialized = TRUE;
  31. _hkey = NULL;
  32. // Open the base key
  33. _tcscpy( tszConfigurationKey, s_tszKeyNameLinkTrack );
  34. _tcscat( tszConfigurationKey, TEXT("\\") );
  35. _tcscat( tszConfigurationKey, TEXT("Configuration") );
  36. lResult = RegOpenKey( HKEY_LOCAL_MACHINE,
  37. tszConfigurationKey,
  38. &_hkey );
  39. if( ERROR_SUCCESS != lResult
  40. &&
  41. ERROR_PATH_NOT_FOUND != lResult
  42. &&
  43. ERROR_FILE_NOT_FOUND != lResult )
  44. {
  45. // We'll just make do without any custom configuration, rather than
  46. // making this a fatal error.
  47. TrkLog(( TRKDBG_ERROR, TEXT("Couldn't open Configuration key (%lu)"), lResult ));
  48. return;
  49. }
  50. // Read values that are common to trkwks & trksvr.
  51. Read( TRK_DEBUG_FLAGS_NAME, &_dwDebugFlags, TRK_DEBUG_FLAGS_DEFAULT );
  52. // Copy these flags into the global debug flags (debug.cxx)
  53. IFDBG( g_grfDebugFlags = _dwDebugFlags; )
  54. Read( DEBUG_STORE_FLAGS_NAME, &_dwDebugStoreFlags, DEBUG_STORE_FLAGS_DEFAULT );
  55. Read( TRK_TEST_FLAGS_NAME, &_dwTestFlags, TRK_TEST_FLAGS_DEFAULT );
  56. Read( OPERATION_LOG_NAME_NAME, &_mtszOperationLog, OPERATION_LOG_NAME_DEFAULT );
  57. }
  58. //+----------------------------------------------------------------------------
  59. //
  60. // CTrkConfiguration::UnInitialize
  61. //
  62. // Close the main reg key.
  63. //
  64. //+----------------------------------------------------------------------------
  65. VOID
  66. CTrkConfiguration::UnInitialize()
  67. {
  68. if( _fInitialized )
  69. {
  70. if( NULL != _hkey )
  71. {
  72. RegCloseKey( _hkey );
  73. _hkey = NULL;
  74. }
  75. _fInitialized = FALSE;
  76. }
  77. }
  78. //+----------------------------------------------------------------------------
  79. //
  80. // CTrkConfiguration::Read( REG_DWORD )
  81. //
  82. // Read a DWORD configuration parameter. Return the defaulut value
  83. // if not found.
  84. //
  85. //+----------------------------------------------------------------------------
  86. VOID
  87. CTrkConfiguration::Read( const TCHAR *ptszName, DWORD *pdwValue, DWORD dwDefault ) const
  88. {
  89. *pdwValue = dwDefault;
  90. if( NULL != _hkey )
  91. {
  92. LONG lResult;
  93. DWORD dwType;
  94. DWORD dwValue;
  95. DWORD cbValue = sizeof(dwValue);
  96. lResult = RegQueryValueEx( const_cast<HKEY>( _hkey ),
  97. ptszName,
  98. NULL,
  99. &dwType,
  100. (LPBYTE) &dwValue,
  101. &cbValue);
  102. if( ERROR_SUCCESS == lResult )
  103. {
  104. if( REG_DWORD == dwType )
  105. {
  106. *pdwValue = dwValue;
  107. TrkLog(( TRKDBG_MISC, TEXT("RegConfig: %s = 0x%x (%lu)"),
  108. ptszName, dwValue, dwValue ));
  109. }
  110. else
  111. TrkLog(( TRKDBG_ERROR,
  112. TEXT("Registry value is wrong type") ));
  113. }
  114. else if( ERROR_FILE_NOT_FOUND != lResult )
  115. {
  116. TrkLog(( TRKDBG_ERROR,
  117. TEXT("Couldn't read %s from registry (%lu)"), ptszName, lResult ));
  118. }
  119. }
  120. return;
  121. }
  122. //+----------------------------------------------------------------------------
  123. //
  124. // CTrkConfiguration::Read( REG_SZ )
  125. //
  126. // Read a string configuration parameter. Return the default value
  127. // if not found.
  128. //
  129. //+----------------------------------------------------------------------------
  130. VOID
  131. CTrkConfiguration::Read( const TCHAR *ptszName, ULONG *pcbValue, TCHAR *ptszValue, TCHAR *ptszDefault ) const
  132. {
  133. ULONG cbIn = *pcbValue;
  134. LONG lResult = ERROR_PATH_NOT_FOUND;
  135. DWORD dwType;
  136. if( NULL != _hkey )
  137. {
  138. lResult = RegQueryValueEx( const_cast<HKEY>( _hkey ),
  139. ptszName,
  140. NULL,
  141. &dwType,
  142. (LPBYTE) ptszValue,
  143. pcbValue );
  144. }
  145. if( ERROR_SUCCESS != lResult || REG_SZ != dwType )
  146. {
  147. *pcbValue = 0;
  148. memset( ptszValue, 0, cbIn );
  149. _tcscpy( ptszValue, ptszDefault );
  150. }
  151. else
  152. {
  153. TrkLog(( TRKDBG_MISC, TEXT("RegConfig: %s = %s"), ptszName, ptszValue ));
  154. }
  155. return;
  156. }
  157. //+----------------------------------------------------------------------------
  158. //
  159. // CTrkConfiguration::Read( REG_MULTI_SZ )
  160. //
  161. // Read a string array configuration parameter. Return the default
  162. // value if not found.
  163. //
  164. //+----------------------------------------------------------------------------
  165. VOID
  166. CTrkConfiguration::Read( const TCHAR *ptszName, CMultiTsz *pmtszValue, TCHAR *ptszDefault ) const
  167. {
  168. ULONG cbValue = pmtszValue->MaxSize();
  169. LONG lResult = ERROR_PATH_NOT_FOUND;
  170. DWORD dwType;
  171. // Read the value
  172. if( NULL != _hkey )
  173. {
  174. lResult = RegQueryValueEx( const_cast<HKEY>( _hkey ),
  175. ptszName,
  176. NULL,
  177. &dwType,
  178. pmtszValue->GetBuffer(),
  179. &cbValue );
  180. }
  181. // Check the type
  182. if( ERROR_SUCCESS != lResult || (REG_SZ != dwType && REG_MULTI_SZ != dwType) )
  183. {
  184. // It didn't exist, or was the wrong type. Return the default.
  185. *pmtszValue = ptszDefault;
  186. #if DBG
  187. if( ERROR_FILE_NOT_FOUND != lResult && ERROR_PATH_NOT_FOUND != lResult )
  188. TrkLog(( TRKDBG_ERROR, TEXT("Couldn't read %s from registry (%lu)"), ptszName, lResult ));
  189. #endif
  190. }
  191. else
  192. {
  193. // We found the value. Dump the first three values to dbgout.
  194. #if DBG
  195. TCHAR tszValues[ sizeof(*pmtszValue) ];
  196. _tcscpy( tszValues, TEXT("") );
  197. for( int i = 0; i < 3; i++ )
  198. {
  199. if( i >= (int) pmtszValue->NumStrings() )
  200. break;
  201. _tcscat( tszValues, (*pmtszValue)[i] );
  202. _tcscat( tszValues, TEXT(" ") );
  203. }
  204. if( 3 < (int) pmtszValue->NumStrings() )
  205. _tcscat( tszValues, TEXT("...") );
  206. TrkLog(( TRKDBG_MISC, TEXT("RegConfig: %s = %s"),
  207. ptszName, tszValues ));
  208. #endif // #if DBG
  209. }
  210. return;
  211. }
  212. //+----------------------------------------------------------------------------
  213. //
  214. // CTrkConfiguration::Write( REG_DWORD )
  215. //
  216. // Write a DWORD configuration parameter.
  217. //
  218. //+----------------------------------------------------------------------------
  219. BOOL
  220. CTrkConfiguration::Write( const TCHAR *ptszName, DWORD dwValue ) const
  221. {
  222. if( NULL != _hkey )
  223. {
  224. LONG lResult;
  225. DWORD cbValue = sizeof(dwValue);
  226. lResult = RegSetValueEx( const_cast<HKEY>( _hkey ),
  227. ptszName,
  228. NULL,
  229. REG_DWORD,
  230. (LPBYTE) &dwValue,
  231. cbValue);
  232. if( ERROR_SUCCESS != lResult )
  233. {
  234. TrkLog(( TRKDBG_ERROR, TEXT("Couldn't write %s to registry (%lu)"), ptszName, lResult ));
  235. return( FALSE );
  236. }
  237. }
  238. return( TRUE );
  239. }
  240. //+----------------------------------------------------------------------------
  241. //
  242. // CTrkConfiguration::Write( REG_SZ )
  243. //
  244. // Write a string configuration parameter.
  245. //
  246. //+----------------------------------------------------------------------------
  247. BOOL
  248. CTrkConfiguration::Write( const TCHAR *ptszName, const TCHAR *ptszValue ) const
  249. {
  250. LONG lResult = ERROR_PATH_NOT_FOUND;
  251. if( NULL != _hkey )
  252. {
  253. lResult = RegSetValueEx( const_cast<HKEY>( _hkey ),
  254. ptszName,
  255. NULL,
  256. REG_SZ,
  257. (LPBYTE) ptszValue,
  258. _tcslen(ptszValue) + 1 );
  259. }
  260. if( ERROR_SUCCESS != lResult )
  261. {
  262. TrkLog(( TRKDBG_ERROR, TEXT("Couldn't write %s to registry (%lu)"), ptszName, lResult ));
  263. return( FALSE );
  264. }
  265. return( TRUE );
  266. }