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.

230 lines
5.4 KiB

  1. ////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2000-2002, Microsoft Corporation.
  4. //
  5. // All rights reserved.
  6. //
  7. // Module Name:
  8. //
  9. // WMI_adapter.cpp
  10. //
  11. // Abstract:
  12. //
  13. // Defines the entry point for the DLL application.
  14. //
  15. // History:
  16. //
  17. // initial a-marius
  18. //
  19. ////////////////////////////////////////////////////////////////////////////////////
  20. #include "precomp.h"
  21. #include "WMI_adapter.h"
  22. // debuging features
  23. #ifndef _INC_CRTDBG
  24. #include <crtdbg.h>
  25. #endif _INC_CRTDBG
  26. // new stores file/line info
  27. #ifdef _DEBUG
  28. #ifndef NEW
  29. #define NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
  30. #define new NEW
  31. #endif NEW
  32. #endif _DEBUG
  33. ////////////////////////////////////////////////////////////////////////////////////
  34. // variables
  35. ////////////////////////////////////////////////////////////////////////////////////
  36. #include "wmi_adapter_wrapper.h"
  37. __WrapperPtr<WmiAdapterWrapper> pWrapper;
  38. #include "wmi_eventlog.h"
  39. __WrapperPtr<CPerformanceEventLog> pEventLog;
  40. #include "wmi_security.h"
  41. #include "wmi_security_attributes.h"
  42. __WrapperPtr<WmiSecurityAttributes> pSA;
  43. #include "WMI_adapter_registry.h"
  44. // save instance
  45. HMODULE g_hModule = NULL;
  46. #ifdef __SUPPORT_ICECAP
  47. #include <icecap.h>
  48. #endif __SUPPORT_ICECAP
  49. #include <wmiaplog.h>
  50. #include ".\\WMIAdapter_Refresh\\RefresherGenerate.H"
  51. ////////////////////////////////////////////////////////////////////////////////////
  52. // dll main
  53. ////////////////////////////////////////////////////////////////////////////////////
  54. BOOL APIENTRY DllMain( HANDLE hModule, DWORD dwReason, LPVOID )
  55. {
  56. switch (dwReason)
  57. {
  58. case DLL_PROCESS_ATTACH:
  59. {
  60. AdapterLogMessage0 ( L"DllMain -> DLL_PROCESS_ATTACH" );
  61. // if ( !lpReserved )
  62. // {
  63. // // loaded dynamic
  64. // }
  65. // else
  66. // {
  67. // // loaded static
  68. // }
  69. // disable attach/detach of threads
  70. ::DisableThreadLibraryCalls ( (HMODULE) hModule );
  71. BOOL bResult = FALSE;
  72. try
  73. {
  74. // event log initialization
  75. pEventLog.SetData( new CPerformanceEventLog( L"WmiAdapter" ) );
  76. pSA.SetData ( new WmiSecurityAttributes() );
  77. if ( ! pSA.IsEmpty() )
  78. {
  79. if ( pSA->GetSecurityAttributtes () )
  80. {
  81. pWrapper.SetData( new WmiAdapterWrapper() );
  82. if ( ! pWrapper.IsEmpty() )
  83. {
  84. bResult = TRUE;
  85. }
  86. }
  87. }
  88. }
  89. catch ( ... )
  90. {
  91. }
  92. if ( bResult )
  93. {
  94. g_hModule = ( HMODULE ) hModule;
  95. }
  96. return bResult;
  97. }
  98. break;
  99. case DLL_PROCESS_DETACH:
  100. {
  101. AdapterLogMessage0 ( L"DllMain -> DLL_PROCESS_DETACH" );
  102. // if ( !lpReserved )
  103. // {
  104. // // unloaded by FreeLibrary
  105. // }
  106. // else
  107. // {
  108. // // unloaded by end of proccess
  109. // }
  110. // delete event log
  111. if ( !pEventLog.IsEmpty() )
  112. {
  113. delete pEventLog.Detach();
  114. }
  115. // delete perflib wrapper
  116. delete pWrapper.Detach ();
  117. // delete security attributes
  118. delete pSA.Detach();
  119. #ifdef _DEBUG
  120. _CrtDumpMemoryLeaks();
  121. #endif _DEBUG
  122. }
  123. break;
  124. // thread attaching is not used !!!
  125. case DLL_THREAD_ATTACH: break;
  126. case DLL_THREAD_DETACH: break;
  127. }
  128. AdapterLogMessage1 ( L"DllMain", 1 );
  129. return TRUE;
  130. }
  131. ////////////////////////////////////////////////////////////////////////////////////
  132. // registration of perflib dll
  133. ////////////////////////////////////////////////////////////////////////////////////
  134. EXTERN_C HRESULT __stdcall DllRegisterServer ( )
  135. {
  136. if ( ::GetModuleFileNameW ( g_hModule, g_szPath, _MAX_PATH - 1 ) )
  137. {
  138. g_szPath [ _MAX_PATH - 1 ] = L'\0';
  139. HRESULT hr = S_OK;
  140. if SUCCEEDED ( hr = WmiAdapterRegistry::__UpdateRegistrySZ( true ) )
  141. {
  142. hr = DoReverseAdapterMaintenanceInternal ( FALSE, Registration );
  143. }
  144. return hr;
  145. }
  146. return HRESULT_FROM_WIN32 ( ::GetLastError() );
  147. }
  148. ////////////////////////////////////////////////////////////////////////////////////
  149. // unregistration of perflib dll
  150. ////////////////////////////////////////////////////////////////////////////////////
  151. EXTERN_C HRESULT __stdcall DllUnregisterServer ( void )
  152. {
  153. HRESULT hr = S_OK;
  154. if SUCCEEDED ( hr = DoReverseAdapterMaintenanceInternal ( FALSE, UnRegistration ) )
  155. {
  156. hr = WmiAdapterRegistry::__UpdateRegistrySZ( false );
  157. }
  158. return hr;
  159. }
  160. ////////////////////////////////////////////////////////////////////////////////////
  161. ////////////////////////////////////////////////////////////////////////////////////
  162. // exported functions ( PERFORMANCE )
  163. ////////////////////////////////////////////////////////////////////////////////////
  164. ////////////////////////////////////////////////////////////////////////////////////
  165. DWORD __stdcall WmiOpenPerfData ( LPWSTR lpwszDeviceNames )
  166. {
  167. #ifdef __SUPPORT_ICECAP
  168. StartProfile ( PROFILE_GLOBALLEVEL, PROFILE_CURRENTID );
  169. #endif __SUPPORT_ICECAP
  170. AdapterLogMessage0 ( L"WmiOpenPerfData" );
  171. return pWrapper->Open ( lpwszDeviceNames );
  172. }
  173. DWORD __stdcall WmiClosePerfData ()
  174. {
  175. #ifdef __SUPPORT_ICECAP
  176. StopProfile ( PROFILE_GLOBALLEVEL, PROFILE_CURRENTID );
  177. #endif __SUPPORT_ICECAP
  178. AdapterLogMessage0 ( L"WmiClosePerfData" );
  179. return pWrapper->Close ( );
  180. }
  181. DWORD __stdcall WmiCollectPerfData ( LPWSTR lpwszValue,
  182. LPVOID *lppData,
  183. LPDWORD lpcbBytes,
  184. LPDWORD lpcbObjectTypes
  185. )
  186. {
  187. return pWrapper->Collect ( lpwszValue, lppData, lpcbBytes, lpcbObjectTypes );
  188. }