IHttpServer::NotifyCustomNotification 方法

引发自定义全局级别通知。

语法

virtual GLOBAL_NOTIFICATION_STATUS NotifyCustomNotification(  
   ICustomNotificationProvider* pCustomOutput  
) = 0;  

parameters

pCustomOutput
指向 ICustomNotificationProvider 的指针。

返回值

GLOBAL_NOTIFICATION_STATUS值。

备注

方法 NotifyCustomNotification 引发 由 ICustomNotificationProvider 参数中的 pCustomOutput 接口指定的自定义通知。

模块必须注册 GL_CUSTOM_NOTIFICATION 通知,并且模块必须包含 CGlobalModule::OnGlobalCustomNotification 方法来处理自定义通知。 若要引发自定义通知,模块必须首先创建自定义 ICustomNotificationProvider 接口的实例,并将该接口传递给 NotifyCustomNotification 当前全局上下文的 方法。

示例

下面的代码示例演示如何创建执行以下任务的 HTTP 模块:

  1. 注册 GL_PRE_BEGIN_REQUESTGL_CUSTOM_NOTIFICATION 通知。

  2. 创建包含 OnGlobalPreBeginRequestOnGlobalCustomNotification 方法的 CGlobalModule 类。

    1. 方法OnGlobalPreBeginRequest将事件写入指定当前通知的事件查看器。 然后, 方法创建接口的 ICustomNotificationProvider 实例,并使用 NotifyCustomNotification 方法引发自定义通知。

    2. 方法 OnGlobalCustomNotification 使用 ICustomNotificationProvider::QueryNotificationType 方法检索自定义通知的唯一标识符。 如果唯一标识符匹配,则 OnGlobalCustomNotification 方法会将事件写入事件查看器,指定引发自定义通知。

  3. CGlobalModule从内存中删除 类并退出。

#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>

// Define the unique notification indentifier.
#define MY_CUSTOM_NOTIFICATION L"MyCustomNotification"

// Create a pointer for the global server interface.
IHttpServer * g_pHttpServer = NULL;

// Create the custom notification class.
class MyCustomProvider : public ICustomNotificationProvider
{
public:
    // Create the method that will identify the custom notification.
    PCWSTR QueryNotificationType(VOID)
    {
        // Return the unique identifier string for the custom notification.
        return MY_CUSTOM_NOTIFICATION;
    }
    // Create the method that will process errors.
    VOID SetErrorStatus(HRESULT hrError)
    {
        return;
    }
};

// Create the module's global class.
class MyGlobalModule : public CGlobalModule
{
private:

    // Create a handle for the Event Viewer.
    HANDLE m_hEventLog;
    // Create a pointer for the custom notification.
    MyCustomProvider * m_pCustomProvider;

public:

    MyGlobalModule()
    {
        // Open the global handle to the Event Viewer.
        m_hEventLog = RegisterEventSource( NULL,"IISADMIN" );
        // Initialize the pointer for the custom notification to NULL.
        m_pCustomProvider = NULL;
    }

    ~MyGlobalModule()
    {
        // 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;
        }
        // Test whether the pointer for the custom notification is valid.
        if (NULL != m_pCustomProvider)
        {
            // Remove the custom notification from memory.
            delete m_pCustomProvider;
            m_pCustomProvider = NULL;
        }
    }

    GLOBAL_NOTIFICATION_STATUS
    OnGlobalPreBeginRequest(
        IN IPreBeginRequestProvider * pProvider
    )
    {
        // Create an array of strings.
        LPCSTR szBuffer[2] = {"MyGlobalModule","OnGlobalPreBeginRequest"};
        // Write the strings to the Event Viewer.
        WriteEventViewerLog(szBuffer,2);

        // Create the custom notification provider class.
        MyCustomProvider * m_pCustomProvider = new MyCustomProvider;

        // Test if the server and notification pointers are valid.
        if ((NULL != m_pCustomProvider) && (NULL != g_pHttpServer))
        {
            // Raise the custom notification.
            BOOL fCompletionExpected = TRUE;
            g_pHttpServer->NotifyCustomNotification(m_pCustomProvider);
        }

        // Return processing to the pipeline.
        return GL_NOTIFICATION_CONTINUE;
    }

    GLOBAL_NOTIFICATION_STATUS
    OnGlobalCustomNotification(
        IN ICustomNotificationProvider * pProvider
    )
    {
        // Retrieve the custom notification type;
        PCWSTR pNotificationType = pProvider->QueryNotificationType();

        // Test if the custom notification is correct.
        if (0 == wcscmp(pNotificationType,MY_CUSTOM_NOTIFICATION))
        {
            // Create an array of strings.
            LPCSTR szBuffer[2] = {"MyGlobalModule","OnGlobalCustomNotification"};
            // 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;
    }

private:

    // Create 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 != m_hEventLog)
        {
            // Write any strings to the Event Viewer and return.
            return ReportEvent(
                m_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;

    // Store the pointer for the global server interface.
    g_pHttpServer = pGlobalInfo;

    // 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 | GL_CUSTOM_NOTIFICATION );

    // Test for an error and exit if necessary.
    if (FAILED(hr))
    {
        return hr;
    }
    
    // Return a success status;
    return S_OK;
}

模块必须导出 RegisterModule 函数。 可以通过为项目创建模块定义 (.def) 文件导出此函数,也可以使用 开关编译模块 /EXPORT:RegisterModule 。 有关详细信息,请参阅 演练:使用本机代码创建Request-Level HTTP 模块

可以选择使用调用约定编译代码, __stdcall (/Gz) 而不是为每个函数显式声明调用约定。

要求

类型 说明
客户端 - Windows Vista 上的 IIS 7.0
- Windows 7 上的 IIS 7.5
- Windows 8 上的 IIS 8.0
- Windows 10 上的 IIS 10.0
服务器 - Windows Server 2008 上的 IIS 7.0
- Windows Server 2008 R2 上的 IIS 7.5
- Windows Server 2012 上的 IIS 8.0
- Windows Server 2012 R2 上的 IIS 8.5
- Windows Server 2016 上的 IIS 10.0
产品 - 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

另请参阅

ICustomNotificationProvider 接口
IHttpServer 接口
IHttpContext::NotifyCustomNotification 方法