共用方式為


CGlobalModule 類別

定義全域層級 HTTP 模組的基類。

Syntax

class CGlobalModule  

方法

下表列出 類別所 CGlobalModule 公開的方法。

名稱 描述
OnGlobalApplicationPreload deddc775-1ab0-492c-bda0-b32be5d4f4cc
OnGlobalApplicationResolveModules 表示將處理 GlobalApplicationResolveModules 事件的方法,這個方法會在 IIS 解析已註冊的模組時發生。
OnGlobalApplicationStart 表示將處理 GlobalApplicationStart 事件的方法,這會在 IIS 啟動應用程式時發生。
OnGlobalApplicationStop 表示將處理 GlobalApplicationStop 事件的方法,這會在 IIS 關閉應用程式時發生。
OnGlobalCacheCleanup 代表將處理 GlobalCacheCleanup 事件的方法。
OnGlobalCacheOperation 表示將處理 GlobalCacheOperation 事件的方法,這個方法會在 IIS 執行快取相關作業時發生。
OnGlobalConfigurationChange 表示將處理 GlobalConfigurationChange 事件的方法,這個方法會在對組態檔進行變更時發生。
OnGlobalCustomNotification 表示將處理 GlobalCustomNotification 事件的方法,這會在模組引發使用者定義通知時發生。
OnGlobalFileChange 表示將處理 GlobalFileChange 事件的方法,這個方法會在網站內的檔案變更時發生。
OnGlobalHealthCheck 表示將處理 GlobalHealthCheck 事件的方法,這個方法會在執行健康情況相關作業時發生。
OnGlobalPreBeginRequest 表示將處理 GlobalPreBeginRequest 事件的方法,這個方法會在要求進入管線之前發生。
OnGlobalRSCAQuery 表示將處理 GlobalRSCAQuery 事件的方法,這個方法會在執行Run-Time狀態和控制項查詢時發生。
OnGlobalStopListening 表示將處理 GlobalStopListening 事件的方法,這會在 IIS 停止接受新要求時發生。
OnGlobalThreadCleanup 表示將處理 GlobalThreadCleanup 事件的方法,這會在 IIS 將執行緒傳回執行緒集區時發生。
OnGlobalTraceEvent 表示將處理 GlobalTraceEvent 事件的方法,這個方法會在引發追蹤事件時發生。
終止 表示 IIS 在全域模組完成處理時所呼叫的方法。

衍生類別

這個類別不包含衍生類別。

備註

類別 CGlobalModule 是全域層級 HTTP 模組的基類。 全域層級 HTTP 模組必須包含繼承自 CGlobalModule 的類別。 CGlobalModule 定義 IIS 7 在全域層級事件發生時呼叫以處理全域層級通知的方法。 HTTP 模組可以藉由定義模組匯出 的 RegisterModule 函式中的通知清單,來註冊特定事件。 當全域層級模組完成處理時,模組應該使用 CGlobalModule::Terminate 方法從記憶體中移除 CGlobalModule 實例。

範例

下列程式碼範例示範如何建立簡單的「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) 而不是明確宣告每個函式的呼叫慣例。

規格需求

類型 描述
Client - 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
標頭 Httpserv.h

另請參閱

Web Server Core 類別
建立 Native-Code HTTP 模組
CHttpModule 類別