HOWTO: Set Cookies Using ASP and Visual C++

ID: Q240191


The information in this article applies to:
  • Active Server Pages
  • Microsoft Visual C++, 32-bit Editions, version 6.0
  • Microsoft Internet Information Server version 4.0


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:

  1. Open Visual C++ 6.0.


  2. Start the process of creating a new project using the ATL COM AppWizard.


  3. Name the project KBWriteCookies.


  4. From the list of available server types, make sure Dynamic Link Library (DLL) is selected and click Finish.


  5. From the Insert menu, select "New ATL Object".


  6. From the "Objects" category, select "ActiveX Server Component" and click Next.


  7. Enter ASPCookies in the "Short Name" text box, accept the defaults for the other options, and click OK.


  8. From the Class View tab, right-click the IASPCookies interface and choose "Add Method".


  9. Enter SendTestCookies as the Method Name and click OK.


  10. From the Class View tab, expand the IASPCookies interface that is directly below the CASPCookies class in the tree.


  11. Double-click on the SendTestCookies() method to bring up the implementation of the method.


  12. 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);
     


  13. From the Build menu, select "Build KBWriteCookies.dll" to build your project.


  14. 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")
    %> 


  15. View the ASP page in your Web browser and you should see the following displayed on the page:
    
       CookieKey=CookieValue 


There are a few things to note about this sample:
  • 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:
    Q159402 HOWTO: How To Use Response.Redirect in a Server Script


  • 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:
    Q239445 HOWTO: Obtain ObjectContext with ObjectControl Inside VC COM DLL From ASP and MTS



REFERENCES

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

Q239445 HOWTO: Obtain ObjectContext with ObjectControl Inside VC COM DLL from ASP and MTS
Q166279 HOWTO: Lifetime of a COM Component Under IIS, ASP, and RDS
Q159402 HOWTO: How To Use Response.Redirect in a Server Script

Additional query words:

Keywords : kbASP kbASP400 kbASPObj kbCOMt kbVC kbVC600 kbGrpASP kbDSupport
Version : winnt:4.0,6.0
Platform : winnt
Issue type : kbhowto


Last Reviewed: September 22, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.