IHttpModuleCoNtextContainer::SetModuleCoNtext 方法
設定內容容器上的預存內容。
語法
virtual HRESULT SetModuleContext(
IN IHttpStoredContext* ppStoredContext,
IN HTTP_MODULE_ID moduleId
) = 0;
參數
ppStoredContext
[IN] IHttpStoredCoNtext 值的指標。 必須是非 Null。
moduleId
[IN] HTTP_MODULE_ID
指標。
注意
HTTP_MODULE_ID
是指標的類型 void
定義。
傳回值
HRESULT
。 可能的值包括 (但不限於) 下表中的這些值。
值 | 定義 |
---|---|
NO_ERROR | 表示作業成功。 |
ERROR_INVALID_PARAMETER | 表示 ppStoredContext 為 Null。 |
ERROR_NOT_ENOUGH_MEMORY | 表示堆積記憶體已耗盡。 |
ERROR_ALREADY_ASSIGNED | 表示 SetModuleContext 先前已呼叫 。 |
備註
CGlobalModule 或 CHttpModule 指標會註冊 Httpserv.h 標頭檔中定義的各種事件。 如需詳細資訊,請參閱 要求處理常數。 透過這些類別 virtual
的任何方法,您可以從實 GetModuleContextContainer
作方法的各種介面擷取IHttpModuleCoNtextContainer指標。
您可以定義實作 介面的 IHttpStoredContext
自訂類別,然後藉由呼叫 new
運算子來建立這個 IHttpStoredContext
類別實作器的指標。 然後,您可以分別呼叫 SetModuleContext
和GetModuleCoNtext方法,在指標上 IHttpModuleContextContainer
新增和擷取此指標。
IHttpStoredContext
不再需要指標時,IHttpStoredCoNtext::CleanupStoredCoNtext方法會在內部呼叫,其中介面方法的 IHttpStoredContext
實作者通常應該呼叫 delete``this
。
實作者的注意事項
IHttpModuleCoNtextContainer 實作者負責使用此資料的記憶體管理;因此,當不再需要動態記憶體配置時, IHttpModuleContextContainer
使用動態記憶體配置的實作必須釋放或呼叫 delete
IHttpStoredContext
指標。 如果需要清除,您可以呼叫 IHttpStoredCoNtext::CleanupStoredCoNtext 方法。
來電者的注意事項
IHttpModuleContextContainer
實作者負責使用此資料的記憶體管理;因此,當不再需要此資料時, IHttpModuleContextContainer
用戶端不得釋放或呼叫 delete
傳 IHttpStoredContext
回的指標。
範例
下列程式碼範例示範如何建立全域模組,以接聽GL_TRACE_EVENT事件,然後將自訂IHttpStoredCoNtext資訊寫入事件檢視器。
警告
IIS 7 會在事件檢視器中產生大量的事件。 若要避免生產環境中的記錄溢位錯誤,您通常應該避免將快取資訊寫入事件記錄檔。 為了示範目的,此程式碼範例只會以偵錯模式將專案寫入事件檢視器。
#pragma warning( disable : 4290 )
#pragma warning( disable : 4530 )
#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <tchar.h>
#include <initguid.h>
#include <httptrace.h>
#include <httpserv.h>
#include <httpcach.h>
#include <string>
using namespace std;
// The CConvert class mirrors the Convert class that is
// defined in the .NET Framework. It converts primitives
// and other data types to wstring types.
class CConvert
{
public:
// The ToByteString converts a double-byte
// character string to a single-byte string.
// str: the double-byte string to convert.
// return: a single-byte string copied from str.
static string ToByteString(const wstring& str)
{
// Get the length of the
// double-byte string.
size_t length = str.length();
// Create a temporary char pointer.
char* byteChar = new char[length+1];
byteChar[0] = '\0';
// Copy the double-byte character string
// into the single-byte string.
size_t charsReturned = 0;
wcstombs_s(&charsReturned, byteChar,
length+1, str.c_str(), length+1);
// Create a string to return.
string retString = byteChar;
// Delete the temporary string and
// set that string to NULL.
delete[] byteChar;
byteChar = NULL;
// Return the single-byte string.
return retString;
}
};
// The CEventWriter class writes XML
// documents and strings to the event log.
class CEventWriter
{
public:
// Creates the CEventWriter class.
// name: the name of the
// event log to open.
CEventWriter(const wstring& name)
{
#ifdef UNICODE
m_eventLog = RegisterEventSource(NULL, name.c_str());
#else
string multiName = CConvert::ToByteString(name);
m_eventLog = RegisterEventSource(NULL, multiName.c_str());
#endif
}
// Creates the destructor for the
// CEventWriter class. This destructor
// closes the HANDLE to the event
// log if that HANDLE is open.
virtual ~CEventWriter()
{
// If the HANDLE to the event
// log is open, close it.
if (NULL != m_eventLog)
{
// Deregister the event log HANDLE.
DeregisterEventSource(m_eventLog);
// Set the HANDLE to NULL.
m_eventLog = NULL;
}
}
// The ReportInfo method writes
// a wstring to the event log.
// info: the wstring to write.
// return: true if the event log is written.
BOOL ReportInfo(const wstring& info)
{
return ReportEvent(EVENTLOG_INFORMATION_TYPE, info);
}
protected:
// The ReportEvent method accepts an event type
// and a wstring, and attempts to write that
// event to the event log.
// type: the type of the event.
// data: the wstring to write to the event log.
// return: true if the event log is written;
// otherwise, false.
BOOL ReportEvent(WORD type, const wstring& data)
{
// If the m_eventLog HANDLE
// is NULL, return false.
if (NULL == m_eventLog)
{
return FALSE;
}
#ifndef _DEBUG
// If the current build is not debug,
// return so the event log is not written.
return TRUE;
#endif
#ifdef UNICODE
// The unicode version of the ReportEvent
// method requires double-byte strings.
PCWSTR arr[1];
arr[0] = data.c_str();
return ::ReportEvent(m_eventLog,
type,
0, 0, NULL, 1,
0, arr, (void*)arr);
#else
// The non-unicode version of the ReportEvent
// method requires single-byte strings.
string multiByte =
CConvert::ToByteString(data);
LPCSTR arr[1];
arr[0] = multiByte.c_str();
return ::ReportEvent(m_eventLog,
type,
0, 0, NULL, 1,
0, arr, (void*)arr);
#endif
}
private:
// Specify the HANDLE to the
// event log for writing.
HANDLE m_eventLog;
};
// The CStoredContext class implements
// the IHttpStoredContext interface.
class CStoredContext : public IHttpStoredContext
{
public:
// The constructor for the CStoredContext
// class. Initializes a CEventWriter to write
// to the Event Log.
CStoredContext() : m_eventWriter(L"IISADMIN")
{
// Create a string to write.
wstring constructor(L"CStoredContext::Constructor");
// Write the string to the Event Log.
m_eventWriter.ReportInfo(constructor);
}
// The Display method writes
// L"CStoredContext::Display"
// to the Event Log.
virtual void Display()
{
// Create the string to write.
wstring display(L"CStoredContext::Display");
// Write the string to the Event Log.
m_eventWriter.ReportInfo(display);
}
// The CleanupStoredContext is the pure virtual
// method that all non-abstract classes implementing
// the IHttpStoredContext must implement.
virtual void CleanupStoredContext()
{
wstring cleanup(L"CStoredContext::CleanupStoredContext");
m_eventWriter.ReportInfo(cleanup);
delete this;
}
protected:
// The protected destructor for the
// CStoredContext class. This method is
// protected because the clients of this
// class should dispose of a class instance
// by calling the CleanupStoredContext method.
virtual ~CStoredContext()
{
wstring destructor(L"CStoredContext::Destructor");
m_eventWriter.ReportInfo(destructor);
}
private:
// Specify the private CEventWriter
// for writing events.
CEventWriter m_eventWriter;
};
// The CGlobalTraceModule class creates the CGlobalModule
// class and registers for GL_TRACE_EVENT events.
class CGlobalContainerModule : public CGlobalModule
{
public:
// Creates the destructor for the
// CGlobalTraceModule class.
virtual ~CGlobalContainerModule()
{
}
// The RegisterGlobalModule method creates and registers
// a new CGlobalTraceModule for GL_TRACE_EVENT events.
// dwServerVersion: the current server version.
// pModuleInfo: the current IHttpModuleRegistrationInfo pointer.
// pGlobalInfo: the current IHttpServer pointer.
// return: ERROR_NOT_ENOUGH_MEMORY if the heap is out of
// memory; otherwise, the value from the call to the
// SetGlobalNotifications method on the pModuleInfo pointer.
static HRESULT RegisterGlobalModule
(
DWORD dwServerVersion,
IHttpModuleRegistrationInfo* pModuleInfo,
IHttpServer* pGlobalInfo
)
{
// The IHttpModuleRegistrationInfo
// pointermust not be NULL.
if (NULL == pModuleInfo)
{
return E_INVALIDARG;
}
// Get the HTTP_MODULE_ID from the
// IHttpModuleRegistrationInfo pointer.
HTTP_MODULE_ID moduleId =
pModuleInfo->GetId();
// The HTTP_MODULE_ID pointer
// must not be NULL.
if (NULL == moduleId)
{
return E_INVALIDARG;
}
// Create a new CGlobalContainerModule pointer
// using the HTTP_MODULE_ID from the
// IHttpModuleRegistrationInfo pointer.
CGlobalContainerModule* containerModule =
new CGlobalContainerModule(moduleId);
// Return an out-of-memory error if the containerModule
// is NULL after the call to the new operator.
if (NULL == containerModule)
{
return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
}
// Attempt to set global notification
// for an GL_TRACE_EVENT event by using
// the traceModule as a listener.
HRESULT hr = pModuleInfo->SetGlobalNotifications
(containerModule, GL_TRACE_EVENT);
// Return the HRESULT from the call to
// the SetGlobalNotifications method.
return hr;
}
// The OnGlobalTraceEvent method is the callback
// method for GL_TRACE_EVENT events in the pipeline.
// pProvider: the IGlobalTraceEventProvider pointer.
// return: GL_NOTIFICATION_CONTINUE.
virtual
GLOBAL_NOTIFICATION_STATUS
OnGlobalTraceEvent
(
IN IGlobalTraceEventProvider* pProvider
)
{
// If the IGlobalTraceEventProvider pointer
// is NULL, return GL_NOTIFICATION_CONTINUE.
if (NULL == pProvider)
{
return GL_NOTIFICATION_CONTINUE;
}
// Declare an IHttpContext pointer.
IHttpContext* httpContext = NULL;
// Declare an HRESULT and initialize
// the HRESULT to E_FAIL.
HRESULT hr = E_FAIL;
// Call the GetCurrentHttpRequestContext
// method on the IGlobalTraceEventProvider
// pointer.
hr = pProvider->GetCurrentHttpRequestContext(&httpContext);
// If the GetCurrentHttpRequestContext
// method failed, or the IHttpContext
// pointer is NULL, return GL_NOTIFICATION_CONTINUE.
if (FAILED(hr) || (NULL == httpContext))
{
return GL_NOTIFICATION_CONTINUE;
}
// Get the IHttpModuleContextContainer
// pointer from the IHttpContext pointer.
IHttpModuleContextContainer* container =
httpContext->GetModuleContextContainer();
// If the IHttpModuleContextContainer is
// NULL, return GL_NOTIFICATION_CONTINUE.
if (NULL == container)
{
return GL_NOTIFICATION_CONTINUE;
}
// Get the IHttpStoredContext pointer
// from the IHttpModuleContextContainer
// pointer.
IHttpStoredContext* storedContext =
container->GetModuleContext(m_moduleId);
// If the IHttpStoredContext pointer is
// non-NULL, use the dynamic_cast operator
// to retrieve the CStoredContext pointer
// from the storedContext.
if (NULL != storedContext)
{
// Attempt to cast the IHttpStoredContext
// pointer to a CStoredContext pointer.
CStoredContext* customContext =
dynamic_cast<CStoredContext*>(storedContext);
// If the cast does not return
// NULL, ask the CStoredContext
// poitner to display itself.
if (NULL != customContext)
{
customContext->Display();
}
}
if (NULL == storedContext)
{
// Create a custom CStoredContext pointer.
IHttpStoredContext* customContext =
new CStoredContext;
// Call the SetModuleContext method using
// the IHttpModuleContextContainer pointer.
container->SetModuleContext(customContext, m_moduleId);
}
// Return GL_NOTIFICATION_CONTINUE.
return GL_NOTIFICATION_CONTINUE;
}
// The Terminate method is required for
// non-abstract CGlobalTraceModule classes.
// This method calls delete on this.
virtual VOID Terminate(VOID)
{
delete this;
}
protected:
// Creates the constructor for the CGlobalTraceModule
// class. This constructor initializes the CEventWriter
// to write to the application event log.
// moduleId: the current module identifier.
CGlobalContainerModule(HTTP_MODULE_ID moduleId)
{
m_moduleId = moduleId;
}
private:
// Specify the HTTP_MODULE_ID
// for this module.
HTTP_MODULE_ID m_moduleId;
};
// The RegisterModule method is the
// main entry point for the DLL.
// dwServerVersion: the current server version.
// pModuleInfo: the current
// IHttpModuleRegistrationInfo pointer.
// pGlobalInfo: the current IHttpServer pointer.
// return: the value returned by calling the
// CGlobalContainerModule::RegisterGlobalModule
// method.
HRESULT
__stdcall
RegisterModule(
DWORD dwServerVersion,
IHttpModuleRegistrationInfo* pModuleInfo,
IHttpServer* pGlobalInfo
)
{
// Call the static method for initialization.
return CGlobalContainerModule::RegisterGlobalModule
(dwServerVersion,
pModuleInfo,
pGlobalInfo);
}
上述程式碼會將新事件寫入事件檢視器,其中 [資料] 方塊包含類似下列的連續字串。
CStoredContext::Destructor
CStoredContext::CleanupStoredContext
CStoredContext::Display
CStoredContext::Constructor
您的模組必須匯出 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 |