Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Associates an asynchronous completion operation with the thread pool.
Syntax
virtual HRESULT AssociateWithThreadPool(
IN HANDLE hHandle,
IN LPOVERLAPPED_COMPLETION_ROUTINE completionRoutine
) = 0;
Parameters
hHandle
[IN] A HANDLE
for the asynchronous operation.
completionRoutine
[IN] An LPOVERLAPPED_COMPLETION_ROUTINE
function pointer.
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. |
Remarks
Developers can use the AssociateWithThreadPool
method to associate a handle for an asynchronous input/output operation with the IIS thread pool. Because the asynchronous operation associated with that handle will complete within the IIS thread pool, the AssociateWithThreadPool
method allows you to perform a series of asynchronous operations without maintaining your own thread pool.
Example
The following code example demonstrates how to use the AssociateWithThreadPool
method to create an HTTP module that creates a condition that requires an asynchronous input/output operation and associates the asynchronous completion routine with the IIS thread pool.
#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>
// Create a pointer for the global server interface.
IHttpServer * g_pGlobalInfo = NULL;
// Create a global file handle.
HANDLE g_hFile = NULL;
// Create a utility method for asynchronous completion.
VOID
__stdcall
MyCompletionRoutine(
DWORD dwErrorCode,
DWORD dwNumberOfBytesTransfered,
LPOVERLAPPED lpOverlapped)
{
if ((g_hFile != NULL) && (g_hFile != INVALID_HANDLE_VALUE))
{
CloseHandle(g_hFile);
}
return;
}
// Create the module class.
class MyHttpModule : public CHttpModule
{
public:
REQUEST_NOTIFICATION_STATUS
OnSendResponse(
IN IHttpContext * pHttpContext,
IN ISendResponseProvider * pProvider
)
{
UNREFERENCED_PARAMETER( pHttpContext );
UNREFERENCED_PARAMETER( pProvider );
BOOL fAuthenticated = FALSE;
// Retrieve an IHttpUser interface.
IHttpUser * pHttpUser = pHttpContext->GetUser();
char * pszUserName = NULL;
PCWSTR pwszUserName = NULL;
// Test for an error.
if (pHttpUser != NULL)
{
// Retrieve the user name.
pwszUserName = pHttpUser->GetUserName();
// Test for an error.
if (pwszUserName!=NULL)
{
// Test for anonymous user.
if (wcslen(pwszUserName)>0)
{
// Set the flag to indicate an authenticated user.
fAuthenticated = TRUE;
}
}
}
// Test for an authenticated user.
if (fAuthenticated == FALSE)
{
// Clear the existing response.
pHttpContext->GetResponse()->Clear();
// Return an access denied message.
pHttpContext->GetResponse()->SetStatus(401,"Access denied.",0,0);
// End additional processing.
return RQ_NOTIFICATION_FINISH_REQUEST;
}
g_hFile = CreateFile(TEXT("d:\\inetpub\\wwwroot\\myfile.txt"),
GENERIC_WRITE,0,NULL,CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
if (g_hFile == INVALID_HANDLE_VALUE)
{
char szBuffer[256] = "";
sprintf_s(szBuffer,256,"Could not open file (error %d)\n", GetLastError());
WriteResponseMessage(pHttpContext,szBuffer);
return RQ_NOTIFICATION_FINISH_REQUEST;
}
else
{
OVERLAPPED oOverlapped;
oOverlapped.hEvent = NULL;
oOverlapped.Offset = 0;
oOverlapped.OffsetHigh = 0;
DWORD dwBytesWritten = 0;
g_pGlobalInfo->AssociateWithThreadPool(g_hFile,&MyCompletionRoutine);
//pszUserName = (char*) pHttpContext->AllocateRequestMemory( (DWORD) wcslen(pwszUserName)+1 );
//wcstombs(pszUserName,pwszUserName,wcslen(pwszUserName));
//WriteFile(g_hFile, pszUserName, strlen(pszUserName), &dwBytesWritten, &oOverlapped);
WriteFile(g_hFile, "Hello", strlen("Hello"), &dwBytesWritten, oOverlapped);
return RQ_NOTIFICATION_PENDING;
}
}
private:
// Create a utility method that inserts a string value into the response.
HRESULT WriteResponseMessage(
IHttpContext * pHttpContext,
PCSTR pszBuffer
)
{
// Create an HRESULT to receive return values from methods.
HRESULT hr;
// Create a data chunk. (Defined in the Http.h file.)
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 = pHttpContext->GetResponse()->WriteEntityChunks(
&dataChunk,1,FALSE,TRUE,&cbSent);
// Test for an error.
if (FAILED(hr))
{
// Return the error status.
return hr;
}
// Return a success status.
return S_OK;
}
};
// 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 );
// Store the pointer for the global server interface.
g_pGlobalInfo = pGlobalInfo;
// Set the request notifications and exit.
return pModuleInfo->SetRequestNotifications(
new MyHttpModuleFactory,
RQ_SEND_RESPONSE,
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 |