Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 240191 - Last Review: July 13, 2004 - Revision: 3.2
How To Set Cookies Using ASP and Visual C++
This article was previously published under Q240191
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.
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.
- From the Class View tab, right-click the IASPCookies interface and choose "Add Method".
- Enter SendTestCookies as the Method Name and click OK.
- From the Class View tab, expand the IASPCookies interface that is directly below the CASPCookies class in the tree.
- Double-click on the SendTestCookies() method to bring up the implementation of the method.
- 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);
- From the Build menu, select "Build KBWriteCookies.dll" to build your project.
- 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")
%>
- View the ASP page in your Web browser and you should see the following displayed on the page:
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:
159402Â
(http://kbalertz.com/Feedback.aspx?kbNumber=159402/EN-US/
)
How To 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:
239445Â
(http://kbalertz.com/Feedback.aspx?kbNumber=239445/EN-US/
)
How To Obtain ObjectContext with ObjectControl Inside VC COM DLL From ASP and MTS
For additional information, please click the article number(s) below
to view the article(s) in the Microsoft Knowledge Base:
239445Â
(http://kbalertz.com/Feedback.aspx?kbNumber=239445/EN-US/
)
How To Obtain ObjectContext with ObjectControl Inside VC COM DLL from ASP and MTS
166279Â
(http://kbalertz.com/Feedback.aspx?kbNumber=166279/EN-US/
)
How To Lifetime of a COM Component Under IIS, ASP, and RDS
159402Â
(http://kbalertz.com/Feedback.aspx?kbNumber=159402/EN-US/
)
How To How To Use Response.Redirect in a Server Script
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++, 32-bit Learning Edition 6.0
- Microsoft Internet Information Server 4.0
| kbaspobj kbcodesnippet kbhowto KB240191 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate