IPreBeginRequestProvider::GetHttpCoNtext 方法
擷取 IHttpCoNtext 介面。
語法
virtual IHttpContext* GetHttpContext(
VOID
) = 0;
參數
此方法不會採用任何參數。
傳回值
IHttpCoNtext的指標。
備註
方法 GetHttpContext
會擷取 IHttpCoNtext 介面的指標,允許全域層級通知存取要求的內容。 這與提供介面指標 IHttpContext
的要求層級通知相反。
範例
下列程式碼範例示範如何建立全域層級 HTTP 模組,以使用 GetHttpContext
函式來擷取介面的 IHttpContext
指標。 模組會呼叫內容的 IHttpCoNtext::GetSite 方法來擷取 IHttpSite 介面的指標,然後呼叫 IHttpSite::GetSiteName 方法,以擷取正在處理要求的網站名稱。
#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>
// Create the module's global class.
class MyGlobalModule : public CGlobalModule
{
public:
GLOBAL_NOTIFICATION_STATUS
OnGlobalPreBeginRequest(
IN IPreBeginRequestProvider * pProvider
)
{
UNREFERENCED_PARAMETER( pProvider );
// Retrieve a pointer to the IHttpContext for the request.
IHttpContext * pHttpContext = pProvider->GetHttpContext();
// Test for an error.
if (NULL != pHttpContext)
{
// Retrieve a pointer to an IHttpSite class.
IHttpSite * pHttpSite = pHttpContext->GetSite();
// Test for an error.
if (NULL != pHttpSite)
{
// Retrieve the site name.
PCWSTR pwszSiteName = pHttpSite->GetSiteName();
// Test for an error.
if (NULL != pwszSiteName)
{
// Allocate storage for the site name.
char * pszSiteName =
(char *) pHttpContext->AllocateRequestMemory(
(DWORD) wcslen(pwszSiteName)+1 );
// Test for an error.
if (NULL != pszSiteName)
{
// Convert the site name.
wcstombs(pszSiteName,pwszSiteName,
wcslen(pwszSiteName));
// Create an array of strings.
LPCSTR szBuffer[2] = {"Site Name",""};
// Store the site name.
szBuffer[1] = pszSiteName;
// Write the strings to the Event Viewer.
WriteEventViewerLog(szBuffer,2);
}
}
}
}
// Return processing to the pipeline.
return GL_NOTIFICATION_CONTINUE;
}
VOID Terminate()
{
// Remove the class from memory.
delete this;
}
MyGlobalModule()
{
// Open a handle to the Event Viewer.
MyGlobalModule::m_hEventLog = RegisterEventSource( NULL,"IISADMIN" );
}
~MyGlobalModule()
{
// Test if the handle for the Event Viewer is open.
if (NULL != MyGlobalModule::m_hEventLog)
{
DeregisterEventSource( MyGlobalModule::m_hEventLog );
MyGlobalModule::m_hEventLog = NULL;
}
}
private:
// Handle for the Event Viewer.
HANDLE m_hEventLog;
// Define a method that writes to the Event Viewer.
BOOL WriteEventViewerLog(LPCSTR szBuffer[], WORD wNumStrings)
{
// Test whether the handle for the Event Viewer is open.
if (NULL != MyGlobalModule::m_hEventLog)
{
// Write any strings to the Event Viewer and return.
return ReportEvent(
MyGlobalModule::m_hEventLog,
EVENTLOG_INFORMATION_TYPE,
0, 0, NULL, wNumStrings,
0, szBuffer, NULL );
}
return FALSE;
}
};
// Create the module's exported registration function.
HRESULT
__stdcall
RegisterModule(
DWORD dwServerVersion,
IHttpModuleRegistrationInfo * pModuleInfo,
IHttpServer * pGlobalInfo
)
{
UNREFERENCED_PARAMETER( dwServerVersion );
UNREFERENCED_PARAMETER( pGlobalInfo );
// Create an instance of the global module class.
MyGlobalModule * pGlobalModule = new MyGlobalModule;
// Test for an error.
if (NULL == pGlobalModule)
{
return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
}
// Set the global notifications and exit.
return pModuleInfo->SetGlobalNotifications(
pGlobalModule, GL_PRE_BEGIN_REQUEST );
}
您的模組必須匯出 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 |