// Create the module class. classMyHttpModule : public CHttpModule { public:
// Process an RQ_BEGIN_REQUEST event. REQUEST_NOTIFICATION_STATUS OnBeginRequest( IN IHttpContext * pHttpContext, IN IHttpEventProvider * pProvider ) { UNREFERENCED_PARAMETER( pHttpContext ); UNREFERENCED_PARAMETER( pProvider ); WriteEventViewerLog("OnBeginRequest"); return RQ_NOTIFICATION_CONTINUE; }
// Process an RQ_BEGIN_REQUEST post-event. REQUEST_NOTIFICATION_STATUS OnPostBeginRequest( IN IHttpContext * pHttpContext, IN IHttpEventProvider * pProvider ) { UNREFERENCED_PARAMETER( pHttpContext ); UNREFERENCED_PARAMETER( pProvider ); WriteEventViewerLog("OnPostBeginRequest"); return RQ_NOTIFICATION_CONTINUE; }
MyHttpModule() { // Open a handle to the Event Viewer. m_hEventLog = RegisterEventSource( NULL,"IISADMIN" ); }
~MyHttpModule() { // Test whether the handle for the Event Viewer is open. if (NULL != m_hEventLog) { // Close the handle to the Event Viewer. DeregisterEventSource( m_hEventLog ); m_hEventLog = NULL; } }
private:
// Create a handle for the event viewer. HANDLE m_hEventLog;
// Define a method that writes to the Event Viewer. BOOL WriteEventViewerLog(LPCSTR szNotification) { // Test whether the handle for the Event Viewer is open. if (NULL != m_hEventLog) { // Write any strings to the Event Viewer and return. returnReportEvent( m_hEventLog, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, &szNotification, NULL ); } return FALSE; } };
// Create the module's class factory. classMyHttpModuleFactory : public IHttpModuleFactory { public: HRESULT GetHttpModule( OUT CHttpModule ** ppModule, IN IModuleAllocator * pAllocator ) { UNREFERENCED_PARAMETER( pAllocator );
// Create a new instance. MyHttpModule * pModule = new MyHttpModule;
// Test for an error. if (!pModule) { // Return an error if the factory cannot create the instance. returnHRESULT_FROM_WIN32( ERROR_NOT_ENOUGH_MEMORY ); } else { // Return a pointer to the module. *ppModule = pModule; pModule = NULL; // Return a success status. return S_OK; } }
voidTerminate() { // Remove the class from memory. deletethis; } };
// Set the request notifications and exit. return pModuleInfo->SetRequestNotifications( // Specify the class factory. new MyHttpModuleFactory, // Specify the event notifications. RQ_BEGIN_REQUEST, // Specify the post-event notifications. RQ_BEGIN_REQUEST ); }