IHttpModuleRegistrationInfo::SetGlobalNotifications Method
Registers the global-level notifications for a module.
Syntax
virtual HRESULT SetGlobalNotifications(
IN CGlobalModule* pGlobalModule,
IN DWORD dwGlobalNotifications
) = 0;
Parameters
pGlobalModule
[IN] A pointer to a CGlobalModule class.
dwGlobalNotifications
[IN] A bitmask value that contains the global notifications to register. (Defined in Httpserv.h.)
Return Value
An HRESULT
. Possible values include, but are not limited to, those in the following table.
Value | Description |
---|---|
S_OK | Indicates that the operation was successful. |
ERROR_ALREADY_EXISTS | Indicates that the module has already been registered. |
Remarks
The SetGlobalNotifications
method registers the request-level notifications for a CGlobalModule
class.
Note
The bitmask values for global-level notifications are defined in the Httpserv.h file.
The SetGlobalNotifications
method requires a pointer to a CGlobalModule
class, and IIS will automatically create an instance of that class.
Note
The CGlobalModule
class must define a Terminate method.
Example
The following code example demonstrates how to create an HTTP module that uses the RegisterModule function and the following methods to register a module for global-level and request-level notifications.
The SetRequestNotifications method registers a CHttpModule class for a request-level OnBeginRequest notification.
The SetPriorityForRequestNotification method sets the module's priority for request-level notifications.
The
SetGlobalNotifications
method registers aCGlobalModule
class for a global-level OnGlobalPreBeginRequest notification.The SetPriorityForGlobalNotification method sets the module's priority for global-level notification.
The module responds to the registered notifications and writes entries to the application log in the Event Viewer.
Note
The entries in the Event Viewer will display "IISADMIN" as the event source.
#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>
// Create a global handle for the Event Viewer.
HANDLE g_hEventLog;
// Define the method that writes to the Event Viewer.
BOOL WriteEventViewerLog(LPCSTR szBuffer[], WORD wNumStrings);
// Create the HTTP module class.
class MyHttpModule : public CHttpModule
{
public:
REQUEST_NOTIFICATION_STATUS
OnBeginRequest(
IN IHttpContext * pHttpContext,
IN IHttpEventProvider * pProvider
)
{
UNREFERENCED_PARAMETER( pHttpContext );
UNREFERENCED_PARAMETER( pProvider );
// Create an array of strings.
LPCSTR szBuffer[2] = {"MyHttpModule","OnBeginRequest"};
// Write the strings to the Event Viewer.
WriteEventViewerLog(szBuffer,2);
// Return processing to the pipeline.
return RQ_NOTIFICATION_CONTINUE;
}
};
// Create the module's global class.
class MyGlobalModule : public CGlobalModule
{
public:
GLOBAL_NOTIFICATION_STATUS
OnGlobalPreBeginRequest(
IN IPreBeginRequestProvider * pProvider
)
{
UNREFERENCED_PARAMETER( pProvider );
// Create an array of strings.
LPCSTR szBuffer[2] = {"MyGlobalModule","OnGlobalPreBeginRequest"};
// Write the strings to the Event Viewer.
WriteEventViewerLog(szBuffer,2);
// Return processing to the pipeline.
return GL_NOTIFICATION_CONTINUE;
}
VOID Terminate()
{
// Remove the class from memory.
delete this;
}
MyGlobalModule()
{
// Open a handle to the Event Viewer.
g_hEventLog = RegisterEventSource( NULL,"IISADMIN" );
}
~MyGlobalModule()
{
// Test whether the handle for the Event Viewer is open.
if (NULL != g_hEventLog)
{
DeregisterEventSource( g_hEventLog );
g_hEventLog = NULL;
}
}
};
// Create the module's class factory.
class MyHttpModuleFactory : 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.
return HRESULT_FROM_WIN32( ERROR_NOT_ENOUGH_MEMORY );
}
else
{
// Return a pointer to the module.
*ppModule = pModule;
pModule = NULL;
// Return a success status.
return S_OK;
}
}
void Terminate()
{
// Remove the class from memory.
delete this;
}
};
// Define a method that writes to the Event Viewer.
BOOL WriteEventViewerLog(LPCSTR szBuffer[], WORD wNumStrings)
{
// Test whether the handle for the Event Viewer is open.
if (NULL != g_hEventLog)
{
// Write any strings to the Event Viewer and return.
return ReportEvent(
g_hEventLog,
EVENTLOG_INFORMATION_TYPE,
0, 0, NULL, wNumStrings,
0, szBuffer, NULL );
}
return FALSE;
}
// Create the module's exported registration function.
HRESULT
__stdcall
RegisterModule(
DWORD dwServerVersion,
IHttpModuleRegistrationInfo * pModuleInfo,
IHttpServer * pGlobalInfo
)
{
UNREFERENCED_PARAMETER( dwServerVersion );
UNREFERENCED_PARAMETER( pGlobalInfo );
// Create an HRESULT to receive return values from methods.
HRESULT hr;
// Set the request notifications.
hr = pModuleInfo->SetRequestNotifications(
new MyHttpModuleFactory,
RQ_BEGIN_REQUEST, 0 );
// Test for an error and exit if necessary.
if (FAILED(hr))
{
return hr;
}
// Set the request priority.
hr = pModuleInfo->SetPriorityForRequestNotification(
RQ_BEGIN_REQUEST,PRIORITY_ALIAS_MEDIUM);
// Test for an error and exit if necessary.
if (FAILED(hr))
{
return hr;
}
// Create an instance of the global module class.
MyGlobalModule * pGlobalModule = new MyGlobalModule;
// Test for an error.
if (NULL == pGlobalModule)
{
return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
}
// Set the global notifications.
hr = pModuleInfo->SetGlobalNotifications(
pGlobalModule, GL_PRE_BEGIN_REQUEST );
// Test for an error and exit if necessary.
if (FAILED(hr))
{
return hr;
}
// Set the global priority.
hr = pModuleInfo->SetPriorityForGlobalNotification(
GL_PRE_BEGIN_REQUEST,PRIORITY_ALIAS_LOW);
// Test for an error and exit if necessary.
if (FAILED(hr))
{
return hr;
}
// Return a success status;
return S_OK;
}
Your module must export the RegisterModule
function. You can export this function by creating a module definition (.def) file for your project, or you can compile the module by using the /EXPORT:RegisterModule
switch. For more information, see Walkthrough: Creating a Request-Level HTTP Module By Using Native Code.
You can optionally compile the code by using the __stdcall (/Gz)
calling convention instead of explicitly declaring the calling convention for each function.
Requirements
Type | Description |
---|---|
Client | - IIS 7.0 on Windows Vista - IIS 7.5 on Windows 7 - IIS 8.0 on Windows 8 - IIS 10.0 on Windows 10 |
Server | - IIS 7.0 on Windows Server 2008 - IIS 7.5 on Windows Server 2008 R2 - IIS 8.0 on Windows Server 2012 - IIS 8.5 on Windows Server 2012 R2 - IIS 10.0 on Windows Server 2016 |
Product | - IIS 7.0, IIS 7.5, IIS 8.0, IIS 8.5, IIS 10.0 - IIS Express 7.5, IIS Express 8.0, IIS Express 10.0 |
Header | Httpserv.h |
See Also
IHttpModuleRegistrationInfo Interface
IHttpModuleRegistrationInfo::SetPriorityForGlobalNotification Method
IHttpModuleRegistrationInfo::SetPriorityForRequestNotification Method
IHttpModuleRegistrationInfo::SetRequestNotifications Method
PFN_REGISTERMODULE Function