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.

386 lines
8.0 KiB

  1. //***************************************************************************
  2. //
  3. // File:
  4. //
  5. // Module: MS SNMP Provider
  6. //
  7. // Purpose:
  8. //
  9. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. #define _WIN32_WINNT 0x0400
  14. #include <snmpstd.h>
  15. #include <snmpmt.h>
  16. #include <snmptempl.h>
  17. #include <typeinfo.h>
  18. #include <process.h>
  19. #include <objbase.h>
  20. #include <stdio.h>
  21. #include <wbemidl.h>
  22. #include <instpath.h>
  23. #include <snmpcont.h>
  24. #include <snmpevt.h>
  25. #include <snmpthrd.h>
  26. #include <snmplog.h>
  27. #include <guids.h>
  28. #include <classfac.h>
  29. DWORD g_InstanceRegister = 0 ;
  30. DWORD g_ClassRegister = 0 ;
  31. DWORD g_EventReferentRegister = 0 ;
  32. DWORD g_EventEncapsulatedRegister = 0 ;
  33. STDAPI DllRegisterServer () ;
  34. STDAPI DllUnregisterServer () ;
  35. BOOL s_Exiting = FALSE ;
  36. LONG CALLBACK WindowsMainProc ( HWND a_hWnd , UINT a_message , WPARAM a_wParam , LPARAM a_lParam )
  37. {
  38. long t_rc = 0 ;
  39. switch ( a_message )
  40. {
  41. case WM_CLOSE:
  42. {
  43. s_Exiting = TRUE ;
  44. }
  45. break ;
  46. case WM_DESTROY:
  47. {
  48. PostMessage ( a_hWnd , WM_QUIT , 0 , 0 ) ;
  49. }
  50. break ;
  51. default:
  52. {
  53. t_rc = DefWindowProc ( a_hWnd , a_message , a_wParam , a_lParam ) ;
  54. }
  55. break ;
  56. }
  57. return ( t_rc ) ;
  58. }
  59. HWND WindowsInit ( HINSTANCE a_HInstance )
  60. {
  61. static wchar_t *t_TemplateCode = L"TemplateCode" ;
  62. WNDCLASS t_wc ;
  63. t_wc.style = CS_HREDRAW | CS_VREDRAW ;
  64. t_wc.lpfnWndProc = WindowsMainProc ;
  65. t_wc.cbClsExtra = 0 ;
  66. t_wc.cbWndExtra = 0 ;
  67. t_wc.hInstance = a_HInstance ;
  68. t_wc.hIcon = LoadIcon(NULL, IDI_HAND) ;
  69. t_wc.hCursor = LoadCursor(NULL, IDC_ARROW) ;
  70. t_wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
  71. t_wc.lpszMenuName = NULL ;
  72. t_wc.lpszClassName = t_TemplateCode ;
  73. ATOM t_winClass = RegisterClass ( &t_wc ) ;
  74. HWND t_HWnd = CreateWindow (
  75. t_TemplateCode , // see RegisterClass() call
  76. t_TemplateCode , // text for window title bar
  77. WS_OVERLAPPEDWINDOW , // window style
  78. CW_USEDEFAULT , // default horizontal position
  79. CW_USEDEFAULT , // default vertical position
  80. CW_USEDEFAULT , // default width
  81. CW_USEDEFAULT , // default height
  82. NULL , // overlapped windows have no parent
  83. NULL , // use the window class menu
  84. a_HInstance ,
  85. NULL // pointer not needed
  86. ) ;
  87. ShowWindow ( t_HWnd, SW_SHOW ) ;
  88. // ShowWindow ( t_HWnd, SW_HIDE ) ;
  89. return t_HWnd ;
  90. }
  91. void WindowsStop ( HWND a_HWnd )
  92. {
  93. CoUninitialize () ;
  94. DestroyWindow ( a_HWnd ) ;
  95. }
  96. HWND WindowsStart ( HINSTANCE a_Handle )
  97. {
  98. HWND t_HWnd = NULL ;
  99. if ( ! ( t_HWnd = WindowsInit ( a_Handle ) ) )
  100. {
  101. }
  102. return t_HWnd ;
  103. }
  104. void WindowsDispatch ()
  105. {
  106. BOOL t_GetMessage ;
  107. MSG t_lpMsg ;
  108. while ( ( t_GetMessage = GetMessage ( & t_lpMsg , NULL , 0 , 0 ) ) == TRUE )
  109. {
  110. TranslateMessage ( & t_lpMsg ) ;
  111. DispatchMessage ( & t_lpMsg ) ;
  112. if ( s_Exiting )
  113. return ;
  114. }
  115. }
  116. HRESULT UninitComServer ()
  117. {
  118. if ( g_InstanceRegister )
  119. CoRevokeClassObject ( g_InstanceRegister );
  120. if ( g_ClassRegister )
  121. CoRevokeClassObject ( g_ClassRegister );
  122. if ( g_EventEncapsulatedRegister )
  123. CoRevokeClassObject ( g_EventEncapsulatedRegister );
  124. if ( g_EventReferentRegister )
  125. CoRevokeClassObject ( g_EventReferentRegister );
  126. CoUninitialize () ;
  127. return S_OK ;
  128. }
  129. HRESULT InitInstanceProvider ()
  130. {
  131. IUnknown *t_ClassFactory = new CPropProvClassFactory ;
  132. DWORD t_ClassContext = CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER ;
  133. DWORD t_Flags = REGCLS_MULTIPLEUSE ;
  134. HRESULT t_Result = CoRegisterClassObject (
  135. CLSID_CPropProvClassFactory,
  136. t_ClassFactory,
  137. t_ClassContext,
  138. t_Flags,
  139. &g_InstanceRegister
  140. );
  141. return t_Result ;
  142. }
  143. HRESULT InitClassProvider ()
  144. {
  145. IUnknown *t_ClassFactory = new CClasProvClassFactory ;
  146. DWORD t_ClassContext = CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER ;
  147. DWORD t_Flags = REGCLS_MULTIPLEUSE ;
  148. HRESULT t_Result = CoRegisterClassObject (
  149. CLSID_CClasProvClassFactory,
  150. t_ClassFactory,
  151. t_ClassContext,
  152. t_Flags,
  153. &g_ClassRegister
  154. );
  155. return t_Result ;
  156. }
  157. HRESULT InitEventEncapsulatedProvider ()
  158. {
  159. IUnknown *t_ClassFactory = new CSNMPEncapEventProviderClassFactory ;
  160. DWORD t_ClassContext = CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER ;
  161. DWORD t_Flags = REGCLS_MULTIPLEUSE ;
  162. HRESULT t_Result = CoRegisterClassObject (
  163. CLSID_CSNMPEncapEventProviderClassFactory,
  164. t_ClassFactory,
  165. t_ClassContext,
  166. t_Flags,
  167. &g_EventEncapsulatedRegister
  168. );
  169. return t_Result ;
  170. }
  171. HRESULT InitEventReferentProvider ()
  172. {
  173. IUnknown *t_ClassFactory = new CSNMPRefEventProviderClassFactory ;
  174. DWORD t_ClassContext = CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER ;
  175. DWORD t_Flags = REGCLS_MULTIPLEUSE ;
  176. HRESULT t_Result = CoRegisterClassObject (
  177. CLSID_CSNMPReftEventProviderClassFactory,
  178. t_ClassFactory,
  179. t_ClassContext,
  180. t_Flags,
  181. &g_EventReferentRegister
  182. );
  183. return t_Result ;
  184. }
  185. HRESULT InitComServer ( DWORD a_AuthenticationLevel , DWORD a_ImpersonationLevel )
  186. {
  187. HRESULT t_Result = S_OK ;
  188. t_Result = CoInitializeEx (
  189. 0,
  190. COINIT_MULTITHREADED
  191. );
  192. if ( SUCCEEDED ( t_Result ) )
  193. {
  194. t_Result = CoInitializeSecurity (
  195. NULL,
  196. -1,
  197. NULL,
  198. NULL,
  199. a_AuthenticationLevel,
  200. a_ImpersonationLevel,
  201. NULL,
  202. EOAC_NONE,
  203. 0
  204. );
  205. if ( FAILED ( t_Result ) )
  206. {
  207. CoUninitialize () ;
  208. return t_Result ;
  209. }
  210. }
  211. if ( SUCCEEDED ( t_Result ) )
  212. t_Result = InitInstanceProvider () ;
  213. if ( SUCCEEDED ( t_Result ) )
  214. t_Result = InitClassProvider () ;
  215. if ( SUCCEEDED ( t_Result ) )
  216. t_Result = InitEventReferentProvider () ;
  217. if ( SUCCEEDED ( t_Result ) )
  218. t_Result = InitEventEncapsulatedProvider () ;
  219. if ( FAILED ( t_Result ) )
  220. {
  221. UninitComServer () ;
  222. }
  223. return t_Result ;
  224. }
  225. HRESULT Process ()
  226. {
  227. #if 1
  228. DWORD t_ImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE ;
  229. DWORD t_AuthenticationLevel = RPC_C_AUTHN_LEVEL_CONNECT ;
  230. #else
  231. DWORD t_ImpersonationLevel = RPC_C_IMP_LEVEL_IDENTITY ;
  232. DWORD t_AuthenticationLevel = RPC_C_AUTHN_LEVEL_NONE ;
  233. #endif
  234. HRESULT t_Result = InitComServer ( t_ImpersonationLevel , t_AuthenticationLevel ) ;
  235. if ( SUCCEEDED ( t_Result ) )
  236. {
  237. WindowsDispatch () ;
  238. UninitComServer () ;
  239. }
  240. return t_Result ;
  241. }
  242. BOOL ParseCommandLine ()
  243. {
  244. BOOL t_Exit = FALSE ;
  245. LPTSTR t_CommandLine = GetCommandLine () ;
  246. if ( t_CommandLine )
  247. {
  248. TCHAR *t_Arg = NULL ;
  249. TCHAR *t_ApplicationArg = NULL ;
  250. t_ApplicationArg = _tcstok ( t_CommandLine , _TEXT ( " \t" ) ) ;
  251. t_Arg = _tcstok ( NULL , _TEXT ( " \t" ) ) ;
  252. if ( t_Arg )
  253. {
  254. if ( _tcsicmp ( t_Arg , _TEXT ( "/RegServer" ) ) == 0 )
  255. {
  256. t_Exit = TRUE ;
  257. DllRegisterServer () ;
  258. }
  259. else if ( _tcsicmp ( t_Arg , _TEXT ( "/UnRegServer" ) ) == 0 )
  260. {
  261. t_Exit = TRUE ;
  262. DllUnregisterServer () ;
  263. }
  264. }
  265. }
  266. return t_Exit ;
  267. }
  268. int WINAPI WinMain (
  269. HINSTANCE hInstance, // handle to current instance
  270. HINSTANCE hPrevInstance, // handle to previous instance
  271. LPSTR lpCmdLine, // pointer to command line
  272. int nShowCmd // show state of window
  273. )
  274. {
  275. BOOL t_Status = DllMain (
  276. hInstance,
  277. DLL_PROCESS_ATTACH ,
  278. NULL
  279. ) ;
  280. BOOL t_Exit = ParseCommandLine () ;
  281. if ( ! t_Exit )
  282. {
  283. HWND hWnd = WindowsStart ( hInstance ) ;
  284. SnmpDebugLog :: Startup ();
  285. SnmpThreadObject :: Startup () ;
  286. HRESULT t_Result = Process () ;
  287. SnmpDebugLog :: Closedown () ;
  288. SnmpThreadObject :: Closedown () ;
  289. WindowsStop ( hWnd ) ;
  290. }
  291. t_Status = DllMain (
  292. hInstance,
  293. DLL_PROCESS_DETACH ,
  294. NULL
  295. ) ;
  296. return 0 ;
  297. }