PFN_REGISTERMODULE関数

ネイティブ コード HTTP モジュールの RegisterModule 関数プロトタイプを定義します。

構文

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

パラメーター

dwServerVersion
DWORD IIS メジャー バージョン番号を含む 。

pModuleInfo
IHttpModuleRegistrationInfo インターフェイスへのポインター。

pGlobalInfo
IHttpServer インターフェイスへのポインター。

戻り値

HRESULT。 有効な値を次の表に示しますが、これ以外にもあります。

説明
S_OK 操作が成功したことを示します。

注意

モジュールは任意の有効な HRESULT 値を返すことができますが、関数が成功したことを RegisterModule 示すために、少なくともS_OKを返す必要があります。

解説

PFN_REGISTERMODULE は、すべての HTTP モジュールが RegisterModule DLL エントリ ポイントに実装するために必要な関数の関数プロトタイプです。

HTTP モジュールを作成するときは、モジュールで次 RegisterModule のメソッドを追加する必要があります。

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

IIS がモジュールを読み込むには、すべての HTTP モジュールが関数 RegisterModule をエクスポートする必要があります。 関数を RegisterModule エクスポートするには、DLL プロジェクトのモジュール定義 (.def) ファイルを作成するか、 スイッチを使用してモジュールを /EXPORT:RegisterModule コンパイルします。

関数 RegisterModule では、 インターフェイスを IHttpModuleRegistrationInfo 使用して 、SetRequestNotifications メソッドと SetGlobalNotifications メソッドを使用して通知 を登録する 必要があります。 RegisterModule また、 を使用 IHttpModuleRegistrationInfo して 、SetPriorityForRequestNotification メソッドと SetPriorityForGlobalNotification メソッドを使用してモジュールの優先順位 登録する必要があります。

パラメーターには dwServerVersion 、モジュールを読み込む IIS のバージョンのメジャー バージョン番号が含まれています。 たとえば、IIS 7.0 の場合、 dwServerVersion パラメーターには 7 が含まれます。

IIS が関数を RegisterModule 呼び出すと、モジュールが IHttpServer サーバー レベルの情報を取得するために使用できるインターフェイスが提供されます。

注意

トレース イベントは、関数実装内で (IHttpTraceContext::QuickTrace メソッドまたはその他のRegisterModule方法を介して IHttpServer::GetTraceContext メソッドを介して) 発生させるべきではありません。 RegisterModule 内でトレース イベントを発生すると、要求パイプラインの早すぎるため、アクセス違反が発生する可能性があります。

次のコード例は、単純な "Hello World" HTTP モジュールを作成する方法を示しています。 モジュールは、IHttpModuleRegistrationInfo::SetRequestNotifications メソッドに IHttpModuleModuleFactory インターフェイスのインスタンスを渡し、RQ_BEGIN_REQUEST通知に登録するエクスポートされたRegisterModule関数を定義します。 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) 明示的に宣言するのではなく、呼び出し規約を使用してコードをコンパイルできます。

要件

Type 説明
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
Header Httpserv.h

参照

IHttpModuleRegistrationInfo インターフェイス
IHttpModuleRegistrationInfo::SetGlobalNotifications メソッド
IHttpModuleRegistrationInfo::SetPriorityForGlobalNotification メソッド
IHttpModuleRegistrationInfo::SetPriorityForRequestNotification メソッド
IHttpModuleRegistrationInfo::SetRequestNotifications メソッド
IHttpServer インターフェイス