共用方式為


CHttpModule::CHttpModule 方法 1.5

初始化 CHttpModule 類別的新實例

語法

CHttpModule (  
);  

參數

此方法不採用任何參數。

備註

類別的 CHttpModule 建構函式是 protected 防止直接建立 CHttpModule 類別。 您必須建立衍生自 CHttpModule的類別,因為 CHttpModule 會定義衍生類別必須實作的純 virtual 方法。

範例

下列程式代碼範例示範如何建立註冊RQ_BEGIN_REQUEST事件通知的要求層級 HTTP 模組。 當要求進入整合式要求處理管線時,IIS 會呼叫範例模組的 OnBeginRequest 方法。 此方法會在事件數據中包含 「Hello World!“ 的 Windows 事件檢視器 應用程式記錄中寫入專案。 處理完成時,模組將會結束。

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

// Create the module class.
class MyHttpModule : 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("Hello World!");
        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.
            return ReportEvent(
                m_hEventLog,
                EVENTLOG_INFORMATION_TYPE,
                0, 0, NULL, 1, 0, &szNotification, NULL );
        }
        return FALSE;
    }
};

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

// Create the module's exported registration function.
HRESULT
__stdcall
RegisterModule(
    DWORD dwServerVersion,
    IHttpModuleRegistrationInfo * pModuleInfo,
    IHttpServer * pGlobalInfo
)
{
    UNREFERENCED_PARAMETER( dwServerVersion );
    UNREFERENCED_PARAMETER( pGlobalInfo );

    // Set the request notifications and exit.
    return pModuleInfo->SetRequestNotifications(
        // Specify the class factory.
        new MyHttpModuleFactory,
        // Specify the event notifications.
        RQ_BEGIN_REQUEST,
        // Specify no post-event notifications.
        0
    );
}

您的模組必須匯出 RegisterModule 函式。 您可以為專案建立模組定義 (.def) 檔案來匯出此函式,或使用 參數編譯模組 /EXPORT:RegisterModule 。 如需詳細資訊,請參閱 逐步解說:使用機器碼建立要求層級 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
Products - 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

另請參閱

CHttpModule 類別
REQUEST_NOTIFICATION_STATUS列舉