CHttpModule::D ispose 方法
釋放 CHttpModule 類別目前實例所使用的所有資源。
語法
virtual VOID Dispose(
VOID
);
參數
此方法不會採用任何參數。
傳回值
VOID
.
備註
IIS 會在要求結束時自動呼叫 Dispose
方法。
注意
您通常會使用 C++ new
運算子建立衍生自 CHttpModule
的類別。 使用 new
運算子建立的類別不需要實 Dispose
作 方法,才能在要求結束時執行任何清除。
範例
下列範例示範如何建立簡單的 「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 用戶端。 最後,模組會傳回 RQ_NOTIFICATION_FINISH_REQUEST ,以通知 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 |