共用方式為


IHttpApplicationProvider::GetApplication 方法

擷取 IHttpApplication 介面。

語法

virtual IHttpApplication* GetApplication(  
   VOID  
) = 0;  

參數

此方法不會採用任何參數。

傳回值

介面的 IHttpApplication 指標。

備註

開發人員可以使用 GetApplication 方法來擷取 IHttpApplication 介面,然後可用來擷取應用程式的組態資訊。

範例

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

  1. 註冊 GL_APPLICATION_START 通知。

  2. 建立包含OnGlobalApplicationStart方法的CGlobalModule類別。 這個方法會執行下列工作:

    1. IHttpApplication使用 GetApplication 方法擷取介面。

    2. 使用 IHttpApplication::GetApplicationPhysicalPath 方法擷取目前內容應用程式的實體路徑。

    3. 將實體路徑資訊當做事件寫入事件檢視器的應用程式記錄檔。

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

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

// Create the module's global class.
class MyGlobalModule : public CGlobalModule
{
public:
    GLOBAL_NOTIFICATION_STATUS
    OnGlobalApplicationStart(
        IN IHttpApplicationStartProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );
        
        // Retrieve a pointer to the IHttpApplication class.
        IHttpApplication * pHttpApplication =
            pProvider->GetApplication();

        // Retrieve a pointer to the application configuration path.
        PCWSTR pwszPhysicalPath =
            pHttpApplication->GetApplicationPhysicalPath();

        // Test for an error.
        if (m_pHttpContext!=NULL)
        {
            // Allocate space for the user name.
            char * pszBuffer = (char*) m_pHttpContext->AllocateRequestMemory(
                (DWORD) wcslen(pwszPhysicalPath)+1 );
        
            // Test for an error.
            if (pszBuffer!=NULL)
            {
                // Return the user information to the Web client.
                wcstombs(pszBuffer,pwszPhysicalPath,wcslen(pwszPhysicalPath));
                // Create an array of strings.
                LPCSTR szBuffer[3] = {"OnGlobalApplicationStart",
                    "Application Physical Path:",pszBuffer};
                // Write the strings to the Event Viewer.
                WriteEventViewerLog(szBuffer,3);
            }
        }

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

    GLOBAL_NOTIFICATION_STATUS
    OnGlobalPreBeginRequest(
        IN IPreBeginRequestProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );
        // Retrieve a pointer to the context.
        m_pHttpContext = pProvider->GetHttpContext();
        // 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.
        m_hEventLog = RegisterEventSource( NULL,"IISADMIN" );
        // Initialize the context pointer to NULL.
        m_pHttpContext = 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;
        }
    }

private:

    // Create a handle for the event viewer.
    HANDLE m_hEventLog;
    // Create a pointer for the module context.
    IHttpContext * m_pHttpContext;

    // Define a method that writes to the Event Viewer.
    BOOL WriteEventViewerLog(LPCSTR * lpStrings, 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, lpStrings, 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 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 and exit.
    return pModuleInfo->SetGlobalNotifications(
        pGlobalModule, GL_APPLICATION_START | GL_PRE_BEGIN_REQUEST );
}

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

另請參閱

IHttpApplication 介面
IHttpApplicationProvider 介面