IHttpFileInfo::GetVrToken Method


Returns the virtual token for the corresponding file.


virtual HANDLE GetVrToken(  
) const = 0;  


This method takes no parameters.

Return Value

A HANDLE to the virtual token; otherwise, NULL.


The behavior of the GetVrToken method is implementation specific. You should use the following information as a guideline, but it may not be correct in all scenarios:

  • Implementers that reference an Internet file return a valid HANDLE.

  • Implementers that reference a non-Internet file return NULL.


Because some implementers return NULL on the GetVrToken method, always test for this condition before using the returned HANDLE.

Notes for Implementers

IHttpFileInfo implementers are responsible for resource management with this data; therefore, IHttpFileInfo implementers must call CloseHandle on the HANDLE when it is no longer needed.

Notes for Callers

IHttpFileInfo implementers are responsible for resource management with this data; therefore, IHttpFileInfo clients must not call CloseHandle on the returned HANDLE when this data is no longer needed. Furthermore, clients must not change the state of the memory referenced by this HANDLE; otherwise, an access violation will be thrown or the data will become invalid.


The following code example demonstrates how to use the IHttpContext::GetFileInfo method to create an HTTP module that retrieves a pointer to an IHttpFileInfo interface for the current request. The example then calls the GetVrToken method to retrieve virtual token information for the requested file and displays this information to a Web client.

#pragma warning( disable : 4290 )
#pragma warning( disable : 4530 )

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

#include <string>
using namespace std;

// The CConvert class mirrors the Convert class that is 
// defined in the .NET Framework. It converts primitives 
// and other data types to wstring types.
class CConvert
    // The ToString method converts a HANDLE to a wstring.
    // h: the HANDLE to convert to a wstring.
    // return: the HANDLE as a wstring.
    static wstring ToString(HANDLE h)
        // If the HANDLE is NULL, return the "NULL" string.
        if (NULL == h)
            return L"NULL";

        // If the HANDLE is not valid, return 
        // the INVALID_HANDLE_VALUE as a string.
        if (INVALID_HANDLE_VALUE == h)
            return L"INVALID_HANDLE_VALUE";

        // The HANDLE is valid.
        return L"valid";

    // The ToByteString converts a double-byte 
    // character string to a single-byte string.
    // str: the double-byte string to convert.
    // return: a single-byte string copied from str.
    static string ToByteString(const wstring& str)
        // Get the length of the 
        // double-byte string.
        size_t length = str.length();

        // Create a temporary char pointer.
        char* byteChar = new char[length+1];
        byteChar[0] = '\0';
        // Copy the double-byte character string
        // into the single-byte string.        
        size_t charsReturned = 0;
        wcstombs_s(&charsReturned, byteChar, 
                   length+1, str.c_str(), length+1);
        // Create a string to return.
        string retString = byteChar;
        // Delete the temporary string and
        // set that string to NULL.
        delete[] byteChar;
        byteChar = NULL;

        // Return the single-byte string.
        return retString;

// The CResponseWriter class writes 
// text to the response stream.
class CResponseWriter
    // Creates the CResponseWriter class.
    // response: the IHttpResponse 
    // pointer to write to.
    // throws: a _com_error exception if
    // the IHttpResponse pointer is NULL.
    CResponseWriter(IHttpResponse* response)
        throw (_com_error)
        // If the response is NULL,
        // throw an exception.
        if (NULL == response)

        // Set the internal response.
        m_response = response;

    // The destructor for the CResponseWriter
    // class. The CResponseWriter method 
    // sets the IHttpResponse pointer to NULL.
    virtual ~CResponseWriter()
        m_response = NULL;
    // The Write method writes the 
    // PCSTR to the response stream.
    // pszValue: the PCSTR to write.
    // throws: a _com_error if the 
    // Write method fails.
    void Write
        PCSTR pszValue        
    ) throw (_com_error)
        // The string must not be 
        // NULL, and its length must 
        // be greater than 0.
        if ((NULL == pszValue) || 
            (strlen(pszValue) < 1))            
            // Throw an invalid argument
            // exception.

        // Create a data chunk structure.
        HTTP_DATA_CHUNK dataChunk;
        // Set the chunk to a chunk in memory.
        dataChunk.DataChunkType = 

        // Set the chunk to 
        // the pszValue parameter.
        dataChunk.FromMemory.pBuffer = 
        // Set the chunk size 
        // to the pszValue length.
        dataChunk.FromMemory.BufferLength = 
        // Declare and initialize OUT 
        // parameters for the WriteEntityChunks 
        // method.
        DWORD pcbSent = 0;
        BOOL pfCompletionExpected = FALSE;

        // Write the entity chunks to
        // the response stream.
        HRESULT hr =

        // Throw an exception if the call
        // to WriteEntityChunks is not a 
        // success.
    // The WriteLine method writes a string
    // and newline characters to the response steam.
    // pszValue: the PCSTR string to write.
    // throws: a _com_error if the PCSTR 
    // or the newline characters are not 
    // written to the response stream.
    void WriteLine
        PCSTR pszValue        
    ) throw (_com_error)
        // Try to write the pszValue parameter.
        // Try to write the newline characters.
    // Convenience method that writes a name 
    // and value pair to the response stream.
    // name: the name of the parameter.
    // value: the value of the parameter.
    // throws: a _com_error exception if
    // the response stream is not written.
    void WriteLine
        const wstring& name,
        const wstring& value
    ) throw (_com_error)
        // Convert the UNICODE strings 
        // to mutlibyte strings.
        string nameMulti = 
        string valueMulti = 

        // Create the string to write.
        string writeString =
            nameMulti + 
            string(":  ") + 

        // Write the line to
        // the response stream.

    // Tests an HRESULT for success.
    // hr: the HRESULT value to inspect.
    // throws: a _com_error if the HRESULT
    // parameter is not S_OK.
    static void ThrowOnFail(HRESULT hr)
        if (FAILED(hr))
            _com_error ce(hr);
            throw ce;
    // Specify the IHttpResponse
    // pointer to write to.
    IHttpResponse* m_response;

// The MyHttpModule class extends the CHttpModule 
// class and handles response processing.  This 
// class handles retrieving an IHttpFileInfo pointer 
// from the IHttpContext pointer during the overridden 
// OnSendResponse method, and writes the IHttpFileInfo 
// data to the response stream.
class MyHttpModule : public CHttpModule
    // The MyHttpModule method is the public 
    // constructor for the MyHttpModule class.


    // The OnSendResponse method 
    // handles respose processing.
    // pHttpContext: an IHttpContext pointer.
    // pProvider: an ISendResponseProvider pointer.
    // IHttpFileInfo pointer can be retrieved from the 
    // pHttpContext parameter; otherwise, 
        IN IHttpContext* pHttpContext,
        IN OUT ISendResponseProvider* pProvider
        // Use the UNREFERENCED_PARAMETER macro since
        // the pProvider parameter is never used.

        if (NULL == pHttpContext)
            return RQ_NOTIFICATION_CONTINUE;

        // Get an IHttpFileInfo pointer 
        // from the IHttpContext pointer.
        IHttpFileInfo* fileInfo = 

        // Get the IHttpResponse pointer 
        // from the IHttpContext pointer.
        IHttpResponse* response = 

        // Both the IHttpFileInfo pointer
        // and IHttpResponse pointer are
        // required for processing.
        if ((NULL == fileInfo) ||
            (NULL == response))
            return RQ_NOTIFICATION_CONTINUE;

        // Clear the existing response stream.        

        // Set the response header to plain text.
        HRESULT hr = 

        // If the SetHeader method fails,
        // call the SetStatus with an error code.
        if (FAILED(hr))
                                0, hr);
            return RQ_NOTIFICATION_CONTINUE;

        // The CResponseWriter will throw a 
        // com_exception if it cannot be created, 
        // or if the response stream cannot be written. 
        // Create a new CResponseWriter, and wrap 
        // all calls into that CResponseWriter,
        // with a try-catch statement.
            // Declare a writer for 
            // the response stream.
            CResponseWriter writer(response);

            // Call the GetVrToken method 
            // on the IHttpFileInfo pointer.
            HANDLE vrToken = 

            // Convert the VR token to a wstring.
            wstring vrTokenString = 

            // Write the token information 
            // to the response stream.
            writer.WriteLine(L"VR Token", vrTokenString);
        catch (_com_error& ce)
            // Attempt to get the 
            // description of the error.
            _bstr_t description = 

            // Print the description if 
            // it is not empty.
            if (0 == description.length())
                description = ce.ErrorMessage();            
            // Set an error status on the 
            // IHttpResponse pointer.
            response->SetStatus(500, description, 0, ce.Error());            

        // Return RQ_NOTIFICATION_FINISH_REQUEST, which 
        // will terminate any additional request processing.
    // The MyHttpModule is the protected virtual
    // constructor for the MyHttpModule class.
    virtual ~MyHttpModule()


// The MyHttpModuleFactory class implements the IHttpModuleFactory 
// interface, and creates a new MyHttpModule pointer, where this
// MyHttpModule listens for RQ_SEND_RESPONSE events.
class MyHttpModuleFactory : public IHttpModuleFactory
    // The RegisterGlobalModule method creates a new 
    // MyHttpModuleFactory pointer, assigns that pointer 
    // as the factory on the pModuleInfo parameter, and
    // registers for RQ_SEND_RESPONSE events.
    // dwServerVersion: the current server version.
    // pModuleInfo: the IHttpModuleRegistrationInfo 
    // used for requesting notifications.
    // pGlobalInfo: the IHttpServer pointer.
    static HRESULT RegisterGlobalModule
        DWORD dwServerVersion,
        IHttpModuleRegistrationInfo* pModuleInfo,
        IHttpServer* pGlobalInfo
        // Create a new MyHttpModuleFactory pointer.
        MyHttpModuleFactory* factory =
            new MyHttpModuleFactory;

        // If the factory is NULL, return a
        // return a ERROR_NOT_ENOUGH_MEMORY 
        // errror.
        if (NULL == factory)

        // Return the value from calling the 
        // SetRequestNotifications method by 
        // passing the factory for RQ_SEND_RESPONSE
        // events.
        return pModuleInfo->SetRequestNotifications(

    // The GetHttpModule method creates a new MyHttpModule 
    // pointer and sets the ppModule with this pointer.
    // ppModule: the OUT parameter which 
    // accepts a new CHttpModule subclass.
    // pAllocator: the IModuleAllocator pointer, 
    // which is unused in this implementation.
    virtual HRESULT GetHttpModule
        OUT CHttpModule** ppModule, 
        IN IModuleAllocator* pAllocator
        // Call the UNREFERENCED_PARAMETER with 
        // the IModuleAllocator pointer, because
        // this parameter is never used.

        // Create a new MyHttpModule pointer and assign
        // it to the dereferenced ppModule pointer.
        *ppModule = new MyHttpModule;

        // If the dereferenced ppModule is NULL 
        // then heap memory is exhausted. Return
        // an ERROR_NOT_ENOUGH_MEMORY error.
        if (NULL == *ppModule)

        // Creating a new MyHttpModule pointer 
        // was successful; return S_OK.
        return S_OK;       

    // The Terminate method must be implemented
    // by IHttpModuleFactory implementers, 
    // and calls delete on this.
    virtual void Terminate()
        delete this;

    // The MyHttpModuleFactory is the 
    // protected constructor for the 
    // MyHttpModuleFactory class.


    // The MyHttpModuleFactory method is the 
    // protected virtual destructor for the 
    // MyHttpModuleFactory class.
    virtual ~MyHttpModuleFactory()


// The RegisterModule method is the 
// main entry point for the DLL.
// dwServerVersion: the current server version.
// pModuleInfo: the current 
// IHttpModuleRegistrationInfo pointer.
// pGlobalInfo: the current IHttpServer pointer.
// return: the HRESULT value returned from 
// the MyHttpModuleFactory::RegisterGlobalModule
// static method.
    DWORD dwServerVersion,
    IHttpModuleRegistrationInfo* pModuleInfo,
    IHttpServer* pGlobalInfo
    // Register for global events using the MyHttpModuleFactory 
    // class' static RegisterGlobalModule method.
    return MyHttpModuleFactory::RegisterGlobalModule
        (dwServerVersion, pModuleInfo, pGlobalInfo);  

The following text is a sample of the data output from the preceding code example.

HTTP/1.1 200 OK  
Server: Microsoft-IIS/7.0  
Date: Thu, 15 Dec 2005 22:22:09 GMT  
Content-Length: 17  
VR Token:  NULL  

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.


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 Technical Preview
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

See Also

IHttpFileInfo Interface