Microsoft KB Archive/240191

= How To Set Cookies Using ASP and Visual C++ =

Article ID: 240191

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Active Server Pages 4.0
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition
 * Microsoft Internet Information Server 4.0

-



This article was previously published under Q240191



SUMMARY
This article describes how to write cookies to a Web browser using the Active Server Pages Response object from a COM DLL created with Visual C++.

Unlike writing cookies through script on an ASP page, or from a Visual Basic component, you must use three (3) interfaces implemented by ASP to use this functionality. These interfaces (in order of use) are IResponse, IRequestDictionary, and IWriteCookie.

The functional flow of writing cookies from a VC component is as follows:


 * Obtain the Response object from IScriptingContext or IObjectContext.
 * Use the Response object to obtain an IRequestDictionary containing the Cookies collection.
 * Use the Cookies collection to obtain the IWriteCookie object, which will allow you to write cookies and send them to a browser.



MORE INFORMATION
The following are steps on writing cookies for a Web browser:  Open Visual C++ 6.0. Start the process of creating a new project using the ATL COM AppWizard. Name the project KBWriteCookies. From the list of available server types, make sure Dynamic Link Library (DLL) is selected and click Finish. From the Insert menu, select "New ATL Object". From the "Objects" category, select "ActiveX Server Component" and click Next. Enter ASPCookies in the "Short Name" text box, accept the defaults for the other options, and click OK.</li> From the Class View tab, right-click the IASPCookies interface and choose "Add Method".</li> Enter SendTestCookies as the Method Name and click OK.</li> From the Class View tab, expand the IASPCookies interface that is directly below the CASPCookies class in the tree.</li> Double-click on the SendTestCookies method to bring up the implementation of the method.</li>  Paste the following code into the SendTestCookies method implementation:

HRESULT hr = NOERROR; IRequestDictionary* pDict = NULL;  // First step to get cookie collection IWriteCookie* pWriteCookie = NULL; // Cookie collection VARIANT vtCookieDict;              // ptr to dispinterface IWriteCookie VARIANT vtCookieName;              // For the name of the cookie VARIANT vtCookieKey;               // For the cookie key BSTR bstrCookieName;               // For the cookie name BSTR bstrCookieKey;                // For the key BSTR bstrCookieVal;                // For the value

// Initialize the temp vars bstrCookieName = SysAllocString(L"KBTESTCOOKIES"); bstrCookieKey = SysAllocString(L"CookieKey"); bstrCookieVal = SysAllocString(L"CookieValue");

// Initialize the variants VariantInit(&vtCookieDict); VariantInit(&vtCookieName); VariantInit(&vtCookieKey);

// Set the name of the cookie and change the type // If you don't IRequestDictionary::get_Item will // fail because the variant will come through as VT_EMPTY vtCookieName.bstrVal = bstrCookieName; vtCookieName.vt = VT_BSTR;

// Set the key you'll use for the cookie's key/value pair. // Make sure to change the type or put_Item will fail. vtCookieKey.bstrVal = bstrCookieKey; vtCookieKey.vt = VT_BSTR; // First you have to get the IRequestDictionary // then call get_Item to get the actual IWriteCookie // object. hr = m_piResponse->get_Cookies(&pDict);

if(SUCCEEDED(hr)) {       // Request access to the cookie named "KBTESTCOOKIES" specifically. // vtCookieDict will return with the ptr // to the IWriteCookie object hr = pDict->get_Item(vtCookieName, &vtCookieDict);

if(SUCCEEDED(hr)) {           // Got the pointer to IWriteCookie now make it friendly. pWriteCookie = (IWriteCookie*)(vtCookieDict.pdispVal);

// Fill the Response buffer with the cookie data and we're done hr = pWriteCookie->put_Item(vtCookieKey, bstrCookieVal);

pWriteCookie->Release; pDict->Release; }       else {           pDict->Release; }

}   SysFreeString(bstrCookieName); SysFreeString(bstrCookieKey); SysFreeString(bstrCookieVal); return (hr);

</li> From the Build menu, select "Build KBWriteCookies.dll" to build your project.</li>  Create a new ASP page and paste in the following code and save the file: <% Dim obj Set obj = Server.CreateObject("KBWriteCookies.ASPCookies") Call obj.SendTestCookies Response.Write Request.Cookies("KBTESTCOOKIES") %>                       </li>  View the ASP page in your Web browser and you should see the following displayed on the page: CookieKey=CookieValue </li></ol>

There are a few things to note about this sample: <ul> If the SendTestCookies method from this sample is called after calling any method of the Response object (that is, Write, Redirect, and so forth) or after having written any HTML to the page, it is possible that the call to IWriteCookie::put_Item will return E_FAIL. This is because cookies are set using HTTP headers, and HTTP headers must come before any other data is written to the browser. To work around this issue, simply enable Response buffering for the page.

For additional information, please click the article number below to view the article in the Microsoft Knowledge Base:

159402 How To How To Use Response.Redirect in a Server Script

</li> This article uses the IScriptingContext interface to gain access to the ASP intrinsic objects, however it is strongly recommended that you use Microsoft Transaction Server's ObjectContext to obtain the ASP intrinsic objects, as support for IScriptingContext may be removed from future versions of ASP.

For additional information, please click the article number below to view the article in the Microsoft Knowledge Base:

239445 How To Obtain ObjectContext with ObjectControl Inside VC COM DLL From ASP and MTS

</li></ul>

<div class="references_section">