IHttpModuleRegistrationInfo::SetRequestNotifications, méthode

Enregistre les notifications au niveau de la demande pour un module.

Syntaxe

virtual HRESULT SetRequestNotifications(  
   IN IHttpModuleFactory* pModuleFactory,  
   IN DWORD dwRequestNotifications,  
   IN DWORD dwPostRequestNotifications  
) = 0;  

Paramètres

pModuleFactory
[IN] Pointeur vers une interface IHttpModuleFactory .

dwRequestNotifications
[IN] Valeur de masque de bits qui contient les notifications de demande à inscrire. (Défini dans Httpserv.h.)

dwPostRequestNotifications
[IN] Valeur de masque de bits qui contient les notifications post-événement à inscrire. (Défini dans Httpserv.h.)

Valeur renvoyée

Élément HRESULT. Les valeurs possibles sont notamment celles figurant dans le tableau suivant.

Valeur Description
S_OK Indique que l’opération a réussi.
ERROR_ALREADY_EXISTS Indique que le module a déjà été inscrit.

Remarques

La SetRequestNotifications méthode enregistre les notifications au niveau de la demande pour une classe CHttpModule . Un module peut s’inscrire à deux événements pour chaque notification : la notification d’événement, comme indiqué par le masque de bits dans le dwRequestNotifications paramètre, et la notification post-événement, comme indiqué par le masque de bits dans le dwPostRequestNotifications paramètre .

Par exemple, un module HTTP peut s’inscrire à la notification RQ_AUTHENTICATE_REQUEST et à la notification post-événement pour cette même notification. Ce faisant, le module peut fournir des fonctionnalités de traitement supplémentaires pour la notification d’événement et propre tous les détails de traitement dans la notification post-événement.

Notes

Certains événements n’ont pas de notification post-événement. Utilisez 0 pour le dwPostRequestNotifications paramètre lorsque vous ne souhaitez pas de notification ou lorsque la notification post-événement n’est pas prise en charge.

Notes

Les valeurs du masque de bits pour les notifications au niveau de la demande sont définies dans le fichier Httpserv.h.

La SetRequestNotifications méthode nécessite un pointeur vers une interface IHttpModuleFactory, qu’IIS utilisera pour créer un instance d’une CHttpModule classe. Cette fabrique doit gérer la création du instance de la classe et le CHttpModule retour des messages d’erreur si la classe ne peut pas être créée.

Exemple

L’exemple suivant montre comment créer un module HTTP qui utilise la fonction RegisterModule et les méthodes suivantes pour inscrire un module pour les notifications au niveau global et au niveau de la demande.

Notes

Les entrées dans le observateur d'événements affichent « IISADMIN » comme source de l’événement.

#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>

// Create a global handle for the Event Viewer.
HANDLE g_hEventLog;

// Define the method that writes to the Event Viewer.
BOOL WriteEventViewerLog(LPCSTR szBuffer[], WORD wNumStrings);

// Create the HTTP module class.
class MyHttpModule : public CHttpModule
{
public:
    REQUEST_NOTIFICATION_STATUS
    OnBeginRequest(
        IN IHttpContext * pHttpContext,
        IN IHttpEventProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pHttpContext );
        UNREFERENCED_PARAMETER( pProvider );

        // Create an array of strings.
        LPCSTR szBuffer[2] = {"MyHttpModule","OnBeginRequest"};
        // Write the strings to the Event Viewer.
        WriteEventViewerLog(szBuffer,2);

        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }
};

// Create the module's global class.
class MyGlobalModule : public CGlobalModule
{
public:
    GLOBAL_NOTIFICATION_STATUS
    OnGlobalPreBeginRequest(
        IN IPreBeginRequestProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );
        
        // Create an array of strings.
        LPCSTR szBuffer[2] = {"MyGlobalModule","OnGlobalPreBeginRequest"};
        // 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.
        g_hEventLog = RegisterEventSource( NULL,"IISADMIN" );
    }

    ~MyGlobalModule()
    {
        // Test whether the handle for the Event Viewer is open.
        if (NULL != g_hEventLog)
        {
            DeregisterEventSource( g_hEventLog );
            g_hEventLog = NULL;
        }
    }
};

// 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;
    }
};

// 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 != g_hEventLog)
    {
        // Write any strings to the Event Viewer and return.
        return ReportEvent(
            g_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 HRESULT to receive return values from methods.
    HRESULT hr;

    // Set the request notifications.
    hr = pModuleInfo->SetRequestNotifications(
        new MyHttpModuleFactory,
        RQ_BEGIN_REQUEST, 0 );

    // Test for an error and exit if necessary.
    if (FAILED(hr))
    {
        return hr;
    }

    // Set the request priority.
    hr = pModuleInfo->SetPriorityForRequestNotification(
        RQ_BEGIN_REQUEST,PRIORITY_ALIAS_MEDIUM);

    // Test for an error and exit if necessary.
    if (FAILED(hr))
    {
        return hr;
    }

    // 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.
    hr = pModuleInfo->SetGlobalNotifications(
        pGlobalModule, GL_PRE_BEGIN_REQUEST );

    // Test for an error and exit if necessary.
    if (FAILED(hr))
    {
        return hr;
    }

    // Set the global priority.
    hr = pModuleInfo->SetPriorityForGlobalNotification(
        GL_PRE_BEGIN_REQUEST,PRIORITY_ALIAS_LOW);

    // Test for an error and exit if necessary.
    if (FAILED(hr))
    {
        return hr;
    }

    // Return a success status;
    return S_OK;
}

Votre module doit exporter la RegisterModule fonction. Vous pouvez exporter cette fonction en créant un fichier de définition de module (.def) pour votre projet, ou vous pouvez compiler le module à l’aide du /EXPORT:RegisterModule commutateur . Pour plus d’informations, consultez Procédure pas à pas : création d’un module HTTP Request-Level à l’aide de code natif.

Vous pouvez éventuellement compiler le code à l’aide de la __stdcall (/Gz) convention d’appel au lieu de déclarer explicitement la convention d’appel pour chaque fonction.

Spécifications

Type Description
Client - IIS 7.0 sur Windows Vista
- IIS 7.5 sur Windows 7
- IIS 8.0 sur Windows 8
- IIS 10.0 sur Windows 10
Serveur - IIS 7.0 sur Windows Server 2008
- IIS 7.5 sur Windows Server 2008 R2
- IIS 8.0 sur Windows Server 2012
- IIS 8.5 sur Windows Server 2012 R2
- IIS 10.0 sur Windows Server 2016
Produit - 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
En-tête Httpserv.h

Voir aussi

IHttpModuleRegistrationInfo, interface
IHttpModuleRegistrationInfo::SetGlobalNotifications, méthode
IHttpModuleRegistrationInfo::SetPriorityForGlobalNotification, méthode
IHttpModuleRegistrationInfo::SetPriorityForRequestNotification, méthode
PFN_REGISTERMODULE, fonction