Source code of Windows XP (NT5)
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.

300 lines
6.0 KiB

  1. ////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. ////////////////////////////////////////////////////////////////////////////////////////
  6. #include "globals.h"
  7. #include <objbase.h>
  8. #include <locale.h>
  9. #include "classfac.h"
  10. #include <stdio.h>
  11. #include <tchar.h>
  12. #include "wmiprov.h"
  13. extern BOOL APIENTRY DllMain (HINSTANCE hinstDLL,DWORD fdwReason, LPVOID lpReserved) ;
  14. DWORD g_InstanceRegister = 0 ;
  15. DWORD g_EventRegister = 0 ;
  16. STDAPI DllRegisterServer () ;
  17. STDAPI DllUnregisterServer () ;
  18. BOOL s_Exiting = FALSE ;
  19. LONG CALLBACK WindowsMainProc ( HWND a_hWnd , UINT a_message , WPARAM a_wParam , LPARAM a_lParam )
  20. {
  21. long t_rc = 0 ;
  22. switch ( a_message )
  23. {
  24. case WM_CLOSE:
  25. {
  26. s_Exiting = TRUE ;
  27. }
  28. break ;
  29. case WM_DESTROY:
  30. {
  31. PostMessage ( a_hWnd , WM_QUIT , 0 , 0 ) ;
  32. }
  33. break ;
  34. default:
  35. {
  36. t_rc = DefWindowProc ( a_hWnd , a_message , a_wParam , a_lParam ) ;
  37. }
  38. break ;
  39. }
  40. return ( t_rc ) ;
  41. }
  42. HWND WindowsInit ( HINSTANCE a_HInstance )
  43. {
  44. static wchar_t *t_TemplateCode = L"WDM Provider" ;
  45. WNDCLASS t_wc ;
  46. t_wc.style = CS_HREDRAW | CS_VREDRAW ;
  47. t_wc.lpfnWndProc = WindowsMainProc ;
  48. t_wc.cbClsExtra = 0 ;
  49. t_wc.cbWndExtra = 0 ;
  50. t_wc.hInstance = a_HInstance ;
  51. t_wc.hIcon = LoadIcon(NULL, IDI_HAND) ;
  52. t_wc.hCursor = LoadCursor(NULL, IDC_ARROW) ;
  53. t_wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
  54. t_wc.lpszMenuName = NULL ;
  55. t_wc.lpszClassName = t_TemplateCode ;
  56. ATOM t_winClass = RegisterClass ( &t_wc ) ;
  57. HWND t_HWnd = CreateWindow (
  58. t_TemplateCode , // see RegisterClass() call
  59. t_TemplateCode , // text for window title bar
  60. WS_OVERLAPPEDWINDOW , // window style
  61. CW_USEDEFAULT , // default horizontal position
  62. CW_USEDEFAULT , // default vertical position
  63. CW_USEDEFAULT , // default width
  64. CW_USEDEFAULT , // default height
  65. NULL , // overlapped windows have no parent
  66. NULL , // use the window class menu
  67. a_HInstance ,
  68. NULL // pointer not needed
  69. ) ;
  70. ShowWindow ( t_HWnd, SW_SHOW ) ;
  71. // ShowWindow ( t_HWnd, SW_HIDE ) ;
  72. return t_HWnd ;
  73. }
  74. void WindowsStop ( HWND a_HWnd )
  75. {
  76. CoUninitialize () ;
  77. DestroyWindow ( a_HWnd ) ;
  78. }
  79. HWND WindowsStart ( HINSTANCE a_Handle )
  80. {
  81. HWND t_HWnd = NULL ;
  82. if ( ! ( t_HWnd = WindowsInit ( a_Handle ) ) )
  83. {
  84. }
  85. return t_HWnd ;
  86. }
  87. void WindowsDispatch ()
  88. {
  89. BOOL t_GetMessage ;
  90. MSG t_lpMsg ;
  91. while ( ( t_GetMessage = GetMessage ( & t_lpMsg , NULL , 0 , 0 ) ) == TRUE )
  92. {
  93. TranslateMessage ( & t_lpMsg ) ;
  94. DispatchMessage ( & t_lpMsg ) ;
  95. if ( s_Exiting )
  96. return ;
  97. }
  98. }
  99. HRESULT UninitComServer ()
  100. {
  101. if ( g_InstanceRegister )
  102. CoRevokeClassObject ( g_InstanceRegister );
  103. CoUninitialize () ;
  104. return S_OK ;
  105. }
  106. HRESULT InitInstanceProvider ()
  107. {
  108. IUnknown *t_ClassFactory = new CProvFactory(CLSID_WMIProvider);
  109. DWORD t_ClassContext = CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER ;
  110. DWORD t_Flags = REGCLS_MULTIPLEUSE ;
  111. HRESULT t_Result = CoRegisterClassObject ( CLSID_WMIProvider ,
  112. t_ClassFactory,
  113. t_ClassContext,
  114. t_Flags,
  115. &g_InstanceRegister
  116. );
  117. return t_Result ;
  118. }
  119. HRESULT InitEventProvider ()
  120. {
  121. IUnknown *t_ClassFactory = new CProvFactory(CLSID_WMIEventProvider);
  122. DWORD t_ClassContext = CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER ;
  123. DWORD t_Flags = REGCLS_MULTIPLEUSE ;
  124. HRESULT t_Result = CoRegisterClassObject (CLSID_WMIEventProvider,
  125. t_ClassFactory,
  126. t_ClassContext,
  127. t_Flags,
  128. &g_EventRegister
  129. );
  130. return t_Result ;
  131. }
  132. HRESULT InitComServer ( DWORD a_AuthenticationLevel , DWORD a_ImpersonationLevel )
  133. {
  134. HRESULT t_Result = S_OK ;
  135. t_Result = CoInitializeEx (
  136. 0,
  137. COINIT_MULTITHREADED
  138. );
  139. if ( SUCCEEDED ( t_Result ) )
  140. {
  141. t_Result = CoInitializeSecurity (
  142. NULL,
  143. -1,
  144. NULL,
  145. NULL,
  146. a_AuthenticationLevel,
  147. a_ImpersonationLevel,
  148. NULL,
  149. EOAC_NONE,
  150. 0
  151. );
  152. if ( FAILED ( t_Result ) )
  153. {
  154. CoUninitialize () ;
  155. return t_Result ;
  156. }
  157. }
  158. if ( SUCCEEDED ( t_Result ) )
  159. t_Result = InitInstanceProvider () ;
  160. if ( SUCCEEDED ( t_Result ) )
  161. t_Result = InitEventProvider () ;
  162. if ( FAILED ( t_Result ) )
  163. {
  164. UninitComServer () ;
  165. }
  166. return t_Result ;
  167. }
  168. HRESULT Process ()
  169. {
  170. #if 1
  171. DWORD t_ImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE ;
  172. DWORD t_AuthenticationLevel = RPC_C_AUTHN_LEVEL_CONNECT ;
  173. #else
  174. DWORD t_ImpersonationLevel = RPC_C_IMP_LEVEL_IDENTITY ;
  175. DWORD t_AuthenticationLevel = RPC_C_AUTHN_LEVEL_NONE ;
  176. #endif
  177. HRESULT t_Result = InitComServer ( t_ImpersonationLevel , t_AuthenticationLevel ) ;
  178. if ( SUCCEEDED ( t_Result ) )
  179. {
  180. WindowsDispatch () ;
  181. UninitComServer () ;
  182. }
  183. return t_Result ;
  184. }
  185. BOOL ParseCommandLine ()
  186. {
  187. BOOL t_Exit = FALSE ;
  188. LPTSTR t_CommandLine = GetCommandLine () ;
  189. if ( t_CommandLine )
  190. {
  191. TCHAR *t_Arg = NULL ;
  192. TCHAR *t_ApplicationArg = NULL ;
  193. t_ApplicationArg = _tcstok ( t_CommandLine , _TEXT ( " \t" ) ) ;
  194. t_Arg = _tcstok ( NULL , _TEXT ( " \t" ) ) ;
  195. if ( t_Arg )
  196. {
  197. if ( _tcsicmp ( t_Arg , _TEXT ( "/RegServer" ) ) == 0 )
  198. {
  199. t_Exit = TRUE ;
  200. DllRegisterServer () ;
  201. }
  202. else if ( _tcsicmp ( t_Arg , _TEXT ( "/UnRegServer" ) ) == 0 )
  203. {
  204. t_Exit = TRUE ;
  205. DllUnregisterServer () ;
  206. }
  207. }
  208. }
  209. return t_Exit ;
  210. }
  211. int WINAPI WinMain (
  212. HINSTANCE hInstance, // handle to current instance
  213. HINSTANCE hPrevInstance, // handle to previous instance
  214. LPSTR lpCmdLine, // pointer to command line
  215. int nShowCmd // show state of window
  216. )
  217. {
  218. BOOL t_Status = DllMain (
  219. hInstance,
  220. DLL_PROCESS_ATTACH ,
  221. NULL
  222. ) ;
  223. BOOL t_Exit = ParseCommandLine () ;
  224. if ( ! t_Exit )
  225. {
  226. HWND hWnd = WindowsStart ( hInstance ) ;
  227. HRESULT t_Result = Process () ;
  228. WindowsStop ( hWnd ) ;
  229. }
  230. t_Status = DllMain (
  231. hInstance,
  232. DLL_PROCESS_DETACH ,
  233. NULL
  234. ) ;
  235. return 0 ;
  236. }