共用方式為


PFN_REGISTERMODULE函式

RegisterModule定義原生程式碼 HTTP 模組的函式原型。

語法

typedef HRESULT(WINAPI* PFN_REGISTERMODULE)(  
   DWORD dwServerVersion,  
   IHttpModuleRegistrationInfo* pModuleInfo,  
   IHttpServer* pGlobalInfo  
);  

參數

dwServerVersion
DWORD,包含 IIS 主要版本號碼。

pModuleInfo
IHttpModuleRegistrationInfo介面的指標。

pGlobalInfo
IHttpServer介面的指標。

傳回值

HRESULT。 可能的值包括 (但不限於) 下表中的這些值。

描述
S_OK 表示作業成功。

注意

您的模組可以傳回任何有效的 HRESULT 值,但應該至少傳回S_OK,以指出您的 RegisterModule 函式成功。

備註

PFN_REGISTERMODULE 是函式的 RegisterModule 函式原型,所有 HTTP 模組都必須針對其 DLL 進入點實作。

當您建立 HTTP 模組時,您的模組必須新增下列 RegisterModule 方法:

HRESULT RegisterModule(  
   DWORD dwServerVersion,  
   IHttpModuleRegistrationInfo* pModuleInfo,  
   IHttpServer* pGlobalInfo  
)  

所有 HTTP 模組都必須匯出其 RegisterModule 函式,IIS 才能載入模組。 您可以為 DLL 專案建立模組定義 (.def) 檔案,或使用 參數編譯模組來匯出 RegisterModule/EXPORT:RegisterModule 式。

您的 RegisterModule 函式必須使用 IHttpModuleRegistrationInfo 介面來註冊通知,方法是使用 SetRequestNotificationsSetGlobalNotifications 方法。 RegisterModule 也需要使用 IHttpModuleRegistrationInfoSetPriorityForRequestNotificationSetPriorityForGlobalNotification 方法來註冊模組優先順序。

參數 dwServerVersion 包含載入模組之 IIS 版本的主要版本號碼。 例如,針對 IIS 7.0, dwServerVersion 參數將包含 7。

當 IIS 呼叫函 RegisterModule 式時,它會提供 IHttpServer 介面,您的模組可用來擷取伺服器層級資訊。

注意

追蹤事件不應該透過 IHttpTraceCoNtext::QuickTrace 方法 或透過 IHttpServer::GetTraceCoNtext 方法) 函式實作內 RegisterModule 的任何其他方式 (引發追蹤事件。 在 RegisterModule 內引發追蹤事件可能會導致存取違規,因為要求管線中太早。

範例

下列程式碼範例示範如何建立簡單的 「Hello World」 HTTP 模組。 模組會定義匯出 RegisterModule 的函式,將 IHttpModuleFactory 介面的實例傳遞至 IHttpModuleRegistrationInfo::SetRequestNotifications 方法,並註冊 RQ_BEGIN_REQUEST 通知。 IIS 使用 IHttpModuleFactory::GetHttpModule 方法建立 CHttpModule 類別的實例,並傳回成功狀態。 IIS 也會使用 IHttpModuleFactory::Terminate 方法從記憶體中移除處理站。

RQ_BEGIN_REQUEST發生通知時,IIS 會呼叫模組的CHttpModule::OnBeginRequest方法來處理目前的要求。 OnBeginRequest 清除回應緩衝區,並修改回應的 MIME 類型。 然後,方法會建立包含 「Hello World」 字串的資料區塊,並將字串傳回至 Web 用戶端。 最後,模組會傳回狀態指示器,通知 IIS 所有通知都已完成,然後結束。

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

// Create the module class.
class CHelloWorld : public CHttpModule
{
public:
    REQUEST_NOTIFICATION_STATUS
    OnBeginRequest(
        IN IHttpContext * pHttpContext,
        IN IHttpEventProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );

        // Create an HRESULT to receive return values from methods.
        HRESULT hr;
        
        // Retrieve a pointer to the response.
        IHttpResponse * pHttpResponse = pHttpContext->GetResponse();

        // Test for an error.
        if (pHttpResponse != NULL)
        {
            // Clear the existing response.
            pHttpResponse->Clear();
            // Set the MIME type to plain text.
            pHttpResponse->SetHeader(
                HttpHeaderContentType,"text/plain",
                (USHORT)strlen("text/plain"),TRUE);

            // Create a string with the response.
            PCSTR pszBuffer = "Hello World!";
            // Create a data chunk.
            HTTP_DATA_CHUNK dataChunk;
            // Set the chunk to a chunk in memory.
            dataChunk.DataChunkType = HttpDataChunkFromMemory;
            // Buffer for bytes written of data chunk.
            DWORD cbSent;
            
            // Set the chunk to the buffer.
            dataChunk.FromMemory.pBuffer =
                (PVOID) pszBuffer;
            // Set the chunk size to the buffer size.
            dataChunk.FromMemory.BufferLength =
                (USHORT) strlen(pszBuffer);
            // Insert the data chunk into the response.
            hr = pHttpResponse->WriteEntityChunks(
                &dataChunk,1,FALSE,TRUE,&cbSent);

            // Test for an error.
            if (FAILED(hr))
            {
                // Set the HTTP status.
                pHttpResponse->SetStatus(500,"Server Error",0,hr);
            }

            // End additional processing.
            return RQ_NOTIFICATION_FINISH_REQUEST;
        }

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

// Create the module's class factory.
class CHelloWorldFactory : public IHttpModuleFactory
{
public:
    HRESULT
    GetHttpModule(
        OUT CHttpModule ** ppModule, 
        IN IModuleAllocator * pAllocator
    )
    {
        UNREFERENCED_PARAMETER( pAllocator );

        // Create a new instance.
        CHelloWorld * pModule = new CHelloWorld;

        // 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(
        new CHelloWorldFactory,
        RQ_BEGIN_REQUEST,
        0
    );
}

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

另請參閱

IHttpModuleRegistrationInfo 介面
IHttpModuleRegistrationInfo::SetGlobalNotifications 方法
IHttpModuleRegistrationInfo::SetPriorityForGlobalNotification 方法
IHttpModuleRegistrationInfo::SetPriorityForRequestNotification 方法
IHttpModuleRegistrationInfo::SetRequestNotifications 方法
IHttpServer 介面