Retrieving an Item from a Collection

The following example retrieves the value of PATH_INFO from the ServerVariables collection.

STDMETHODIMP CHowtos1::OnStartPage (IUnknown* pUnk)  
{
  HRESULT hr;  
  IScriptingContext *piContext;
  
  //Get IScriptingContext Interface
  hr = pUnk->QueryInterface(IID_IScriptingContext, (void**)&piContext);

  //Get Response Object Pointer
  IResponse* piResponse = NULL;
  hr = piContext->get_Response(&piResponse);

  //Get Request Object Pointer 
  IRequest* piRequest = NULL;
  hr = piContext->get_Request(&piRequest);
  
  //Get ServerVariables Pointer
  IRequestDictionary *piRequestDict = NULL;
  hr = piRequest->get_ServerVariables(&piRequestDict);

  VARIANT vtIn, vtOut;
  VariantInit(&vtIn);
  VariantInit(&vtOut);

  V_VT(&vtIn) = VT_BSTR;
  V_BSTR(&vtIn) = SysAllocString(OLESTR("PATH_INFO"));

  //Get the PATH_INFO Item 
  hr = piRequestDict->get_Item(vtIn, &vtOut);

  //vtOut Contains an IDispatch Pointer.  To fetch the value
  //for PATH_INFO you must get the Default Value for the 
  //Object stored in vtOut using VariantChangeType.
  if (V_VT(&vtOut) != VT_BSTR)
  VariantChangeType(&vtOut, &vtOut, 0, VT_BSTR);

  //Write out the PATH_INFO to the HTML Page
  piResponse->Write(vtOut);

  piRequestDict->Release();
  piResponse->Release();
  piRequest->Release();
  piContext->Release();
      
  return S_OK;
}