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.

1547 lines
46 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1997 - 1999
  3. Module Name:
  4. event.cxx
  5. Abstract:
  6. SENS code related to firing Events using LCE mechanism.
  7. Author:
  8. Gopal Parupudi <GopalP>
  9. [Notes:]
  10. optional-notes
  11. Revision History:
  12. GopalP 10/31/1997 Start.
  13. --*/
  14. #include <precomp.hxx>
  15. //
  16. // Some useful Macros
  17. //
  18. /*++
  19. Macro Description:
  20. Helper Macro for firing Winlogon events.
  21. Arguments:
  22. See signature.
  23. --*/
  24. #define FIRE_WINLOGON_EVENT(_EVENT_NAME_, _EVENT_TYPE_) \
  25. { \
  26. PSENSEVENT_WINLOGON pData = (PSENSEVENT_WINLOGON)EventData; \
  27. WCHAR buffer[CRED_MAX_USERNAME_LENGTH]; \
  28. \
  29. *buffer = 0; \
  30. \
  31. SensPrint(SENS_INFO, (SENS_STRING("\t| WINLOGON Event\n"))); \
  32. SensPrint(SENS_INFO, (SENS_STRING("\t|\n\t| Type - %s\n"), _EVENT_NAME_)); \
  33. SensPrint(SENS_INFO, (SENS_STRING("\t| Size - %d\n"), pData->Info.Size)); \
  34. SensPrint(SENS_INFO, (SENS_STRING("\t| Flags - 0x%x\n"), pData->Info.Flags)); \
  35. SensPrintW(SENS_INFO, (SENS_BSTR("\t| UserName - %s\n"), \
  36. pData->Info.UserName ? pData->Info.UserName : SENS_BSTR(""))); \
  37. SensPrintW(SENS_INFO, (SENS_BSTR("\t| Domain - %s\n"), \
  38. pData->Info.Domain ? pData->Info.Domain : SENS_BSTR(""))); \
  39. SensPrintW(SENS_INFO, (SENS_BSTR("\t| WinStation - %s\n"), \
  40. pData->Info.WindowStation ? pData->Info.WindowStation : SENS_BSTR(""))); \
  41. SensPrint(SENS_INFO, (SENS_STRING("\t| hToken - 0x%x\n"), pData->Info.hToken)); \
  42. SensPrint(SENS_INFO, (SENS_STRING("\t| hDesktop - 0x%x\n"), pData->Info.hDesktop)); \
  43. SensPrint(SENS_INFO, (SENS_STRING("\t| dwSessionId - 0x%x\n"), pData->Info.dwSessionId)); \
  44. \
  45. if (pData->Info.Domain != NULL) \
  46. { \
  47. StringCchCopy(buffer, CRED_MAX_USERNAME_LENGTH, pData->Info.Domain); \
  48. StringCchCat(buffer, CRED_MAX_USERNAME_LENGTH, SENS_BSTR("\\")); \
  49. } \
  50. if (pData->Info.UserName != NULL) \
  51. { \
  52. StringCchCat(buffer, CRED_MAX_USERNAME_LENGTH, pData->Info.UserName); \
  53. } \
  54. SensPrintW(SENS_INFO, (SENS_BSTR("\t| UserName passed is - %s\n"), buffer)); \
  55. hr = SensFireWinlogonEventHelper(buffer, pData->Info.dwSessionId, _EVENT_TYPE_); \
  56. \
  57. break; \
  58. }
  59. /*++
  60. Macro Description:
  61. Helper Macro for firing PnP events.
  62. Arguments:
  63. See signature.
  64. Notes:
  65. a. This is not an actual event exposed by SENS.
  66. --*/
  67. #define FIRE_PNP_EVENT(_EVENT_NAME_) \
  68. { \
  69. PSENSEVENT_PNP pData = (PSENSEVENT_PNP)EventData; \
  70. \
  71. SensPrint(SENS_INFO, (SENS_STRING("\t| PNP Event\n"))); \
  72. SensPrint(SENS_INFO, (SENS_STRING("\t|\n\t| Type - DEVICE %s\n"), _EVENT_NAME_)); \
  73. SensPrint(SENS_INFO, (SENS_STRING("\t| Size - %d\n"), pData->Size)); \
  74. SensPrint(SENS_INFO, (SENS_STRING("\t| DevType - %d\n"), pData->DevType)); \
  75. SensPrint(SENS_INFO, (SENS_STRING("\t| Resource - 0x%x\n"), pData->Resource)); \
  76. SensPrint(SENS_INFO, (SENS_STRING("\t| Flags - 0x%x\n"), pData->Flags)); \
  77. \
  78. break; \
  79. }
  80. /*++
  81. Macro Description:
  82. Helper Macro for firing Power events.
  83. Arguments:
  84. See signature.
  85. --*/
  86. #define FIRE_POWER_EVENT(_EVENT_NAME_, _EVENT_TYPE_) \
  87. { \
  88. PSENSEVENT_POWER pData = (PSENSEVENT_POWER)EventData; \
  89. \
  90. SensPrint(SENS_INFO, (SENS_STRING("\t| POWER MANAGEMENT Event\n"))); \
  91. SensPrint(SENS_INFO, (SENS_STRING("\t|\n\t| Type - %s\n"), _EVENT_NAME_)); \
  92. SensPrint(SENS_INFO, (SENS_STRING("\t| ACLineStatus - %d\n"), \
  93. pData->PowerStatus.ACLineStatus)); \
  94. SensPrint(SENS_INFO, (SENS_STRING("\t| BatteryFlag - %d\n"), \
  95. pData->PowerStatus.BatteryFlag)); \
  96. SensPrint(SENS_INFO, (SENS_STRING("\t| BatteryLifePercent - %d\n"), \
  97. pData->PowerStatus.BatteryLifePercent)); \
  98. SensPrint(SENS_INFO, (SENS_STRING("\t| BatteryLifeTime - 0x%x secs\n"), \
  99. pData->PowerStatus.BatteryLifeTime)); \
  100. SensPrint(SENS_INFO, (SENS_STRING("\t| BatteryFullLifeTime - 0x%x secs\n"), \
  101. pData->PowerStatus.BatteryFullLifeTime)); \
  102. hr = SensFirePowerEventHelper(pData->PowerStatus, _EVENT_TYPE_); \
  103. break; \
  104. }
  105. /*++
  106. Macro Description:
  107. Helper Macro for firing RAS events.
  108. Arguments:
  109. See signature.
  110. Notes:
  111. a. This is not an actual event exposed by SENS. It may, however, generate
  112. a WAN Connectivity event.
  113. --*/
  114. #define FIRE_RAS_EVENT(_EVENT_NAME_) \
  115. { \
  116. \
  117. PSENSEVENT_RAS pData = (PSENSEVENT_RAS)EventData; \
  118. \
  119. SensPrint(SENS_INFO, (SENS_STRING("\t| RAS Event\n"))); \
  120. SensPrint(SENS_INFO, (SENS_STRING("\t|\n\t| Type - %s\n"), _EVENT_NAME_)); \
  121. SensPrint(SENS_INFO, (SENS_STRING("\t| Connection Handle - 0x%x\n"), pData->hConnection)); \
  122. break; \
  123. }
  124. /*++
  125. Macro Description:
  126. Helper Macro for firing LAN events.
  127. Arguments:
  128. See signature.
  129. Notes:
  130. a. This is not an actual event exposed by SENS. It may, however, generate
  131. a LAN Connectivity event.
  132. --*/
  133. #define FIRE_LAN_EVENT(_EVENT_NAME_) \
  134. \
  135. { \
  136. PSENSEVENT_LAN pData = (PSENSEVENT_LAN)EventData; \
  137. \
  138. SensPrint(SENS_INFO, (SENS_STRING("\t| LAN Event\n"))); \
  139. SensPrint(SENS_INFO, (SENS_STRING("\t|\n\t| Type - %s\n"), _EVENT_NAME_)); \
  140. SensPrint(SENS_INFO, (SENS_STRING("\t|\n\t| Connection Name - %s\n"), pData->Name)); \
  141. SensPrint(SENS_INFO, (SENS_STRING("\t|\n\t| Status - 0x%x\n"), pData->Status)); \
  142. SensPrint(SENS_INFO, (SENS_STRING("\t|\n\t| Type - 0x%x\n"), pData->Type)); \
  143. break; \
  144. }
  145. /*++
  146. Macro Description:
  147. This macro is called when we allocate the EventData so that it can be
  148. queued to a worker thread. ALLOCATE_END() should be called to signal the
  149. end of the allocation.
  150. Arguments:
  151. See signature.
  152. Notes:
  153. a. pData, pTempData and pReturnData are fixed names for the variable.
  154. They should not be changed without updating the code that uses
  155. this macro.
  156. --*/
  157. #define ALLOCATE_BEGIN(_EVENT_STRUCT_) \
  158. \
  159. _EVENT_STRUCT_ *pData, *pTempData; \
  160. \
  161. /* Allocate the Data structure */ \
  162. pTempData = (_EVENT_STRUCT_ *) EventData; \
  163. pData = (_EVENT_STRUCT_ *) new char[sizeof(_EVENT_STRUCT_)]; \
  164. if (NULL == pData) \
  165. { \
  166. goto Cleanup; \
  167. } \
  168. \
  169. memcpy(pData, EventData, sizeof(_EVENT_STRUCT_));
  170. /*++
  171. Macro Description:
  172. This macro is called when we allocate the strings in the EventData before
  173. queueing to a worker thread. FREE_STRING_MEMBER() should be called to free
  174. the string before the EventData itself is freed.
  175. Arguments:
  176. See signature.
  177. Notes:
  178. a. pData, pTempData and pReturnData are fixed names for the variable.
  179. They should not be changed without updating the code that uses
  180. this macro.
  181. --*/
  182. #define ALLOCATE_STRING_MEMBER(_DEST_, _SOURCE_) \
  183. \
  184. if (NULL != _SOURCE_) \
  185. { \
  186. SIZE_T size = wcslen(_SOURCE_)+1; \
  187. /* Allocate the string */ \
  188. _DEST_ = new WCHAR[size]; \
  189. if (NULL == (_DEST_)) \
  190. { \
  191. delete pData; \
  192. goto Cleanup; \
  193. } \
  194. StringCchCopy((_DEST_), size, (_SOURCE_)); \
  195. }
  196. /*++
  197. Macro Description:
  198. This macro is called when we finish allocating the EventData so that
  199. it can be queued to a worker thread. It should be always called after
  200. ALLOCATE_BEGIN() macro.
  201. Arguments:
  202. None.
  203. Notes:
  204. a. pData, pTempData and pReturnData are fixed names for the variable.
  205. They should not be changed without updating the code that uses
  206. this macro.
  207. --*/
  208. #define ALLOCATE_END() \
  209. \
  210. SensPrint(SENS_INFO, (SENS_STRING("******** Allocated a DS (0x%x)\n"), pData)); \
  211. pReturnData = pData; \
  212. break;
  213. /*++
  214. Macro Description:
  215. This macro is called to begin the deallocation of the EventData. This
  216. should always match with a call to ALLOCATE_BEGIN() macro.
  217. Arguments:
  218. None.
  219. Notes:
  220. a. pData, pTempData and pReturnData are fixed names for the variable.
  221. They should not be changed without updating the code that uses
  222. this macro.
  223. --*/
  224. #define FREE_BEGIN(_EVENT_STRUCT_) \
  225. \
  226. _EVENT_STRUCT_ *pData; \
  227. \
  228. pData = (_EVENT_STRUCT_ *) EventData;
  229. /*++
  230. Macro Description:
  231. This macro is called to free the string member of an EventData. This
  232. should always match with a call to ALLOCATE_STRING_MEMBER() macro.
  233. Arguments:
  234. See signature.
  235. Notes:
  236. a. pData, pTempData and pReturnData are fixed names for the variable.
  237. They should not be changed without updating the code that uses
  238. this macro.
  239. --*/
  240. #define FREE_STRING_MEMBER(_STRING_) \
  241. \
  242. if (NULL != _STRING_) \
  243. { \
  244. /* Free the string */ \
  245. delete _STRING_; \
  246. }
  247. /*++
  248. Macro Description:
  249. This macro is called to end the deallocation of EventData. This
  250. should always match with a call to FREE_BEGIN() macro.
  251. Arguments:
  252. See signature.
  253. Notes:
  254. a. pData, pTempData and pReturnData are fixed names for the variable.
  255. They should not be changed without updating the code that uses
  256. this macro.
  257. --*/
  258. #define FREE_END() \
  259. \
  260. SensPrint(SENS_INFO, (SENS_STRING("********** Freed a DS (0x%x)\n"), pData)); \
  261. delete pData; \
  262. break;
  263. void
  264. EvaluateConnectivity(
  265. IN CONNECTIVITY_TYPE Type
  266. )
  267. /*++
  268. Routine Description:
  269. This code queues up a job (for evaluating Network connectivity) to
  270. a worker thread.
  271. Arguments:
  272. Type - Indicates the type of connectivity to be evaluated.
  273. Return Value:
  274. None.
  275. --*/
  276. {
  277. BOOL bRetVal;
  278. LPTHREAD_START_ROUTINE lpfnEvaluate;
  279. switch (Type)
  280. {
  281. case TYPE_WAN:
  282. lpfnEvaluate = (LPTHREAD_START_ROUTINE) EvaluateWanConnectivity;
  283. break;
  284. case TYPE_DELAY_LAN:
  285. lpfnEvaluate = (LPTHREAD_START_ROUTINE) EvaluateLanConnectivityDelayed;
  286. break;
  287. default:
  288. case TYPE_LAN:
  289. lpfnEvaluate = (LPTHREAD_START_ROUTINE) EvaluateLanConnectivity;
  290. break;
  291. }
  292. bRetVal = SensQueueUserWorkItem(
  293. (LPTHREAD_START_ROUTINE) lpfnEvaluate,
  294. NULL,
  295. SENS_LONG_ITEM // Flags
  296. );
  297. if (FALSE == bRetVal)
  298. {
  299. SensPrintA(SENS_ERR, ("EvaluateConnectivity(): SensQueueUserWorkItem() failed with %d.\n",
  300. GetLastError()));
  301. }
  302. }
  303. void
  304. SensFireEvent(
  305. IN PVOID EventData
  306. )
  307. /*++
  308. Routine Description:
  309. This code queues up a job (for firing a SENS event) to a worker thread.
  310. Arguments:
  311. EventData - Data relating to the event.
  312. Return Value:
  313. None.
  314. --*/
  315. {
  316. BOOL bRetVal;
  317. PVOID pAllocatedData;
  318. pAllocatedData = AllocateEventData(EventData);
  319. if (NULL == pAllocatedData)
  320. {
  321. SensPrintA(SENS_ERR, ("SensFireEvent(): Failed to allocate Event Data!\n"));
  322. return;
  323. }
  324. bRetVal = SensQueueUserWorkItem(
  325. (LPTHREAD_START_ROUTINE) SensFireEventHelper,
  326. pAllocatedData, // Event Data
  327. SENS_LONG_ITEM // Flags
  328. );
  329. if (FALSE == bRetVal)
  330. {
  331. SensPrintA(SENS_ERR, ("SensFireEvent(): SensQueueUserWorkItem() failed with %d.\n",
  332. GetLastError()));
  333. }
  334. else
  335. {
  336. SensPrintA(SENS_INFO, ("SensFireEvent(): SensQueueUserWorkItem() succeeded.\n"));
  337. }
  338. }
  339. DWORD WINAPI
  340. SensFireEventHelper(
  341. IN PVOID EventData
  342. )
  343. /*++
  344. Routine Description:
  345. This code sets up the necessary stuff for firing a SENS event.
  346. Arguments:
  347. EventData - Data relating to the event.
  348. Return Value:
  349. S_OK, if successful
  350. hr, otherwise
  351. --*/
  352. {
  353. SENS_EVENT_TYPE eType;
  354. HRESULT hr;
  355. SensPrint(SENS_INFO, (SENS_STRING("\t|-------------------------------------------------------|\n")));
  356. SensPrint(SENS_INFO, (SENS_STRING("\t| E V E N T F I R E D |\n")));
  357. SensPrint(SENS_INFO, (SENS_STRING("\t|-------------------------------------------------------|\n")));
  358. hr = S_OK;
  359. eType = *(SENS_EVENT_TYPE *)EventData;
  360. switch (eType)
  361. {
  362. case SENS_EVENT_NETALIVE:
  363. {
  364. PSENSEVENT_NETALIVE pData = (PSENSEVENT_NETALIVE)EventData;
  365. SensPrint(SENS_INFO, (SENS_STRING("\t| %s%sNetwork Connectivity is %sPRESENT.\n\t|\n"),
  366. (pData->QocInfo.dwFlags & CONNECTION_WAN) ? SENS_STRING("WAN ") : SENS_STRING(""),
  367. (pData->QocInfo.dwFlags & CONNECTION_LAN) ? SENS_STRING("LAN ") : SENS_STRING(""),
  368. pData->bAlive ? SENS_STRING("") : SENS_STRING("NOT "))
  369. );
  370. hr = SensFireNetEventHelper(pData);
  371. break;
  372. }
  373. case SENS_EVENT_REACH:
  374. {
  375. PSENSEVENT_REACH pData = (PSENSEVENT_REACH)EventData;
  376. SensPrint(SENS_INFO, (SENS_STRING("\t| Destination is %sREACHABLE.\n"), pData->bReachable ? "" : "NOT "));
  377. SensPrint(SENS_INFO, (SENS_STRING("\t|\n\t| Name : %s\n"), pData->Destination));
  378. if (pData->bReachable == TRUE)
  379. {
  380. SensPrint(SENS_INFO, (SENS_STRING("\t| dwFlags : 0x%x \n"), pData->QocInfo.dwFlags));
  381. SensPrint(SENS_INFO, (SENS_STRING("\t| InSpeed : %d bits/sec.\n"), pData->QocInfo.dwInSpeed));
  382. SensPrint(SENS_INFO, (SENS_STRING("\t| OutSpeed : %d bits/sec.\n"), pData->QocInfo.dwOutSpeed));
  383. }
  384. hr = SensFireReachabilityEventHelper(pData);
  385. break;
  386. }
  387. case SENS_EVENT_PNP_DEVICE_ARRIVED:
  388. FIRE_PNP_EVENT(SENS_STRING("ARRIVED"));
  389. case SENS_EVENT_PNP_DEVICE_REMOVED:
  390. FIRE_PNP_EVENT(SENS_STRING("REMOVED"));
  391. case SENS_EVENT_POWER_ON_ACPOWER:
  392. FIRE_POWER_EVENT(SENS_STRING("ON AC POWER"), eType);
  393. case SENS_EVENT_POWER_ON_BATTERYPOWER:
  394. FIRE_POWER_EVENT(SENS_STRING("ON BATTERY POWER"), eType);
  395. case SENS_EVENT_POWER_BATTERY_LOW:
  396. FIRE_POWER_EVENT(SENS_STRING("BATTERY IS LOW"), eType);
  397. case SENS_EVENT_POWER_STATUS_CHANGE:
  398. FIRE_POWER_EVENT(SENS_STRING("POWER STATUS CHANGED"), eType);
  399. case SENS_EVENT_LOGON:
  400. FIRE_WINLOGON_EVENT(SENS_STRING("LOGON"), eType);
  401. case SENS_EVENT_LOGOFF:
  402. FIRE_WINLOGON_EVENT(SENS_STRING("LOGOFF"), eType);
  403. case SENS_EVENT_STARTSHELL:
  404. FIRE_WINLOGON_EVENT(SENS_STRING("STARTSHELL"), eType);
  405. case SENS_EVENT_POSTSHELL:
  406. FIRE_WINLOGON_EVENT(SENS_STRING("POSTSHELL"), eType);
  407. case SENS_EVENT_SESSION_DISCONNECT:
  408. FIRE_WINLOGON_EVENT(SENS_STRING("SESSION DISCONNECT"), eType);
  409. case SENS_EVENT_SESSION_RECONNECT:
  410. FIRE_WINLOGON_EVENT(SENS_STRING("SESSION RECONNECT"), eType);
  411. case SENS_EVENT_STARTSCREENSAVER:
  412. FIRE_WINLOGON_EVENT(SENS_STRING("STARTSCREENSAVER"), eType);
  413. case SENS_EVENT_STOPSCREENSAVER:
  414. FIRE_WINLOGON_EVENT(SENS_STRING("STOPSCREENSAVER"), eType);
  415. case SENS_EVENT_LOCK:
  416. FIRE_WINLOGON_EVENT(SENS_STRING("DISPLAY LOCK"), eType);
  417. case SENS_EVENT_UNLOCK:
  418. FIRE_WINLOGON_EVENT(SENS_STRING("DISPLAY UNLOCK"), eType);
  419. case SENS_EVENT_RAS_STARTED:
  420. FIRE_RAS_EVENT(SENS_STRING("RAS STARTED"));
  421. case SENS_EVENT_RAS_STOPPED:
  422. FIRE_RAS_EVENT(SENS_STRING("RAS STOPPED"));
  423. case SENS_EVENT_RAS_CONNECT:
  424. FIRE_RAS_EVENT(SENS_STRING("RAS CONNECT"));
  425. case SENS_EVENT_RAS_DISCONNECT:
  426. FIRE_RAS_EVENT(SENS_STRING("RAS DISCONNECT"));
  427. case SENS_EVENT_RAS_DISCONNECT_PENDING:
  428. FIRE_RAS_EVENT(SENS_STRING("RAS DISCONNECT PENDING"));
  429. case SENS_EVENT_LAN_CONNECT:
  430. FIRE_LAN_EVENT(SENS_STRING("LAN CONNECT"));
  431. case SENS_EVENT_LAN_DISCONNECT:
  432. FIRE_LAN_EVENT(SENS_STRING("LAN DISCONNECT"));
  433. default:
  434. SensPrint(SENS_ERR, (SENS_STRING("\t| A bogus event - %d !\n"), eType));
  435. hr = HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
  436. }
  437. //
  438. // Free the allocated Event data structure.
  439. //
  440. FreeEventData(EventData);
  441. return ((DWORD) hr);
  442. }
  443. PVOID
  444. AllocateEventData(
  445. PVOID EventData
  446. )
  447. /*++
  448. Routine Description:
  449. Allocated the EventData depending on the type of the event.
  450. Arguments:
  451. EventData - Data relating to the event.
  452. Return Value:
  453. None.
  454. --*/
  455. {
  456. SENS_EVENT_TYPE eType;
  457. PVOID pReturnData;
  458. pReturnData = NULL;
  459. if (NULL == EventData)
  460. {
  461. goto Cleanup;
  462. }
  463. eType = *(SENS_EVENT_TYPE *)EventData;
  464. switch (eType)
  465. {
  466. case SENS_EVENT_NETALIVE:
  467. {
  468. ALLOCATE_BEGIN(SENSEVENT_NETALIVE);
  469. ALLOCATE_STRING_MEMBER(pData->strConnection, pTempData->strConnection);
  470. ALLOCATE_END();
  471. }
  472. case SENS_EVENT_REACH:
  473. {
  474. ALLOCATE_BEGIN(SENSEVENT_REACH);
  475. ALLOCATE_STRING_MEMBER(pData->Destination, pTempData->Destination);
  476. ALLOCATE_STRING_MEMBER(pData->strConnection, pTempData->strConnection);
  477. ALLOCATE_END();
  478. }
  479. case SENS_EVENT_PNP_DEVICE_ARRIVED:
  480. case SENS_EVENT_PNP_DEVICE_REMOVED:
  481. {
  482. ALLOCATE_BEGIN(SENSEVENT_PNP);
  483. ALLOCATE_END();
  484. }
  485. case SENS_EVENT_POWER_ON_ACPOWER:
  486. case SENS_EVENT_POWER_ON_BATTERYPOWER:
  487. case SENS_EVENT_POWER_BATTERY_LOW:
  488. case SENS_EVENT_POWER_STATUS_CHANGE:
  489. {
  490. ALLOCATE_BEGIN(SENSEVENT_POWER);
  491. ALLOCATE_END();
  492. }
  493. case SENS_EVENT_LOGON:
  494. case SENS_EVENT_LOGOFF:
  495. case SENS_EVENT_STARTSHELL:
  496. case SENS_EVENT_POSTSHELL:
  497. case SENS_EVENT_SESSION_DISCONNECT:
  498. case SENS_EVENT_SESSION_RECONNECT:
  499. case SENS_EVENT_STARTSCREENSAVER:
  500. case SENS_EVENT_STOPSCREENSAVER:
  501. case SENS_EVENT_LOCK:
  502. case SENS_EVENT_UNLOCK:
  503. {
  504. ALLOCATE_BEGIN(SENSEVENT_WINLOGON);
  505. ALLOCATE_STRING_MEMBER(pData->Info.UserName, pTempData->Info.UserName);
  506. ALLOCATE_STRING_MEMBER(pData->Info.Domain, pTempData->Info.Domain);
  507. ALLOCATE_STRING_MEMBER(pData->Info.WindowStation, pTempData->Info.WindowStation);
  508. ALLOCATE_END();
  509. }
  510. case SENS_EVENT_RAS_STARTED:
  511. case SENS_EVENT_RAS_STOPPED:
  512. case SENS_EVENT_RAS_CONNECT:
  513. case SENS_EVENT_RAS_DISCONNECT:
  514. case SENS_EVENT_RAS_DISCONNECT_PENDING:
  515. {
  516. ALLOCATE_BEGIN(SENSEVENT_RAS);
  517. ALLOCATE_END();
  518. }
  519. case SENS_EVENT_LAN_CONNECT:
  520. case SENS_EVENT_LAN_DISCONNECT:
  521. {
  522. ALLOCATE_BEGIN(SENSEVENT_LAN);
  523. ALLOCATE_STRING_MEMBER(pData->Name, pTempData->Name);
  524. ALLOCATE_END();
  525. }
  526. default:
  527. SensPrint(SENS_ERR, (SENS_STRING("\t| A bogus event - %d !\n"), eType));
  528. break;
  529. }
  530. Cleanup:
  531. //
  532. // Cleanup
  533. //
  534. return pReturnData;
  535. }
  536. void
  537. FreeEventData(
  538. PVOID EventData
  539. )
  540. /*++
  541. Routine Description:
  542. Frees the EventData depending on the type of the event.
  543. Arguments:
  544. EventData - Data relating to the event.
  545. Return Value:
  546. None.
  547. --*/
  548. {
  549. SENS_EVENT_TYPE eType;
  550. if (NULL == EventData)
  551. {
  552. goto Cleanup;
  553. }
  554. eType = *(SENS_EVENT_TYPE *)EventData;
  555. switch (eType)
  556. {
  557. case SENS_EVENT_NETALIVE:
  558. {
  559. FREE_BEGIN(SENSEVENT_NETALIVE);
  560. FREE_STRING_MEMBER(pData->strConnection);
  561. FREE_END();
  562. }
  563. case SENS_EVENT_REACH:
  564. {
  565. FREE_BEGIN(SENSEVENT_REACH);
  566. FREE_STRING_MEMBER(pData->Destination);
  567. FREE_STRING_MEMBER(pData->strConnection);
  568. FREE_END();
  569. }
  570. case SENS_EVENT_PNP_DEVICE_ARRIVED:
  571. case SENS_EVENT_PNP_DEVICE_REMOVED:
  572. {
  573. FREE_BEGIN(SENSEVENT_PNP);
  574. FREE_END();
  575. }
  576. case SENS_EVENT_POWER_ON_ACPOWER:
  577. case SENS_EVENT_POWER_ON_BATTERYPOWER:
  578. case SENS_EVENT_POWER_BATTERY_LOW:
  579. case SENS_EVENT_POWER_STATUS_CHANGE:
  580. {
  581. FREE_BEGIN(SENSEVENT_POWER);
  582. FREE_END();
  583. }
  584. case SENS_EVENT_LOGON:
  585. case SENS_EVENT_LOGOFF:
  586. case SENS_EVENT_STARTSHELL:
  587. case SENS_EVENT_POSTSHELL:
  588. case SENS_EVENT_SESSION_DISCONNECT:
  589. case SENS_EVENT_SESSION_RECONNECT:
  590. case SENS_EVENT_STARTSCREENSAVER:
  591. case SENS_EVENT_STOPSCREENSAVER:
  592. case SENS_EVENT_LOCK:
  593. case SENS_EVENT_UNLOCK:
  594. {
  595. FREE_BEGIN(SENSEVENT_WINLOGON);
  596. FREE_STRING_MEMBER(pData->Info.UserName);
  597. FREE_STRING_MEMBER(pData->Info.Domain);
  598. FREE_STRING_MEMBER(pData->Info.WindowStation);
  599. FREE_END();
  600. }
  601. case SENS_EVENT_RAS_STARTED:
  602. case SENS_EVENT_RAS_STOPPED:
  603. case SENS_EVENT_RAS_CONNECT:
  604. case SENS_EVENT_RAS_DISCONNECT:
  605. case SENS_EVENT_RAS_DISCONNECT_PENDING:
  606. {
  607. FREE_BEGIN(SENSEVENT_RAS);
  608. FREE_END();
  609. }
  610. case SENS_EVENT_LAN_CONNECT:
  611. case SENS_EVENT_LAN_DISCONNECT:
  612. {
  613. FREE_BEGIN(SENSEVENT_LAN);
  614. FREE_STRING_MEMBER(pData->Name);
  615. FREE_END();
  616. }
  617. default:
  618. SensPrint(SENS_ERR, (SENS_STRING("\t| A bogus structure to free - %d !\n"), eType));
  619. break;
  620. }
  621. Cleanup:
  622. //
  623. // Cleanup
  624. //
  625. return;
  626. }
  627. HRESULT
  628. SensFireNetEventHelper(
  629. PSENSEVENT_NETALIVE pData
  630. )
  631. /*++
  632. Routine Description:
  633. Helps fire the SENS Network Alive event.
  634. Arguments:
  635. pData - Net alive event data.
  636. Return Value:
  637. S_OK, if successful.
  638. Failure hr, otherwise.
  639. --*/
  640. {
  641. HRESULT hr;
  642. BSTR bstrConnectionName;
  643. BSTR bstrMethodName;
  644. ISensNetwork *pISensNetwork;
  645. IEventControl *pIEventControl;
  646. SENS_QOCINFO SensQocInfo;
  647. CImpISensNetworkFilter NetEventsFilter; // Already AddRef'ed
  648. hr = S_OK;
  649. bstrConnectionName = NULL;
  650. bstrMethodName = NULL;
  651. pISensNetwork = NULL;
  652. pIEventControl = NULL;
  653. hr = CoCreateInstance(
  654. SENSGUID_EVENTCLASS_NETWORK,
  655. NULL,
  656. CLSCTX_SERVER,
  657. IID_ISensNetwork,
  658. (LPVOID *) &pISensNetwork
  659. );
  660. if (FAILED(hr))
  661. {
  662. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't get ISensNetwork object - ")
  663. SENS_STRING("hr = 0x%x\n"), hr));
  664. goto Cleanup;
  665. }
  666. SensPrint(SENS_INFO, (SENS_STRING("\t| Successfully created ISensNetwork Object\n")));
  667. //
  668. // Setup Publisher filtering
  669. //
  670. hr = pISensNetwork->QueryInterface(
  671. IID_IEventControl,
  672. (LPVOID *) &pIEventControl
  673. );
  674. if (FAILED(hr))
  675. {
  676. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't get IEventControl object - ")
  677. SENS_STRING("hr = 0x%x\n"), hr));
  678. goto Cleanup;
  679. }
  680. SensPrint(SENS_INFO, (SENS_STRING("\t| Successfully created IEventControl Object\n")));
  681. //
  682. // Disallow inproc activation (we're local system, and we
  683. // don't like foreign components in our security context,
  684. // potentially crashing us).
  685. //
  686. pIEventControl->put_AllowInprocActivation(FALSE);
  687. AllocateBstrFromString(bstrConnectionName, pData->strConnection);
  688. if (pData->bAlive)
  689. {
  690. // Connect events
  691. AllocateBstrFromString(bstrMethodName, CONNECTION_MADE_NOQOC_METHOD);
  692. hr = NetEventsFilter.Initialize(bstrMethodName, pIEventControl);
  693. if (FAILED(hr))
  694. {
  695. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't initialize PublisherFilters for Net Connect Event - ")
  696. SENS_STRING("hr = 0x%x\n"), hr));
  697. goto Cleanup;
  698. }
  699. hr = pIEventControl->SetPublisherFilter(
  700. bstrMethodName,
  701. &NetEventsFilter
  702. );
  703. if (FAILED(hr))
  704. {
  705. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't set PublisherFilters for Net Connect Event - ")
  706. SENS_STRING("hr = 0x%x\n"), hr));
  707. goto Cleanup;
  708. }
  709. //
  710. // Fire the necessary ISensNetwork events
  711. //
  712. //
  713. // ConnectionMadeNoQOCInfo event
  714. //
  715. hr = pISensNetwork->ConnectionMadeNoQOCInfo(
  716. bstrConnectionName,
  717. pData->QocInfo.dwFlags
  718. );
  719. if (FAILED(hr))
  720. {
  721. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't fire ConnectionMadeQOCInfo - ")
  722. SENS_STRING("hr = 0x%x\n"), hr));
  723. goto Cleanup;
  724. }
  725. SensPrint(SENS_INFO, (SENS_STRING("\t| ConnectionMadeNoQOCInfo() returned 0x%x\n"), hr));
  726. //
  727. // ConnectionMade event
  728. //
  729. FreeBstr(bstrMethodName);
  730. AllocateBstrFromString(bstrMethodName, CONNECTION_MADE_METHOD);
  731. hr = NetEventsFilter.Initialize(bstrMethodName, pIEventControl);
  732. if (FAILED(hr))
  733. {
  734. SensPrint(SENS_ERR, (SENS_STRING("\t| initialize't set PublisherFilters for NetEvent - ")
  735. SENS_STRING("hr = 0x%x\n"), hr));
  736. goto Cleanup;
  737. }
  738. hr = pIEventControl->SetPublisherFilter(
  739. bstrMethodName,
  740. &NetEventsFilter
  741. );
  742. if (FAILED(hr))
  743. {
  744. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't set PublisherFilters for NetEvent - ")
  745. SENS_STRING("hr = 0x%x\n"), hr));
  746. goto Cleanup;
  747. }
  748. memcpy(&SensQocInfo, &pData->QocInfo, sizeof(SENS_QOCINFO));
  749. hr = pISensNetwork->ConnectionMade(
  750. bstrConnectionName,
  751. pData->QocInfo.dwFlags,
  752. &SensQocInfo
  753. );
  754. if (FAILED(hr))
  755. {
  756. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't fire ConnectionMade - ")
  757. SENS_STRING("hr = 0x%x\n"), hr));
  758. goto Cleanup;
  759. }
  760. SensPrint(SENS_INFO, (SENS_STRING("\t| ConnectionMade() returned 0x%x\n"), hr));
  761. }
  762. else // bAlive == FALSE
  763. {
  764. // Disconnect event
  765. AllocateBstrFromString(bstrMethodName, CONNECTION_LOST_METHOD);
  766. hr = NetEventsFilter.Initialize(bstrMethodName, pIEventControl);
  767. if (FAILED(hr))
  768. {
  769. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't initialize PublisherFilters for Net Disconnect Event - ")
  770. SENS_STRING("hr = 0x%x\n"), hr));
  771. goto Cleanup;
  772. }
  773. hr = pIEventControl->SetPublisherFilter(
  774. bstrMethodName,
  775. &NetEventsFilter
  776. );
  777. if (FAILED(hr))
  778. {
  779. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't set PublisherFilters for Net Disconnect Event - ")
  780. SENS_STRING("hr = 0x%x\n"), hr));
  781. goto Cleanup;
  782. }
  783. //
  784. // Fire the necessary ISensNetwork events
  785. //
  786. //
  787. // ConnectionMadeNoQOCInfo event
  788. //
  789. hr = pISensNetwork->ConnectionLost(
  790. bstrConnectionName,
  791. pData->QocInfo.dwFlags
  792. );
  793. if (FAILED(hr))
  794. {
  795. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't fire ConnectionLost - ")
  796. SENS_STRING("hr = 0x%x\n"), hr));
  797. goto Cleanup;
  798. }
  799. SensPrint(SENS_INFO, (SENS_STRING("\t| ConnectionLost() returned 0x%x\n"), hr));
  800. }
  801. Cleanup:
  802. //
  803. // Cleanup
  804. //
  805. if (pIEventControl)
  806. {
  807. pIEventControl->Release();
  808. }
  809. if (pISensNetwork)
  810. {
  811. pISensNetwork->Release();
  812. }
  813. FreeBstr(bstrMethodName);
  814. FreeBstr(bstrConnectionName);
  815. return hr;
  816. }
  817. HRESULT
  818. SensFireReachabilityEventHelper(
  819. PSENSEVENT_REACH pData
  820. )
  821. /*++
  822. Routine Description:
  823. Helps fire the SENS Reachability event.
  824. Arguments:
  825. pData - Reachability event Data.
  826. Return Value:
  827. S_OK, if successful.
  828. Failure hr, otherwise.
  829. --*/
  830. {
  831. HRESULT hr;
  832. BSTR bstrConnectionName;
  833. BSTR bstrMethodName;
  834. BSTR bstrDestinationName;
  835. ISensNetwork *pISensNetwork;
  836. IEventControl *pIEventControl;
  837. SENS_QOCINFO SensQocInfo;
  838. CImpISensNetworkFilter NetEventsFilter; // Already AddRef'ed
  839. hr = S_OK;
  840. bstrMethodName = NULL;
  841. bstrConnectionName = NULL;
  842. bstrDestinationName = NULL;
  843. pISensNetwork = NULL;
  844. pIEventControl = NULL;
  845. hr = CoCreateInstance(
  846. SENSGUID_EVENTCLASS_NETWORK,
  847. NULL,
  848. CLSCTX_SERVER,
  849. IID_ISensNetwork,
  850. (LPVOID *) &pISensNetwork
  851. );
  852. if (FAILED(hr))
  853. {
  854. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't get ISensNetwork object - ")
  855. SENS_STRING("hr = 0x%x\n"), hr));
  856. goto Cleanup;
  857. }
  858. SensPrint(SENS_INFO, (SENS_STRING("\t| Successfully created ISensNetwork Object\n")));
  859. //
  860. // Setup Publisher filtering
  861. //
  862. hr = pISensNetwork->QueryInterface(
  863. IID_IEventControl,
  864. (LPVOID *) &pIEventControl
  865. );
  866. if (FAILED(hr))
  867. {
  868. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't get IEventControl object - ")
  869. SENS_STRING("hr = 0x%x\n"), hr));
  870. goto Cleanup;
  871. }
  872. SensPrint(SENS_INFO, (SENS_STRING("\t| Successfully created IEventControl Object\n")));
  873. //
  874. // Disallow inproc activation (we're local system, and we
  875. // don't like foreign components in our security context,
  876. // potentially crashing us).
  877. //
  878. pIEventControl->put_AllowInprocActivation(FALSE);
  879. AllocateBstrFromString(bstrMethodName, DESTINATION_REACHABLE_NOQOC_METHOD);
  880. hr = NetEventsFilter.Initialize(bstrMethodName, pIEventControl);
  881. if (FAILED(hr))
  882. {
  883. SensPrint(SENS_ERR, (SENS_STRING("\t| initialize't set PublisherFilters for ReachabilityEvent - ")
  884. SENS_STRING("hr = 0x%x\n"), hr));
  885. goto Cleanup;
  886. }
  887. hr = pIEventControl->SetPublisherFilter(
  888. bstrMethodName,
  889. &NetEventsFilter
  890. );
  891. if (FAILED(hr))
  892. {
  893. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't set PublisherFilters for ReachabilityEvent - ")
  894. SENS_STRING("hr = 0x%x\n"), hr));
  895. goto Cleanup;
  896. }
  897. //
  898. // Fire the Reachability events
  899. //
  900. AllocateBstrFromString(bstrConnectionName, pData->strConnection);
  901. AllocateBstrFromString(bstrDestinationName, pData->Destination);
  902. //
  903. // DestinationReachableNoQOCInfo event
  904. //
  905. hr = pISensNetwork->DestinationReachableNoQOCInfo(
  906. bstrDestinationName,
  907. bstrConnectionName,
  908. pData->QocInfo.dwFlags
  909. );
  910. if (FAILED(hr))
  911. {
  912. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't fire DestinationReachableNoQOCInfo - ")
  913. SENS_STRING("hr = 0x%x\n"), hr));
  914. goto Cleanup;
  915. }
  916. SensPrint(SENS_INFO, (SENS_STRING("\t| DestinationReachableNoQOCInfo() returned 0x%x\n"), hr));
  917. //
  918. // DestinationReachable event
  919. //
  920. FreeBstr(bstrMethodName);
  921. AllocateBstrFromString(bstrMethodName, DESTINATION_REACHABLE_METHOD);
  922. hr = NetEventsFilter.Initialize(bstrMethodName, pIEventControl);
  923. if (FAILED(hr))
  924. {
  925. SensPrint(SENS_ERR, (SENS_STRING("\t| initialize't set PublisherFilters for ReachabilityEvent - ")
  926. SENS_STRING("hr = 0x%x\n"), hr));
  927. goto Cleanup;
  928. }
  929. hr = pIEventControl->SetPublisherFilter(
  930. bstrMethodName,
  931. &NetEventsFilter
  932. );
  933. if (FAILED(hr))
  934. {
  935. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't set PublisherFilters for ReachabilityEvent - ")
  936. SENS_STRING("hr = 0x%x\n"), hr));
  937. goto Cleanup;
  938. }
  939. memcpy(&SensQocInfo, &pData->QocInfo, sizeof(SENS_QOCINFO));
  940. hr = pISensNetwork->DestinationReachable(
  941. bstrDestinationName,
  942. bstrConnectionName,
  943. pData->QocInfo.dwFlags,
  944. &SensQocInfo
  945. );
  946. if (FAILED(hr))
  947. {
  948. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't fire DestinationReachable - ")
  949. SENS_STRING("hr = 0x%x\n"), hr));
  950. goto Cleanup;
  951. }
  952. SensPrint(SENS_INFO, (SENS_STRING("\t| DestinationReachable() returned 0x%x\n"), hr));
  953. Cleanup:
  954. //
  955. // Cleanup
  956. //
  957. if (pIEventControl)
  958. {
  959. pIEventControl->Release();
  960. }
  961. if (pISensNetwork)
  962. {
  963. pISensNetwork->Release();
  964. }
  965. FreeBstr(bstrMethodName);
  966. FreeBstr(bstrConnectionName);
  967. FreeBstr(bstrDestinationName);
  968. return hr;
  969. }
  970. HRESULT
  971. SensFireWinlogonEventHelper(
  972. LPWSTR strArg,
  973. DWORD dwSessionId,
  974. SENS_EVENT_TYPE eType
  975. )
  976. /*++
  977. Routine Description:
  978. Helps fire the SENS Winlogon event.
  979. Arguments:
  980. strArg - DomainName\UserName
  981. dwSessionId - Session Id of the session on which this event was fired.
  982. eType - Type of Winlogon event.
  983. Return Value:
  984. S_OK, if successful.
  985. Failure hr, otherwise.
  986. --*/
  987. {
  988. HRESULT hr;
  989. HRESULT hr2;
  990. BOOL bLogon2;
  991. BSTR bstrUserName;
  992. ISensLogon *pISensLogon;
  993. ISensLogon2 *pISensLogon2;
  994. hr = S_OK;
  995. hr2 = S_OK;
  996. bstrUserName = NULL;
  997. pISensLogon = NULL;
  998. pISensLogon2 = NULL;
  999. //
  1000. // Get the ISensLogon Object
  1001. //
  1002. hr = CoCreateInstance(
  1003. SENSGUID_EVENTCLASS_LOGON,
  1004. NULL,
  1005. CLSCTX_SERVER,
  1006. IID_ISensLogon,
  1007. (LPVOID *) &pISensLogon
  1008. );
  1009. if (FAILED(hr))
  1010. {
  1011. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't get ISensLogon object - ")
  1012. SENS_STRING("hr = 0x%x\n"), hr));
  1013. goto Cleanup;
  1014. }
  1015. SensPrint(SENS_INFO, (SENS_STRING("\t| Successfully created ISensLogon Object\n")));
  1016. //
  1017. // Get the ISensLogon2 Object
  1018. //
  1019. hr2 = CoCreateInstance(
  1020. SENSGUID_EVENTCLASS_LOGON2,
  1021. NULL,
  1022. CLSCTX_SERVER,
  1023. IID_ISensLogon2,
  1024. (LPVOID *) &pISensLogon2
  1025. );
  1026. if (FAILED(hr2))
  1027. {
  1028. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't get ISensLogon2 object - ")
  1029. SENS_STRING("hr = 0x%x\n"), hr2));
  1030. goto Cleanup;
  1031. }
  1032. SensPrint(SENS_INFO, (SENS_STRING("\t| Successfully created ISensLogon2 Object\n")));
  1033. AllocateBstrFromString(bstrUserName, strArg);
  1034. switch (eType)
  1035. {
  1036. case SENS_EVENT_LOGON:
  1037. hr = pISensLogon->Logon(bstrUserName);
  1038. hr2 = pISensLogon2->Logon(bstrUserName, dwSessionId);
  1039. break;
  1040. case SENS_EVENT_LOGOFF:
  1041. hr = pISensLogon->Logoff(bstrUserName);
  1042. hr2 = pISensLogon2->Logoff(bstrUserName, dwSessionId);
  1043. break;
  1044. case SENS_EVENT_STARTSHELL:
  1045. hr = pISensLogon->StartShell(bstrUserName);
  1046. break;
  1047. case SENS_EVENT_POSTSHELL:
  1048. hr2 = pISensLogon2->PostShell(bstrUserName, dwSessionId);
  1049. break;
  1050. case SENS_EVENT_SESSION_DISCONNECT:
  1051. hr2 = pISensLogon2->SessionDisconnect(bstrUserName, dwSessionId);
  1052. break;
  1053. case SENS_EVENT_SESSION_RECONNECT:
  1054. hr2 = pISensLogon2->SessionReconnect(bstrUserName, dwSessionId);
  1055. break;
  1056. case SENS_EVENT_LOCK:
  1057. hr = pISensLogon->DisplayLock(bstrUserName);
  1058. break;
  1059. case SENS_EVENT_UNLOCK:
  1060. hr = pISensLogon->DisplayUnlock(bstrUserName);
  1061. break;
  1062. case SENS_EVENT_STARTSCREENSAVER:
  1063. hr = pISensLogon->StartScreenSaver(bstrUserName);
  1064. break;
  1065. case SENS_EVENT_STOPSCREENSAVER:
  1066. hr = pISensLogon->StopScreenSaver(bstrUserName);
  1067. break;
  1068. default:
  1069. SensPrint(SENS_WARN, (SENS_STRING("\t| Bad Winlogon Event - %d\n"), eType));
  1070. break;
  1071. }
  1072. //
  1073. // Check for failures
  1074. //
  1075. if (FAILED(hr))
  1076. {
  1077. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't fire ISensLogon->WinlogonEvent - ")
  1078. SENS_STRING("hr = 0x%x\n"), hr));
  1079. }
  1080. else
  1081. {
  1082. SensPrint(SENS_INFO, (SENS_STRING("\t| ISensLogon->WinlogonEvent() returned 0x%x\n"), hr));
  1083. }
  1084. if (FAILED(hr2))
  1085. {
  1086. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't fire ISensLogon2->WinlogonEvent(%d) - ")
  1087. SENS_STRING("hr = 0x%x\n"), dwSessionId, hr2));
  1088. }
  1089. else
  1090. {
  1091. SensPrint(SENS_INFO, (SENS_STRING("\t| ISensLogon2->WinlogonEvent(%d) returned 0x%x\n"), dwSessionId, hr));
  1092. }
  1093. Cleanup:
  1094. //
  1095. // Cleanup
  1096. //
  1097. if (pISensLogon)
  1098. {
  1099. pISensLogon->Release();
  1100. }
  1101. if (pISensLogon2)
  1102. {
  1103. pISensLogon2->Release();
  1104. }
  1105. FreeBstr(bstrUserName);
  1106. return hr;
  1107. }
  1108. HRESULT
  1109. SensFirePowerEventHelper(
  1110. SYSTEM_POWER_STATUS PowerStatus,
  1111. SENS_EVENT_TYPE eType
  1112. )
  1113. /*++
  1114. Routine Description:
  1115. Helps fire the SENS Power event.
  1116. Arguments:
  1117. PowerStatus - Power Status event structure
  1118. eType - Type of the Power event.
  1119. Return Value:
  1120. S_OK, if successful.
  1121. Failure hr, otherwise.
  1122. --*/
  1123. {
  1124. HRESULT hr;
  1125. ISensOnNow *pISensOnNow;
  1126. hr = S_OK;
  1127. pISensOnNow = NULL;
  1128. //
  1129. // Get the ISensOnNow Object
  1130. //
  1131. hr = CoCreateInstance(
  1132. SENSGUID_EVENTCLASS_ONNOW,
  1133. NULL,
  1134. CLSCTX_SERVER,
  1135. IID_ISensOnNow,
  1136. (LPVOID *) &pISensOnNow
  1137. );
  1138. if (FAILED(hr))
  1139. {
  1140. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't get ISensOnNow object - ")
  1141. SENS_STRING("hr = 0x%x\n"), hr));
  1142. goto Cleanup;
  1143. }
  1144. SensPrint(SENS_INFO, (SENS_STRING("\t| Successfully created ISensOnNow Object\n")));
  1145. switch (eType)
  1146. {
  1147. case SENS_EVENT_POWER_ON_ACPOWER:
  1148. hr = pISensOnNow->OnACPower();
  1149. break;
  1150. case SENS_EVENT_POWER_ON_BATTERYPOWER:
  1151. hr = pISensOnNow->OnBatteryPower(PowerStatus.BatteryLifePercent);
  1152. break;
  1153. case SENS_EVENT_POWER_BATTERY_LOW:
  1154. hr = pISensOnNow->BatteryLow(PowerStatus.BatteryLifePercent);
  1155. break;
  1156. default:
  1157. SensPrint(SENS_WARN, (SENS_STRING("\t| Bad Power Event - %d\n"), eType));
  1158. break;
  1159. }
  1160. if (FAILED(hr))
  1161. {
  1162. SensPrint(SENS_ERR, (SENS_STRING("\t| Couldn't fire ISensOnNow->PowerEvent - ")
  1163. SENS_STRING("hr = 0x%x\n"), hr));
  1164. //SensPrintToDebugger(SENS_DEB, ("\t| Couldn't fire ISensOnNow->PowerEvent - "
  1165. // "hr = 0x%x\n", hr));
  1166. goto Cleanup;
  1167. }
  1168. SensPrint(SENS_INFO, (SENS_STRING("\t| ISensOnNow->PowerEvent() returned 0x%x\n"), hr));
  1169. //SensPrintToDebugger(SENS_DEB, ("\t| ISensOnNow->PowerEvent() returned 0x%x\n", hr));
  1170. Cleanup:
  1171. //
  1172. // Cleanup
  1173. //
  1174. if (pISensOnNow)
  1175. {
  1176. pISensOnNow->Release();
  1177. }
  1178. return hr;
  1179. }