AdminEventLoggerHook.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /////////////////////////////////////////////////////////////////////////////
  2. // AdminEventLoggerHook.cpp : Implementation of the CAdminEventLoggerHook
  3. // class.
  4. #include "pch.h"
  5. #include "AdminEventLoggerHook.h"
  6. #if !defined(ALLSRV_STANDALONE)
  7. /////////////////////////////////////////////////////////////////////////////
  8. // CAdminEventLoggerHook
  9. /////////////////////////////////////////////////////////////////////////////
  10. // Implementation
  11. /////////////////////////////////////////////////////////////////////////////
  12. // Purpose: Callback from completion of a synchronous CQLogEvent query.
  13. //
  14. void CAdminEventLoggerHook::EventLogged(CQLogEvent* pquery)
  15. {
  16. // Signal the event
  17. CQLogEventData* pqd = pquery->GetData();
  18. assert(pqd->hevt);
  19. ::SetEvent(pqd->hevt);
  20. }
  21. /////////////////////////////////////////////////////////////////////////////
  22. // IAGCEventLoggerHook Interface Methods
  23. STDMETHODIMP CAdminEventLoggerHook::LogEvent(IAGCEvent* pEvent, VARIANT_BOOL bSynchronous)
  24. {
  25. // Do nothing if SQLCore has not been initialized
  26. if (!g.sql.GetNotifyThreadID())
  27. return S_OK;
  28. // Get the fields of the specified event object
  29. AGCEventID idEvent;
  30. long idSubject;
  31. CComBSTR bstrComputerName, bstrSubjectName, bstrContext;
  32. ZSucceeded(pEvent->get_ID(&idEvent));
  33. ZSucceeded(pEvent->get_SubjectID (&idSubject ));
  34. ZSucceeded(pEvent->get_ComputerName(&bstrComputerName));
  35. ZSucceeded(pEvent->get_SubjectName (&bstrSubjectName ));
  36. ZSucceeded(pEvent->get_Context (&bstrContext ));
  37. // Use a callback if event is synchronous
  38. void (*pfnDataReady)(CQLogEvent* pquery) = bSynchronous ? EventLogged : NULL;
  39. // Create the database update query
  40. CQLogEvent* pquery = new CQLogEvent(pfnDataReady);
  41. CQLogEventData* pqd = pquery->GetData();
  42. // Ensure that the callback does NOT go through the message queue
  43. pquery->SetCallbackOnMainThread(false);
  44. // Populate the query parameters from the event fields
  45. USES_CONVERSION;
  46. pqd->nEvent = idEvent;
  47. pqd->nSubject = idSubject;
  48. if (bstrComputerName.Length())
  49. lstrcpyn(pqd->szComputerName, OLE2CT(bstrComputerName), sizeofArray(pqd->szComputerName));
  50. else
  51. pqd->szComputerName[0] = TEXT('\0');
  52. if (bstrSubjectName.Length())
  53. lstrcpyn(pqd->szSubjectName, OLE2CT(bstrSubjectName), sizeofArray(pqd->szSubjectName));
  54. else
  55. pqd->szSubjectName[0] = TEXT('\0');
  56. if (bstrContext.Length())
  57. lstrcpyn(pqd->szContext, OLE2CT(bstrContext), sizeofArray(pqd->szContext));
  58. else
  59. pqd->szContext[0] = TEXT('\0');
  60. // Get the event object as a string
  61. assert(IPersistStreamInitPtr(pEvent) != NULL || IPersistStreamPtr(pEvent) != NULL);
  62. assert(IMarshalPtr(pEvent) != NULL);
  63. CComBSTR bstrTemp;
  64. ZSucceeded(pEvent->SaveToString(&bstrTemp));
  65. WideCharToMultiByte(CP_ACP, 0, bstrTemp, -1,
  66. pqd->szObjectRef, sizeof(pqd->szObjectRef), 0, 0);
  67. // Create an event upon which to wait, if event is synchronous
  68. TCHandle shevt;
  69. if (bSynchronous)
  70. shevt = ::CreateEvent(NULL, false, false, NULL);
  71. pqd->hevt = shevt.GetHandle();
  72. // Post the query for async completion
  73. g.sql.PostQuery(pquery);
  74. // Wait for the event, if event is synchronous
  75. if (!shevt.IsNull())
  76. ::WaitForSingleObject(shevt, INFINITE);
  77. // Indicate that we handled it
  78. return S_OK;
  79. }
  80. #endif // !defined(ALLSRV_STANDALONE)