Compartir a través de


IHttpContext::SetServerVariable (Método)

Establece una variable de servidor en un valor especificado.

Sintaxis

virtual HRESULT SetServerVariable(  
   PCSTR pszVariableName,  
   PCWSTR pszVariableValue  
) = 0;  

Parámetros

pszVariableName
[IN] Puntero a una cadena que contiene el nombre de la variable de servidor que se va a establecer.

pszVariableValue
[IN] Puntero a una cadena que contiene el valor de la variable de servidor que se va a establecer.

Valor devuelto

Una clase HRESULT. Entre los valores posibles se incluyen los que se indican en la tabla siguiente, entre otros.

Valor Descripción
S_OK Indica que la operación se realizó correctamente.
ERROR_NOT_ENOUGH_MEMORY Indica que no hay memoria suficiente para realizar la operación.

Comentarios

El SetServerVariable método especifica los valores de las variables de servidor de Common Gateway Interface (CGI). La variable de servidor especificada por el pszVariableName parámetro puede ser una variable personalizada o una variable definida en Request for Comments (RFC) 3875, "The Common Gateway Interface (CGI) Version 1.1".

Nota:

La variable de servidor especificada por el pszVariableName parámetro se crea si no existe.

Ejemplo

En el ejemplo de código siguiente se muestra cómo usar el SetServerVariable método para crear un módulo HTTP que establece el valor de la SERVER_NAME variable de servidor en un valor de ejemplo.

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

// Create the module class.
class MyHttpModule : 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 request.
        IHttpRequest * pHttpRequest = pHttpContext->GetRequest();

        // Test for an error.
        if (pHttpRequest != NULL)
        {
            // Set the value of an example server variable.
            hr = pHttpContext->SetServerVariable("SERVER_NAME", L"Example Value");

            // Test for an error.
            if (FAILED(hr))
            {
                // Set the error status.
                pProvider->SetErrorStatus( 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 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 );

    // Set the request notifications and exit.
    return pModuleInfo->SetRequestNotifications(
        new MyHttpModuleFactory,
        RQ_BEGIN_REQUEST,
        0
    );
}

El módulo debe exportar la función RegisterModule . Puede exportar esta función mediante la creación de un archivo de definición de módulo (.def) para el proyecto, o bien puede compilar el módulo mediante el /EXPORT:RegisterModule modificador . Para obtener más información, vea Tutorial: Creación de un módulo HTTP de Request-Level mediante código nativo.

Puede compilar el código mediante la __stdcall (/Gz) convención de llamada en lugar de declarar explícitamente la convención de llamada para cada función.

Requisitos

Tipo Descripción
Remoto - IIS 7.0 en Windows Vista
- IIS 7.5 en Windows 7
- IIS 8.0 en Windows 8
- IIS 10.0 en Windows 10
Servidor - IIS 7.0 en Windows Server 2008
- IIS 7.5 en Windows Server 2008 R2
- IIS 8.0 en Windows Server 2012
- IIS 8.5 en Windows Server 2012 R2
- IIS 10.0 en Windows Server 2016
Producto - 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
Encabezado Httpserv.h

Consulte también

IHttpContext (interfaz)
IHttpContext::GetServerVariable (Método)