CHttpModule 클래스의 새 인스턴스를 초기화합니다.
구문
CHttpModule (
);
매개 변수
이 메서드는 매개 변수를 사용하지 않습니다.
설명
클래스의 CHttpModule 생성자는 클래스 protected 의 직접 생성을 방지하는 것입니다 CHttpModule . 파생 클래스가 구현해야 하는 순수 virtual 메서드를 정의하므로 파생 CHttpModuleCHttpModule클래스를 만들어야 합니다.
예시
다음 코드 예제에서는 RQ_BEGIN_REQUEST 이벤트 알림에 등록 하는 요청 수준 HTTP 모듈을 만드는 방법을 보여 줍니다. 요청이 통합된 요청 처리 파이프라인에 들어가면 IIS는 예제 모듈의 OnBeginRequest 메서드를 호출합니다. 이 메서드는 이벤트 데이터에 "헬로 월드!"이 포함된 Windows 이벤트 뷰어 애플리케이션 로그에 항목을 작성합니다. 처리가 완료되면 모듈이 종료됩니다.
#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>
// Create the module class.
class MyHttpModule : public CHttpModule
{
public:
// Process an RQ_BEGIN_REQUEST event.
REQUEST_NOTIFICATION_STATUS
OnBeginRequest(
IN IHttpContext * pHttpContext,
IN IHttpEventProvider * pProvider
)
{
UNREFERENCED_PARAMETER( pHttpContext );
UNREFERENCED_PARAMETER( pProvider );
WriteEventViewerLog("Hello World!");
return RQ_NOTIFICATION_CONTINUE;
}
//
MyHttpModule()
{
// Open a handle to the Event Viewer.
m_hEventLog = RegisterEventSource( NULL,"IISADMIN" );
}
~MyHttpModule()
{
// Test whether the handle for the Event Viewer is open.
if (NULL != m_hEventLog)
{
// Close the handle to the Event Viewer.
DeregisterEventSource( m_hEventLog );
m_hEventLog = NULL;
}
}
private:
// Create a handle for the event viewer.
HANDLE m_hEventLog;
// Define a method that writes to the Event Viewer.
BOOL WriteEventViewerLog(LPCSTR szNotification)
{
// Test whether the handle for the Event Viewer is open.
if (NULL != m_hEventLog)
{
// Write any strings to the Event Viewer and return.
return ReportEvent(
m_hEventLog,
EVENTLOG_INFORMATION_TYPE,
0, 0, NULL, 1, 0, &szNotification, NULL );
}
return FALSE;
}
};
// Create the module's class factory.
class MyHttpModuleFactory : public IHttpModuleFactory
{
public:
HRESULT
GetHttpModule(
OUT CHttpModule ** ppModule,
IN IModuleAllocator * pAllocator
)
{
UNREFERENCED_PARAMETER( pAllocator );
// Create a new instance.
MyHttpModule * pModule = new MyHttpModule;
// 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(
// Specify the class factory.
new MyHttpModuleFactory,
// Specify the event notifications.
RQ_BEGIN_REQUEST,
// Specify no post-event notifications.
0
);
}
모듈은 RegisterModule 함수를 내보내야 합니다. 프로젝트에 대한 모듈 정의(.def) 파일을 만들어 이 함수를 내보내거나 스위치를 사용하여 /EXPORT:RegisterModule 모듈을 컴파일할 수 있습니다. 자세한 내용은 연습: 네이티브 코드를 사용하여 요청 수준 HTTP 모듈 만들기를 참조 하세요.
필요에 따라 각 함수에 대한 호출 규칙을 명시적으로 선언하는 대신 호출 규칙을 사용하여 __stdcall (/Gz) 코드를 컴파일할 수 있습니다.
요구 사항
| Type | 설명 |
|---|---|
| 클라이언트 | - 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 |
| Product | - 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 |