IHttpModuleFactory::GetHttpModule Method
Creates an instance of a CHttpModule class.
Syntax
virtual HRESULT GetHttpModule(
OUT CHttpModule** ppModule,
IN IModuleAllocator* pAllocator
) = 0;
Parameters
ppModule
[OUT] A dereferenced pointer to a CHttpModule class.
pAllocator
[IN] A pointer to an IModuleAllocator interface.
Return Value
An HRESULT
. Possible values include, but are not limited to, those in the following table.
Value | Description |
---|---|
S_OK | Indicates that the operation was successful. |
Note
Because your module factory is required to provide a GetHttpModule
method, you can provide any status code for the return value as appropriate for your application. At the very least, your GetHttpModule
method should return S_OK to indicate a successful completion.
Remarks
An IHttpModuleFactory interface must provide a GetHttpModule
method that creates an instance of your CHttpModule
class. When IIS calls your module's exported RegisterModule function, IIS will use module factory's GetHttpModule
method to create an instance of your CHttpModule
class.
Example
The following code example demonstrates how to create a simple "Hello World" HTTP module. The module defines an exported RegisterModule
function that passes an instance of an IHttpModuleFactory
interface to the IHttpModuleRegistrationInfo::SetRequestNotifications method and registers for the RQ_BEGIN_REQUEST notification. IIS uses the GetHttpModule
method to create an instance of a CHttpModule
class and returns a success status. IIS also uses the Terminate method of the IHttpModuleFactory
interface to remove the factory from memory.
When a RQ_BEGIN_REQUEST
notification is triggered, IIS calls the module's CHttpModule::OnBeginRequest method to process the current request. OnBeginRequest
clears the response buffer and modifies the MIME type for the response. The method then creates a data chunk that contains a "Hello World" string and returns the string to a Web client. Finally, the module returns a status indicator that notifies IIS that all notifications are finished and then exits.
#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
);
}
Your module must export the RegisterModule
function. You can export this function by creating a module definition (.def) file for your project, or you can compile the module by using the /EXPORT:RegisterModule
switch. For more information, see Walkthrough: Creating a Request-Level HTTP Module By Using Native Code.
You can optionally compile the code by using the __stdcall (/Gz)
calling convention instead of explicitly declaring the calling convention for each function.
Requirements
Type | Description |
---|---|
Client | - IIS 7.0 on Windows Vista - IIS 7.5 on Windows 7 - IIS 8.0 on Windows 8 - IIS 10.0 on Windows 10 |
Server | - IIS 7.0 on Windows Server 2008 - IIS 7.5 on Windows Server 2008 R2 - IIS 8.0 on Windows Server 2012 - IIS 8.5 on Windows Server 2012 R2 - IIS 10.0 on Windows Server 2016 |
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 |
Header | Httpserv.h |
See Also
CHttpModule Class
IHttpModuleFactory Interface
Designing Native-Code HTTP Modules
Walkthrough: Creating a Request-Level HTTP Module By Using Native Code