共用方式為


CGlobalModule::OnGlobalCustomNotification 方法

表示將處理 GlobalCustomNotification 事件的方法,這會在模組引發使用者定義通知時發生。

語法

virtual GLOBAL_NOTIFICATION_STATUS OnGlobalCustomNotification(  
   IN ICustomNotificationProvider* pProvider  
);  

參數

pProvider
[IN] ICustomNotificationProvider 介面的 指標。

傳回值

GLOBAL_NOTIFICATION_STATUS值。

備註

當全域模組已註冊GL_CUSTOM_NOTIFICATION事件通知時,如果模組呼叫IHttpServer::NotifyCustomNotification) ,IIS 將會呼叫模組 OnGlobalCustomNotification 的 方法 (。

注意

全域模組可以在模組的RegisterModule函式中註冊 ,以註冊 GlobalCustomNotificationGL_CUSTOM_NOTIFICATION 事件通知。

範例

下列程式碼範例示範如何建立執行下列工作的 HTTP 模組:

  1. 註冊 GL_PRE_BEGIN_REQUESTGL_CUSTOM_NOTIFICATION 通知。

  2. 建立包含OnGlobalPreBeginRequestOnGlobalCustomNotification方法的CGlobalModule類別。

    • 方法 OnGlobalPreBeginRequest 會將事件寫入指定目前通知的事件檢視器。 方法接著會建立 介面的 ICustomNotificationProvider 實例,並使用 方法引發自訂通知 NotifyCustomNotification

    • 方法 OnGlobalCustomNotification 會使用 ICustomNotificationProvider::QueryNotificationType 方法來擷取自訂通知的唯一識別碼。 如果唯一識別碼相符, OnGlobalCustomNotification 方法會將事件寫入至指定引發自訂通知的事件檢視器。

  3. CGlobalModule從記憶體中移除 類別,然後結束。

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

// Define the unique notification indentifier.
#define MY_CUSTOM_NOTIFICATION L"MyCustomNotification"

// Create a pointer for the global server interface.
IHttpServer * g_pHttpServer = NULL;

// Create the custom notification class.
class MyCustomProvider : public ICustomNotificationProvider
{
public:
    // Create the method that will identify the custom notification.
    PCWSTR QueryNotificationType(VOID)
    {
        // Return the unique identifier string for the custom notification.
        return MY_CUSTOM_NOTIFICATION;
    }
    // Create the method that will process errors.
    VOID SetErrorStatus(HRESULT hrError)
    {
        return;
    }
};

// Create the module's global class.
class MyGlobalModule : public CGlobalModule
{
private:

    // Create a handle for the Event Viewer.
    HANDLE m_hEventLog;
    // Create a pointer for the custom notification.
    MyCustomProvider * m_pCustomProvider;

public:

    MyGlobalModule()
    {
        // Open the global handle to the Event Viewer.
        m_hEventLog = RegisterEventSource( NULL,"IISADMIN" );
        // Initialize the pointer for the custom notification to NULL.
        m_pCustomProvider = NULL;
    }

    ~MyGlobalModule()
    {
        // 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;
        }
        // Test whether the pointer for the custom notification is valid.
        if (NULL != m_pCustomProvider)
        {
            // Remove the custom notification from memory.
            delete m_pCustomProvider;
            m_pCustomProvider = NULL;
        }
    }

    GLOBAL_NOTIFICATION_STATUS
    OnGlobalPreBeginRequest(
        IN IPreBeginRequestProvider * pProvider
    )
    {
        // Create an array of strings.
        LPCSTR szBuffer[2] = {"MyGlobalModule","OnGlobalPreBeginRequest"};
        // Write the strings to the Event Viewer.
        WriteEventViewerLog(szBuffer,2);

        // Create the custom notification provider class.
        MyCustomProvider * m_pCustomProvider = new MyCustomProvider;

        // Test if the server and notification pointers are valid.
        if ((NULL != m_pCustomProvider) && (NULL != g_pHttpServer))
        {
            // Raise the custom notification.
            BOOL fCompletionExpected = TRUE;
            g_pHttpServer->NotifyCustomNotification(m_pCustomProvider);
        }

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

    GLOBAL_NOTIFICATION_STATUS
    OnGlobalCustomNotification(
        IN ICustomNotificationProvider * pProvider
    )
    {
        // Retrieve the custom notification type;
        PCWSTR pNotificationType = pProvider->QueryNotificationType();

        // Test if the custom notification is correct.
        if (0 == wcscmp(pNotificationType,MY_CUSTOM_NOTIFICATION))
        {
            // Create an array of strings.
            LPCSTR szBuffer[2] = {"MyGlobalModule","OnGlobalCustomNotification"};
            // 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;
    }

private:

    // Create 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 != m_hEventLog)
        {
            // Write any strings to the Event Viewer and return.
            return ReportEvent(
                m_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;

    // Store the pointer for the global server interface.
    g_pHttpServer = pGlobalInfo;

    // 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 | GL_CUSTOM_NOTIFICATION );

    // Test for an error and exit if necessary.
    if (FAILED(hr))
    {
        return hr;
    }
    
    // Return a success status;
    return S_OK;
}

您的模組必須匯出 RegisterModule 函式 。 您可以為專案建立模組定義 (.def) 檔案,或使用 參數編譯模組 /EXPORT:RegisterModule 來匯出此函式。 如需詳細資訊,請參閱逐步解說 :使用機器碼建立Global-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

另請參閱

CGlobalModule 類別
IHttpServer::NotifyCustomNotification 方法