IHttpModuleFactory::GetHttpModule 方法
建立 CHttpModule 類別的實例。
語法
virtual HRESULT GetHttpModule(
OUT CHttpModule** ppModule,
IN IModuleAllocator* pAllocator
) = 0;
參數
ppModule
[OUT] CHttpModule 類別的 dereferenced 指標。
pAllocator
[IN] IModuleAllocator 介面的指標。
傳回值
HRESULT
。 可能的值包括 (但不限於) 下表中的這些值。
值 | 描述 |
---|---|
S_OK | 表示作業成功。 |
注意
因為您的模組處理站需要提供 GetHttpModule
方法,所以您可以針對應用程式適當地提供傳回值的任何狀態碼。 至少,您的 GetHttpModule
方法應該傳回S_OK,以指出成功完成。
備註
IHttpModuleFactory介面必須提供方法 GetHttpModule
,以建立類別 CHttpModule
的實例。 當 IIS 呼叫模組匯出的 RegisterModule 函式時,IIS 會使用模組處理 GetHttpModule
站的 方法來建立類別 CHttpModule
的實例。
範例
下列程式碼範例示範如何建立簡單的 「Hello World」 HTTP 模組。 模組會定義匯出 RegisterModule
的函式,將介面的 IHttpModuleFactory
實例傳遞至 IHttpModuleRegistrationInfo::SetRequestNotifications 方法,並註冊 RQ_BEGIN_REQUEST 通知。 IIS 會 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 |
另請參閱
CHttpModule 類別
IHttpModuleFactory 介面
設計Native-Code HTTP 模組
逐步解說:使用原生程式碼建立Request-Level HTTP 模組