Reading Cookies (Tough one!)


2003-11-21 03:28:15 AM
cppbuilder40
I wrote a function for use in my Active Server Object that is meant to read cookies. It gives some very strange behavior in that it won't work at all until I refresh my web page 6 times (yes, exactly 6 times, every time), then only the first request for a cookie value works. Sounds like I'm possible not properly allocating/deallocating memory or something? Anyway, below is the function, any thoughts?
BTW: You can see commented lines where I have been trying different things.
Thanks in advance to you all,
-Brandon
AnsiString TBrandonsImpl::get_cookie( AnsiString name, AnsiString key )
{
HRESULT result = NOERROR;
IRequestDictionary* CookiesDict = NULL;
IReadCookie* ReadCookie = NULL;
TVariant TheCookie;
AnsiString mycookieval = "Undefined";
int numkeys = 0;
VARIANT vtCookieDict; // ptr to dispinterface IWriteCookie
VARIANT vtCookieName; // For the name of the cookie
VARIANT vtCookieKey; // For the cookie key
VARIANT vtCookieVal; // For the cookie value
VariantInit(&vtCookieDict);
VariantInit(&vtCookieName);
VariantInit(&vtCookieKey);
//VariantInit(&vtCookieVal);
//
// 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 = BSTR(WideString(name));
vtCookieName.vt = VT_BSTR;
//
// Because this is a "simple" cookie (as opposed to a named cookie), we'll set
// the Variant Type to VT_ERROR and the error to DISP_E_PARAMNOTFOUND. This
// essentially makes the Varient an "Automation optional parameter" when
// passed into a function.
//
if( key == "" )
{
vtCookieKey.bstrVal = BSTR(WideString(key));
vtCookieKey.vt = VT_ERROR;
V_ERROR(&vtCookieKey) = DISP_E_PARAMNOTFOUND;
}
else
{
vtCookieKey.bstrVal = BSTR(WideString(key));
vtCookieKey.vt = VT_BSTR;
}
aspwrite( "Getting the cookie dictionary...<br>" );
//
// First you have to get the IRequestDictionary then call get_Item() to get
// the actual IWriteCookie object.
//
result = Response->get_Cookies(&CookiesDict);
if(!SUCCEEDED(result))
{
return( mycookieval );
}
else
{
aspwrite( "Got it. Now getting the cookie by name (" + name + ")...<br>" );
//
// Request access to the cookie with the name of the input parameter.
// CookiesDict will return with the ptr to the IWriteCookie object.
//
result = CookiesDict->get_Item(vtCookieName, &vtCookieDict);
if(!SUCCEEDED(result))
{
CookiesDict->Release();
return( mycookieval );
}
else
{
//
// Get the cookie...
//
//ReadCookie = (IReadCookie*)(vtCookieDict.pdispVal);
vtCookieDict.pdispVal->QueryInterface( IID_IReadCookie, (void **)&ReadCookie );
ReadCookie->get_Count( &numkeys );
aspwrite( "Got that, too (" + AnsiString(numkeys) + " keys). Now getting the cookie by key (" + key + ")...<br>" );
result = ReadCookie->get_Item( vtCookieKey, &vtCookieVal );
//
// Cleanup...
//
ReadCookie->Release();
CookiesDict->Release();
if( !SUCCEEDED(result) )
return( mycookieval );
//
// Set the return value (as AnsiString)
// Note: used to fill AnsiString value, passed by pointer.
//
mycookieval = Variant(vtCookieVal);
//Response->Write(vtCookieVal);
aspwrite( "cookie: " + mycookieval + ".<br>" );
//*value = AnsiString(vtCookieVal);
//*value = mycookieval;
}
}
return( mycookieval );
}