GLOBAL_NOTIFICATION_STATUS 枚举

定义全局级别通知的返回值。

语法

typedef enum GLOBAL_NOTIFICATION_STATUS{  
   GL_NOTIFICATION_CONTINUE,  
   GL_NOTIFICATION_HANDLED  
};  

成员

成员名称 说明
GL_NOTIFICATION_CONTINUE 指示 IIS 应继续处理其他全局级别通知。
GL_NOTIFICATION_HANDLED 指示 IIS 已完成处理全局级别通知,并且不应处理任何其他全局级别通知。

备注

枚举的成员 GLOBAL_NOTIFICATION_STATUS 用作全局级别通知的返回值,成员有助于控制全局级别通知处理管道中的进程流。 例如,从全局级别通知处理程序返回 GL_NOTIFICATION_CONTINUE 指示 IIS 继续处理其他全局级别通知,而从全局级别通知处理程序返回 GL_NOTIFICATION_HANDLED 会通知 IIS 全局级别处理已完成,并且 IIS 不应处理其他全局级别通知。

示例

下面的代码示例演示如何创建简单的“Hello World”全局级 HTTP 模块。 该模块定义导出的 RegisterModule 函数,该函数创建派生自 CGlobalModule 的类的实例。 如果无法创建类,则函数退出并显示错误代码;否则,函数调用 IHttpModuleRegistrationInfo::SetRequestNotifications 方法注册 GL_PRE_BEGIN_REQUEST 通知。

GL_PRE_BEGIN_REQUEST发生通知时,IIS 会调用模块的 CGlobalModule::OnGlobalPreBeginRequest 方法来处理通知。 方法调用专用方法将事件写入事件查看器的应用程序日志,然后返回GL_NOTIFICATION_CONTINUE通知 IIS 继续处理其他通知。 处理完成后,IIS 会调用模块的 CGlobalModule::Terminate 方法从内存中删除类。

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

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

    // Process a GL_APPLICATION_START notification.
    GLOBAL_NOTIFICATION_STATUS
    OnGlobalPreBeginRequest(
        IN IPreBeginRequestProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );
        WriteEventViewerLog( "Hello World!" );
        return GL_NOTIFICATION_CONTINUE;
    }

    VOID Terminate()
    {
        // Remove the class from memory.
        delete this;
    }

    MyGlobalModule()
    {
        // Open a handle to the Event Viewer.
        m_hEventLog = RegisterEventSource( NULL,"IISADMIN" );
    }

    ~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;
        }
    }

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.
            return ReportEvent(
                m_hEventLog,
                EVENTLOG_INFORMATION_TYPE, 0, 0,
                NULL, 1, 0, &szNotification, 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 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 and exit.
    return pModuleInfo->SetGlobalNotifications(
        pGlobalModule, GL_PRE_BEGIN_REQUEST );
}

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

另请参阅

Web 服务器核心枚举
REQUEST_NOTIFICATION_STATUS 枚举