home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 September / CHIPCD_9_99.iso / software / uaktualnienia / OptionPackPL / iis4_07.cab / power.cpp < prev    next >
C/C++ Source or Header  |  1998-04-27  |  4KB  |  168 lines

  1. // Power.cpp : Implementation of CCATLPwrApp and DLL registration.
  2.  
  3. #include "stdafx.h"
  4. #include "CATLPwr.h"
  5. #include "Power.h"
  6. #include "context.h"
  7. /////////////////////////////////////////////////////////////////////////////
  8. //
  9.  
  10. STDMETHODIMP CPower::InterfaceSupportsErrorInfo(REFIID riid)
  11. {
  12.     static const IID* arr[] = 
  13.     {
  14.         &IID_IPower,
  15.     };
  16.  
  17.     for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
  18.     {
  19.         if (InlineIsEqualGUID(*arr[i],riid))
  20.             return S_OK;
  21.     }
  22.     return S_FALSE;
  23. }
  24.  
  25. // Ctor
  26. CPower::CPower()
  27.     : m_bstrMyProperty(OLESTR("C++ ATL Power Component"))
  28. {}
  29.  
  30. // Get function for myProperty
  31. STDMETHODIMP CPower::get_myProperty(BSTR* pbstrOutValue)
  32. {
  33.     if (pbstrOutValue == NULL)
  34.         return E_POINTER;
  35.  
  36.     // Get Value from Property
  37.     *pbstrOutValue = m_bstrMyProperty.Copy();
  38.     
  39.     return S_OK;
  40. }
  41.  
  42. // Put function for myProperty
  43. STDMETHODIMP CPower::put_myProperty(BSTR bstrInValue)
  44. {
  45.     if (bstrInValue == NULL)
  46.         return E_POINTER;
  47.  
  48.     m_bstrMyProperty = bstrInValue;
  49.  
  50.     return S_OK;
  51. }
  52.  
  53. // Basic Method which Converts bstrIn to Upper Case
  54. STDMETHODIMP CPower::myMethod(BSTR bstrIn, BSTR* pbstrOut)  
  55. {
  56.     if (bstrIn == NULL || pbstrOut == NULL)
  57.         return E_POINTER;
  58.  
  59.     // Create a temporary CComBSTR
  60.     CComBSTR bstrTemp(bstrIn);
  61.  
  62.     if (!bstrTemp)
  63.         return E_OUTOFMEMORY;
  64.  
  65.     // Make string uppercase   
  66.     wcsupr(bstrTemp);  
  67.     
  68.     // Return m_str member of bstrTemp
  69.     *pbstrOut = bstrTemp.Detach();
  70.  
  71.     return S_OK;
  72. }
  73.  
  74.  
  75. ///////////// ASP-Specific Component Methods ////////////////
  76.  
  77. // Get Function Returns the Name of the Current Script
  78. STDMETHODIMP CPower::get_myPowerProperty(BSTR* pbstrOutValue)
  79. {
  80.     // the Context class is an easy way to use IIS 4's new way
  81.     // of getting instrinic objects.  The new method may seem
  82.     // more complex, but it is more powerful since obects no
  83.     // longer have to be page/session level to get access
  84.     // to them.
  85.     CContext cxt;
  86.     if ( FAILED( cxt.Init( CContext::get_Request ) ) )
  87.     {
  88.         return E_FAIL;
  89.     }
  90.  
  91.     // Do we have somewhere valid to store the return value?
  92.     if (pbstrOutValue == NULL)
  93.         return E_POINTER;
  94.  
  95.     // Initialize the return value
  96.     *pbstrOutValue = NULL;
  97.  
  98.     // Get the ServerVariables Collection
  99.     CComPtr<IRequestDictionary> piServerVariables;
  100.     HRESULT hr = cxt.Request()->get_ServerVariables(&piServerVariables);
  101.  
  102.     if (FAILED(hr))
  103.         return hr;
  104.  
  105.     // Get the SCRIPT_NAME item from the ServerVariables collection
  106.     CComVariant vtIn(OLESTR("SCRIPT_NAME")), vtOut;
  107.     hr = piServerVariables->get_Item(vtIn, &vtOut);
  108.  
  109.     if (FAILED(hr))
  110.         return hr;
  111.  
  112.     // Get the SCRIPT_NAME item from the ServerVariables collection
  113.     // vtOut Contains an IDispatch Pointer.  To fetch the value
  114.     // for SCRIPT_NAME, you must get the Default Value for the 
  115.     // Object stored in vtOut using VariantChangeType.
  116.     hr = VariantChangeType(&vtOut, &vtOut, 0, VT_BSTR);
  117.  
  118.     // Copy and return SCRIPT_NAME
  119.     if (SUCCEEDED(hr))
  120.         *pbstrOutValue = ::SysAllocString(V_BSTR(&vtOut));
  121.     
  122.     return hr;
  123. }
  124.  
  125. // ASP-specific Power Method
  126. STDMETHODIMP CPower::myPowerMethod()  
  127. {
  128.     CContext cxt;
  129.     if ( FAILED( cxt.Init( CContext::get_Request | CContext::get_Response ) ) )
  130.     {
  131.         return E_FAIL;
  132.     }
  133.  
  134.     // Get the ServerVariables Collection
  135.     CComPtr<IRequestDictionary> piServerVariables;
  136.     HRESULT hr = cxt.Request()->get_ServerVariables(&piServerVariables);
  137.  
  138.     if (FAILED(hr))
  139.         return hr;
  140.  
  141.     // Get the HTTP_USER_AGENT item from the ServerVariables collection
  142.     CComVariant vtIn(OLESTR("HTTP_USER_AGENT")), vtOut;
  143.     hr = piServerVariables->get_Item(vtIn, &vtOut);
  144.  
  145.     if (FAILED(hr))
  146.         return hr;
  147.  
  148.     // vtOut Contains an IDispatch Pointer.  To fetch the value
  149.     // for HTTP_USER_AGENT, you must get the Default Value for the 
  150.     // Object stored in vtOut using VariantChangeType.
  151.     hr = VariantChangeType(&vtOut, &vtOut, 0, VT_BSTR);
  152.  
  153.     if (SUCCEEDED(hr))
  154.     { 
  155.         // Look for "MSIE" in HTTP_USER_AGENT string
  156.         if (wcsstr(vtOut.bstrVal, L"MSIE") != NULL)
  157.             cxt.Response()->Write(CComVariant(
  158.                 OLESTR("You are using a very powerful browser."))); 
  159.         else
  160.             cxt.Response()->Write(CComVariant(
  161.                 OLESTR("Try Internet Explorer today!")));
  162.     }
  163.         
  164.     return hr;
  165. }
  166.  
  167.  
  168.