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.

808 lines
19 KiB

  1. //***************************************************************************
  2. //
  3. // HMAGENT.CPP
  4. //
  5. // Module: HEALTHMON SERVER AGENT
  6. //
  7. // Purpose: Defines the entry point for the DLL application.
  8. //
  9. // Copyright (c)1999 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. //#include <windows.h>
  13. //#include <winnls.h>
  14. #include "hmagent.h"
  15. #include "system.h"
  16. #include "factory.h"
  17. #include <process.h>
  18. /////////////////////////////////////////////////////////////////////////////
  19. // Global data
  20. HMODULE g_hModule = NULL;
  21. HINSTANCE g_hResLib = NULL;
  22. HMODULE g_hWbemComnModule = NULL;
  23. long g_cComponents = 0;
  24. long g_cServerLocks = 0;
  25. HANDLE g_hConfigLock = NULL;
  26. CSystem* g_pSystem = NULL;
  27. CSystem* g_pStartupSystem = NULL;
  28. HANDLE g_hEvtReady = NULL; // ready to service COM requests
  29. CRITICAL_SECTION g_protect;
  30. HANDLE g_hThrdDie = NULL;
  31. HANDLE g_hThrdDead = NULL;
  32. //#ifdef _DEBUG
  33. FILE* debug_f;
  34. //#endif
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CLSIDs for HealthMon
  37. // HealthMon Instance Provider
  38. // {68AC0D34-DB09-11d2-8F56-006097919914}
  39. static const GUID CLSID_HMInstProvider =
  40. { 0x68ac0d34, 0xdb09, 0x11d2, { 0x8f, 0x56, 0x0, 0x60, 0x97, 0x91, 0x99, 0x14 } };
  41. // HealthMon Consumer
  42. // {68AC0D35-DB09-11d2-8F56-006097919914}
  43. static const GUID CLSID_HMConsumer =
  44. { 0x68ac0d35, 0xdb09, 0x11d2, { 0x8f, 0x56, 0x0, 0x60, 0x97, 0x91, 0x99, 0x14 } };
  45. // HealthMon System Event Provider
  46. // {68AC0D36-DB09-11d2-8F56-006097919914}
  47. static const GUID CLSID_HMSystemEventProvider =
  48. { 0x68ac0d36, 0xdb09, 0x11d2, { 0x8f, 0x56, 0x0, 0x60, 0x97, 0x91, 0x99, 0x14 } };
  49. // HealthMon DataGroup Event Provider
  50. // {68AC0D37-DB09-11d2-8F56-006097919914}
  51. static const GUID CLSID_HMDataGroupEventProvider =
  52. { 0x68ac0d37, 0xdb09, 0x11d2, { 0x8f, 0x56, 0x0, 0x60, 0x97, 0x91, 0x99, 0x14 } };
  53. // HealthMon DataCollector Event Provider
  54. // {68AC0D38-DB09-11d2-8F56-006097919914}
  55. static const GUID CLSID_HMDataCollectorEventProvider =
  56. { 0x68ac0d38, 0xdb09, 0x11d2, { 0x8f, 0x56, 0x0, 0x60, 0x97, 0x91, 0x99, 0x14 } };
  57. // HealthMon DataCollector PerInstance Event Provider
  58. // {3A7A82DC-8D5C-4ab7-801B-A1C7D30089C6}
  59. static const GUID CLSID_HMDataCollectorPerInstanceEventProvider =
  60. { 0x3a7a82dc, 0x8d5c, 0x4ab7, { 0x80, 0x1b, 0xa1, 0xc7, 0xd3, 0x0, 0x89, 0xc6 } };
  61. // HealthMon DataCollectorStatistics Event Provider
  62. // {68AC0D40-DB09-11d2-8F56-006097919914}
  63. //static const GUID CLSID_HMDataCollectorStatisticsEventProvider =
  64. //{ 0x68ac0d40, 0xdb09, 0x11d2, { 0x8f, 0x56, 0x0, 0x60, 0x97, 0x91, 0x99, 0x14 } };
  65. // HealthMon Threshold Event Provider
  66. // {68AC0D39-DB09-11d2-8F56-006097919914}
  67. static const GUID CLSID_HMThresholdEventProvider =
  68. { 0x68ac0d39, 0xdb09, 0x11d2, { 0x8f, 0x56, 0x0, 0x60, 0x97, 0x91, 0x99, 0x14 } };
  69. // HealthMon Method Provider
  70. // {68AC0D41-DB09-11d2-8F56-006097919914}
  71. static const GUID CLSID_HMMethProvider =
  72. { 0x68ac0d41, 0xdb09, 0x11d2, { 0x8f, 0x56, 0x0, 0x60, 0x97, 0x91, 0x99, 0x14 } };
  73. // HealthMon ThresholdInstance Event Provider
  74. // {68AC0D42-DB09-11d2-8F56-006097919914}
  75. //static const GUID CLSID_HMThresholdInstanceEventProvider =
  76. //{ 0x68ac0d42, 0xdb09, 0x11d2, { 0x8f, 0x56, 0x0, 0x60, 0x97, 0x91, 0x99, 0x14 } };
  77. // HealthMon Action Event Provider
  78. // {68AC0D43-DB09-11d2-8F56-006097919914}
  79. static const GUID CLSID_HMActionEventProvider =
  80. { 0x68ac0d43, 0xdb09, 0x11d2, { 0x8f, 0x56, 0x0, 0x60, 0x97, 0x91, 0x99, 0x14 } };
  81. // HealthMon Action Trigger Event Provider
  82. // {68AC0D44-DB09-11d2-8F56-006097919914}
  83. static const GUID CLSID_HMActionTriggerEventProvider =
  84. { 0x68ac0d44, 0xdb09, 0x11d2, { 0x8f, 0x56, 0x0, 0x60, 0x97, 0x91, 0x99, 0x14 } };
  85. //static unsigned int __stdcall CheckFinalInit(void *pv);
  86. unsigned int __stdcall CheckFinalInit(void *pv);
  87. /////////////////////////////////////////////////////////////////////////////
  88. // DLL Entry Point
  89. extern "C"
  90. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  91. {
  92. if (dwReason == DLL_PROCESS_ATTACH)
  93. {
  94. debug_f = _tfopen(L"hmdebug.log", L"a+");
  95. if(!debug_f)
  96. {
  97. OutputDebugString(L"Impossible to open log file");
  98. }
  99. else // write a start line in the debug log file
  100. {
  101. _ftprintf(debug_f, HM_START_LINE);
  102. fflush(debug_f);
  103. }
  104. g_hModule = hInstance;
  105. MY_OUTPUT(L"DLLMAIN->DLL_PROCESS_ATTACH", 1);
  106. DisableThreadLibraryCalls(hInstance);
  107. __try
  108. {
  109. InitializeCriticalSection(&g_protect);
  110. }
  111. __except(EXCEPTION_EXECUTE_HANDLER)
  112. {
  113. OutputDebugString(L"Impossible to initialize critical section");
  114. MY_ASSERT(FALSE);
  115. return FALSE;
  116. }
  117. if (Initialize())
  118. {
  119. return TRUE;
  120. }
  121. else
  122. {
  123. MY_ASSERT(FALSE);
  124. return FALSE;
  125. }
  126. }
  127. else if (dwReason == DLL_PROCESS_DETACH)
  128. {
  129. MY_OUTPUT(L"DLLMAIN->DLL_PROCESS_DETACH", 1);
  130. UnInitialize(); // even if it fails, try to unitialize the rest anyway
  131. DeleteCriticalSection(&g_protect);
  132. if(debug_f)
  133. {
  134. fclose(debug_f);
  135. }
  136. return TRUE;
  137. }
  138. return TRUE;
  139. }
  140. STDAPI DllGetClassObject(REFCLSID clsid, REFIID iid, void** ppv)
  141. {
  142. unsigned uThrdId = 0;
  143. HANDLE hThrdFn = NULL;
  144. HRESULT hRes = S_OK;
  145. CBaseFactory* pFactory = NULL;
  146. static BOOL bFirstTime = TRUE;
  147. DWORD dwErr = 0;
  148. BOOL bGotCritSec = FALSE;
  149. if (clsid == CLSID_HMInstProvider ||
  150. clsid == CLSID_HMMethProvider ||
  151. clsid == CLSID_HMConsumer)
  152. {
  153. try
  154. {
  155. EnterCriticalSection(&g_protect);
  156. }
  157. catch(...)
  158. {
  159. return E_OUTOFMEMORY;
  160. }
  161. bGotCritSec = TRUE;
  162. //
  163. // Initialize HealthMon()
  164. //
  165. if (!g_pStartupSystem)
  166. {
  167. if (bFirstTime)
  168. {
  169. if(!ResetEvent(g_hEvtReady)) // System is Not yet ready!
  170. {
  171. dwErr = GetLastError();
  172. MY_OUTPUT2(L"ResetEvent failed with error %d",dwErr, 4);
  173. hRes = HRESULT_FROM_WIN32(dwErr);
  174. goto ExitPoint;
  175. }
  176. hThrdFn = (HANDLE)_beginthreadex(NULL, 0, CheckFinalInit, NULL, 0, &uThrdId);
  177. if (!hThrdFn)
  178. {
  179. MY_OUTPUT(L"ERROR creating a thread", 1);
  180. hRes = E_UNEXPECTED;
  181. goto ExitPoint;
  182. }
  183. bFirstTime = FALSE;
  184. }
  185. if (InitializeHealthMon())
  186. {
  187. MY_OUTPUT(L"DllGetClassObject: HealthMon Initialized", 1);
  188. if(!SetEvent(g_hEvtReady)) // System is ready.
  189. {
  190. dwErr = GetLastError();
  191. MY_OUTPUT2(L"ResetEvent failed with error %d",dwErr, 4);
  192. hRes = HRESULT_FROM_WIN32(dwErr);
  193. goto ExitPoint;
  194. }
  195. }
  196. else
  197. {
  198. MY_ASSERT(FALSE);
  199. MY_OUTPUT(L"DllGetClassObject: FAILURE TO INITIALIZE HealthMon", 1);
  200. hRes = E_UNEXPECTED;
  201. goto ExitPoint;
  202. }
  203. }
  204. // initialization went fine.
  205. LeaveCriticalSection(&g_protect);
  206. bGotCritSec = FALSE;
  207. }
  208. else
  209. {
  210. // nothing to initialize
  211. }
  212. //
  213. // Create Class Factories for Providers
  214. //
  215. if (clsid == CLSID_HMInstProvider)
  216. {
  217. pFactory = new CInstProvFactory;
  218. }
  219. else if (clsid == CLSID_HMMethProvider)
  220. {
  221. pFactory = new CMethProvFactory;
  222. }
  223. else if (clsid == CLSID_HMSystemEventProvider)
  224. {
  225. pFactory = new CSystemEvtProvFactory;
  226. }
  227. else if (clsid == CLSID_HMDataGroupEventProvider)
  228. {
  229. pFactory = new CDataGroupEvtProvFactory;
  230. }
  231. else if (clsid == CLSID_HMDataCollectorEventProvider)
  232. {
  233. pFactory = new CDataCollectorEvtProvFactory;
  234. }
  235. else if (clsid == CLSID_HMDataCollectorPerInstanceEventProvider)
  236. {
  237. pFactory = new CDataCollectorPerInstanceEvtProvFactory;
  238. }
  239. else if (clsid == CLSID_HMThresholdEventProvider)
  240. {
  241. pFactory = new CThresholdEvtProvFactory;
  242. }
  243. else if (clsid == CLSID_HMActionEventProvider)
  244. {
  245. pFactory = new CActionEvtProvFactory;
  246. }
  247. else if (clsid == CLSID_HMActionTriggerEventProvider)
  248. {
  249. pFactory = new CActionTriggerEvtProvFactory;
  250. }
  251. else if (clsid == CLSID_HMConsumer)
  252. {
  253. pFactory = new CConsFactory;
  254. }
  255. else
  256. {
  257. MY_ASSERT(FALSE);
  258. return CLASS_E_CLASSNOTAVAILABLE;
  259. }
  260. if (pFactory ==NULL)
  261. {
  262. MY_OUTPUT(L"DllGetClassObject: ERROR:pFactory is NULL", 1);
  263. return E_OUTOFMEMORY;
  264. }
  265. hRes = pFactory->QueryInterface(iid, ppv);
  266. if (FAILED(hRes))
  267. {
  268. // MY_HRESASSERT(hRes);
  269. MY_OUTPUT(L"DllGetClassObject: ERROR:QI failed", 1);
  270. pFactory->Release();
  271. }
  272. ExitPoint:
  273. if(bGotCritSec)
  274. {
  275. LeaveCriticalSection(&g_protect);
  276. }
  277. return hRes;
  278. }
  279. unsigned int __stdcall CheckFinalInit(void *pv)
  280. {
  281. DWORD dwError = 0;
  282. // We will get the Mutex first here, and block everyone else from
  283. // proceeding untill fully initialized.
  284. MY_OUTPUT(L"BLOCK - BLOCK Consumer::Update BLOCK - g_hConfigLock BLOCK WAIT", 1);
  285. if(WaitForSingleObject(g_hConfigLock, INFINITE) != WAIT_OBJECT_0)
  286. {
  287. // error!
  288. MY_OUTPUT(L"WaitForSingleObject failed",1);
  289. dwError = GetLastError();
  290. goto Exit;
  291. }
  292. MY_OUTPUT(L"BLOCK - BLOCK Consumer::Update BLOCK - g_hConfigLock BLOCK GOT IT", 1);
  293. if(WaitForSingleObject(g_hEvtReady, INFINITE) != WAIT_OBJECT_0)
  294. {
  295. // error!
  296. MY_OUTPUT(L"WaitForSingleObject failed",1);
  297. dwError = GetLastError();
  298. goto Exit;
  299. }
  300. try
  301. {
  302. g_pSystem = g_pStartupSystem;
  303. g_pSystem->OnAgentInterval();
  304. }
  305. catch (...)
  306. {
  307. MY_ASSERT(FALSE);
  308. }
  309. MY_OUTPUT(L"BLOCK - BLOCK Consumer::Update g_hConfigLock BLOCK - RELEASE IT", 1);
  310. if(!ReleaseMutex(g_hConfigLock))
  311. {
  312. dwError = GetLastError();
  313. MY_OUTPUT2(L"ReleaseMutex failed with error %d",dwError, 4);
  314. }
  315. MY_OUTPUT(L"BLOCK - BLOCK Consumer::Update g_hConfigLock BLOCK - RELEASED", 1);
  316. Exit:
  317. _endthreadex(0);
  318. return dwError;
  319. }
  320. STDAPI DllCanUnloadNow()
  321. {
  322. if(g_cServerLocks == 0 && g_cComponents == 0)
  323. {
  324. return S_OK;
  325. }
  326. else
  327. {
  328. return S_FALSE;
  329. }
  330. }
  331. STDAPI DllRegisterServer()
  332. {
  333. HRESULT hRes;
  334. wchar_t szModule[_MAX_PATH];
  335. MY_OUTPUT(L"DllRegisterServer", 1);
  336. // Get module file name
  337. //DebugBreak();
  338. //MY_ASSERT(FALSE);
  339. if(!GetModuleFileNameW(g_hModule, szModule, _MAX_PATH))
  340. {
  341. return HRESULT_FROM_WIN32(GetLastError());
  342. }
  343. // Register Instance Provider
  344. hRes = Register(szModule, L"HealthMon System Instance Provider", L"Both", CLSID_HMInstProvider);
  345. if (FAILED(hRes))
  346. return hRes;
  347. // Register Method Provider
  348. hRes = Register(szModule, L"HealthMon System Method Provider", L"Both", CLSID_HMMethProvider);
  349. if (FAILED(hRes))
  350. return hRes;
  351. // Register System Event Provider
  352. hRes = Register(szModule, L"HealthMon System Event Provider", L"Both", CLSID_HMSystemEventProvider);
  353. if (FAILED(hRes))
  354. return hRes;
  355. // Register DataGroup Event Provider
  356. hRes = Register(szModule, L"HealthMon DataGroup Event Provider", L"Both", CLSID_HMDataGroupEventProvider);
  357. if (FAILED(hRes))
  358. return hRes;
  359. // Register DataCollector Event Provider
  360. hRes = Register(szModule, L"HealthMon DataCollector Event Provider", L"Both", CLSID_HMDataCollectorEventProvider);
  361. if (FAILED(hRes))
  362. return hRes;
  363. // Register DataCollector Per Instance Event Provider
  364. hRes = Register(szModule, L"HealthMon DataCollector Per Instance Event Provider", L"Both", CLSID_HMDataCollectorPerInstanceEventProvider);
  365. if (FAILED(hRes))
  366. return hRes;
  367. // Register DataCollectorStatistics Event Provider
  368. // hRes = Register(szModule, L"HealthMon DataCollectorStatistics Event Provider", L"Both", CLSID_HMDataCollectorStatisticsEventProvider);
  369. // if (FAILED(hRes))
  370. // return hRes;
  371. // Register Threshold Event Provider
  372. hRes = Register(szModule, L"HealthMon Threshold Event Provider", L"Both", CLSID_HMThresholdEventProvider);
  373. if (FAILED(hRes))
  374. return hRes;
  375. // Register ThresholdInstance Event Provider
  376. // hRes = Register(szModule, L"HealthMon ThresholdInstance Event Provider", L"Both", CLSID_HMThresholdInstanceEventProvider);
  377. // if (FAILED(hRes))
  378. // return hRes;
  379. // Register Action Event Provider
  380. hRes = Register(szModule, L"HealthMon Action Event Provider", L"Both", CLSID_HMActionEventProvider);
  381. if (FAILED(hRes))
  382. return hRes;
  383. // Register Action Trigger Event Provider
  384. hRes = Register(szModule, L"HealthMon Action Trigger Event Provider", L"Both", CLSID_HMActionTriggerEventProvider);
  385. if (FAILED(hRes))
  386. return hRes;
  387. //Register consumer
  388. hRes = Register(szModule, L"HealthMon Event Consumer", L"Both", CLSID_HMConsumer);
  389. if (FAILED(hRes))
  390. return hRes;
  391. MY_OUTPUT(L"DllRegisterServer - OK", 1);
  392. return S_OK;
  393. }
  394. STDAPI DllUnregisterServer()
  395. {
  396. HRESULT hRes;
  397. hRes = UnRegister(CLSID_HMInstProvider);
  398. if (FAILED(hRes))
  399. return hRes;
  400. hRes = UnRegister(CLSID_HMMethProvider);
  401. if (FAILED(hRes))
  402. return hRes;
  403. hRes = UnRegister(CLSID_HMSystemEventProvider);
  404. if (FAILED(hRes))
  405. return hRes;
  406. hRes = UnRegister(CLSID_HMDataGroupEventProvider);
  407. if (FAILED(hRes))
  408. return hRes;
  409. hRes = UnRegister(CLSID_HMDataCollectorEventProvider);
  410. if (FAILED(hRes))
  411. return hRes;
  412. hRes = UnRegister(CLSID_HMDataCollectorPerInstanceEventProvider);
  413. if (FAILED(hRes))
  414. return hRes;
  415. // hRes = UnRegister(CLSID_HMDataCollectorStatisticsEventProvider);
  416. // if (FAILED(hRes))
  417. // return hRes;
  418. hRes = UnRegister(CLSID_HMThresholdEventProvider);
  419. if (FAILED(hRes))
  420. return hRes;
  421. // hRes = UnRegister(CLSID_HMThresholdInstanceEventProvider);
  422. // if (FAILED(hRes))
  423. // return hRes;
  424. hRes = UnRegister(CLSID_HMActionEventProvider);
  425. if (FAILED(hRes))
  426. return hRes;
  427. hRes = UnRegister(CLSID_HMActionTriggerEventProvider);
  428. if (FAILED(hRes))
  429. return hRes;
  430. hRes = UnRegister(CLSID_HMConsumer);
  431. if (FAILED(hRes))
  432. return hRes;
  433. return S_OK;
  434. }
  435. HRESULT Register(const wchar_t* szModule, const wchar_t* szName, const wchar_t* szThreading, REFCLSID clsid)
  436. {
  437. wchar_t szKey[_MAX_PATH];
  438. wchar_t* pGuidStr = 0;
  439. HKEY hKey;
  440. HKEY hSubKey;
  441. HRESULT hRes;
  442. // pass CLSID
  443. hRes = StringFromCLSID(clsid, &pGuidStr);
  444. if (hRes != S_OK)
  445. {
  446. if (pGuidStr)
  447. {
  448. CoTaskMemFree(pGuidStr);
  449. }
  450. return hRes;
  451. }
  452. swprintf(szKey, L"SOFTWARE\\CLASSES\\CLSID\\%s", pGuidStr);
  453. CoTaskMemFree(pGuidStr);
  454. LONG lRet = RegCreateKeyW(HKEY_LOCAL_MACHINE, szKey, &hKey);
  455. if (lRet != ERROR_SUCCESS)
  456. return E_FAIL;
  457. lRet = RegSetValueExW(hKey, 0, 0, REG_SZ, (const BYTE *) szName, wcslen(szName) * 2 + 2);
  458. if (lRet != ERROR_SUCCESS)
  459. return E_FAIL;
  460. lRet = RegCreateKey(hKey, L"InprocServer32", &hSubKey);
  461. if (lRet != ERROR_SUCCESS)
  462. return E_FAIL;
  463. lRet = RegSetValueExW(hSubKey, 0, 0, REG_SZ, (const BYTE *) szModule, wcslen(szModule) * 2 + 2);
  464. if (lRet != ERROR_SUCCESS)
  465. return E_FAIL;
  466. lRet = RegSetValueExW(hSubKey, L"ThreadingModel", 0, REG_SZ, (const BYTE *) szThreading, wcslen(szThreading) * 2 + 2);
  467. if (lRet != ERROR_SUCCESS)
  468. return E_FAIL;
  469. lRet = RegCloseKey(hSubKey);
  470. if (lRet != ERROR_SUCCESS)
  471. return E_FAIL;
  472. lRet = RegCloseKey(hKey);
  473. if (lRet != ERROR_SUCCESS)
  474. return E_FAIL;
  475. return S_OK;
  476. }
  477. HRESULT UnRegister(REFCLSID clsid)
  478. {
  479. wchar_t* pGuidStr = 0;
  480. wchar_t szKeyPath[_MAX_PATH];
  481. HRESULT hRes;
  482. HKEY hKey;
  483. hRes = StringFromCLSID(clsid, &pGuidStr);
  484. if (hRes != S_OK)
  485. {
  486. if (pGuidStr)
  487. CoTaskMemFree(pGuidStr);
  488. return hRes;
  489. }
  490. swprintf(szKeyPath, L"SOFTWARE\\CLASSES\\CLSID\\%s", pGuidStr);
  491. // Delete InProcServer32 subkey
  492. LONG lRet = RegOpenKeyW(HKEY_LOCAL_MACHINE, szKeyPath, &hKey);
  493. if (lRet != ERROR_SUCCESS)
  494. return E_FAIL;
  495. lRet = RegDeleteKeyW(hKey, L"InprocServer32");
  496. if (lRet != ERROR_SUCCESS)
  497. return E_FAIL;
  498. lRet = RegCloseKey(hKey);
  499. if (lRet != ERROR_SUCCESS)
  500. return E_FAIL;
  501. // Delete CLSID GUID key
  502. lRet = RegOpenKeyW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\CLASSES\\CLSID", &hKey);
  503. if (lRet != ERROR_SUCCESS)
  504. return E_FAIL;
  505. lRet = RegDeleteKeyW(hKey, pGuidStr);
  506. if (lRet != ERROR_SUCCESS)
  507. return E_FAIL;
  508. lRet = RegCloseKey(hKey);
  509. if (lRet != ERROR_SUCCESS)
  510. return E_FAIL;
  511. return S_OK;
  512. }
  513. BOOL Initialize()
  514. {
  515. WIN32_FIND_DATA FindFileData;
  516. HANDLE hFind;
  517. wchar_t szModule[_MAX_PATH];
  518. wchar_t szPath[_MAX_PATH];
  519. wchar_t szDefPath[_MAX_PATH];
  520. wchar_t szDir[_MAX_PATH];
  521. wchar_t szTemp[_MAX_PATH];
  522. LCID lcID;
  523. LCID dirlcID;
  524. BOOL bFound = FALSE;
  525. //
  526. // Load the resource file for the system local
  527. //
  528. DWORD dwRes = GetModuleFileNameW(g_hModule, szModule, _MAX_PATH);
  529. if(!dwRes)
  530. {
  531. dwRes = GetLastError();
  532. MY_OUTPUT2(L"GetModuleFileName failed: %d",dwRes,4);
  533. return FALSE;
  534. // dbg output
  535. }
  536. /*
  537. _tsplitpath(szModule, szPath, szDir, NULL, NULL);
  538. lstrcat(szPath, szDir);
  539. lstrcpy(szDefPath, szPath);
  540. pStr = wcsrchr(szPath, '\\');
  541. *pStr = '\0';
  542. pStr = wcsrchr(szPath, '\\');
  543. *pStr = '\0';
  544. lstrcpy(szPath, L"WBEMCOMN.DLL");*/
  545. g_hWbemComnModule = LoadLibrary(L"WBEMCOMN.DLL");
  546. if(!g_hWbemComnModule)
  547. {
  548. dwRes = GetLastError();
  549. MY_OUTPUT2(L"LoadLibrary failed: %d",dwRes,4);
  550. return FALSE;
  551. }
  552. _tsplitpath(szModule, szPath, szDir, NULL, NULL);
  553. lstrcat(szPath, szDir);
  554. lstrcpy(szDefPath, szPath);
  555. lcID = GetSystemDefaultLCID();
  556. swprintf(szTemp, L"%08x\\HMonitorRes.dll", lcID);
  557. lstrcat(szPath, szTemp);
  558. if ((g_hResLib = LoadLibrary(szPath)) == NULL)
  559. {
  560. MY_ASSERT(FALSE);
  561. // Couldn't find what we think is the default language, so search for one that matches the
  562. // primary ID.
  563. szPath[0] = '\0';
  564. lstrcpy(szPath, szDefPath);
  565. lstrcat(szPath, L"0*");
  566. hFind = FindFirstFile(szPath, &FindFileData);
  567. while (hFind != INVALID_HANDLE_VALUE)
  568. {
  569. if (FindFileData.dwFileAttributes | FILE_ATTRIBUTE_DIRECTORY)
  570. {
  571. dirlcID = wcstoul(FindFileData.cFileName, NULL, 16);
  572. if (PRIMARYLANGID(lcID) == PRIMARYLANGID(dirlcID))
  573. {
  574. lstrcpy(szTemp, szDefPath);
  575. lstrcat(szTemp, FindFileData.cFileName);
  576. lstrcat(szTemp, L"\\HMonitorRes.dll");
  577. g_hResLib = LoadLibrary(szTemp);
  578. if (g_hResLib != NULL)
  579. {
  580. bFound = TRUE;
  581. FindClose(hFind);
  582. break;
  583. }
  584. else
  585. {
  586. MY_ASSERT(FALSE);
  587. dwRes = GetLastError();
  588. }
  589. }
  590. }
  591. if (!FindNextFile(hFind, &FindFileData))
  592. {
  593. FindClose(hFind);
  594. break;
  595. }
  596. }
  597. if (bFound == FALSE)
  598. {
  599. // default case.
  600. lstrcpy(szTemp, szDefPath);
  601. lstrcat(szTemp, L"00000409\\HMonitorRes.dll");
  602. g_hResLib = LoadLibrary(szTemp);
  603. if (g_hResLib == NULL)
  604. {
  605. MY_ASSERT(FALSE);
  606. dwRes = GetLastError();
  607. }
  608. }
  609. }
  610. //
  611. // Create the objects needed for thread syncronization
  612. //
  613. g_hEvtReady = CreateEvent(NULL, TRUE, TRUE, NULL); // ready to service COM requests
  614. if (!g_hEvtReady)
  615. return FALSE;
  616. g_hConfigLock = CreateMutex(NULL, FALSE, NULL);
  617. if (!g_hConfigLock)
  618. return FALSE;
  619. g_hThrdDie = CreateEvent(NULL, TRUE, FALSE, NULL);
  620. if (!g_hThrdDie)
  621. return FALSE;
  622. g_hThrdDead = CreateEvent(NULL, TRUE, FALSE, NULL);
  623. if (!g_hThrdDead)
  624. return FALSE;
  625. // artificially increment the lock count
  626. InterlockedIncrement(&g_cServerLocks);
  627. return TRUE;
  628. }
  629. void UnInitialize()
  630. {
  631. if (g_hResLib)
  632. FreeLibrary(g_hResLib);
  633. if (g_hEvtReady)
  634. CloseHandle(g_hEvtReady);
  635. if (g_hConfigLock)
  636. CloseHandle(g_hConfigLock);
  637. if (g_hThrdDie)
  638. CloseHandle(g_hThrdDie);
  639. if (g_hThrdDead)
  640. CloseHandle(g_hThrdDead);
  641. if(g_hWbemComnModule)
  642. FreeLibrary(g_hWbemComnModule);
  643. }
  644. BOOL InitializeHealthMon()
  645. {
  646. CSystem* pSystem = NULL;
  647. // don't forget to remove the CEvent log creation from the System constructor
  648. //XXXDebugBreak();
  649. OutputDebugString(L"InitializeHealthMon()\n");
  650. try
  651. {
  652. pSystem = new CSystem;
  653. if (!pSystem->InitWbemPointer())
  654. {
  655. OutputDebugString(L"InitializeHealthMon() - Failed to init Wbem pointers!\n");
  656. delete pSystem;
  657. pSystem = NULL;
  658. return FALSE;
  659. }
  660. //XXXDebug and make sure this first test works
  661. if (pSystem->InternalizeHMNamespace()!=S_OK)
  662. {
  663. OutputDebugString(L"InitializeHealthMon() - Failed to init Status!\n");
  664. // Log to EventLog
  665. #ifdef SAVE
  666. pSystem->m_pLog->LogEvent(0,0,EVENTLOG_ID_FATAL_ERROR,0,NULL);
  667. #endif
  668. delete pSystem;
  669. pSystem = NULL;
  670. return FALSE;
  671. }
  672. // Log to NT Event Log that HealthMon is started.
  673. #ifdef SAVE
  674. pSystem->m_pLog->LogEvent(0,0,EVENTLOG_ID_STARTED,0,NULL);
  675. #endif
  676. // g_pSystem = pSystem;
  677. g_pStartupSystem = pSystem;
  678. }
  679. catch (...)
  680. {
  681. MY_ASSERT(FALSE);
  682. //XXXSee if can put in a log to NTEvent Log is think it is a ggod idea still
  683. //from the catch points, of from MY_ASSERT???
  684. //LogEvent(0,0,EVENTLOG_ID_STARTED,0,NULL);
  685. return FALSE;
  686. }
  687. return TRUE;
  688. }