convert _variant_t to wstring

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
hi,all, how to convert a _variant_t to a wstring?
I use IHTMLElement getAttribute function to retrieve the value of some attribute, it seems that the doc didnt specify clearly that what a html element doesnt have that attribute, what will be returned? a null string? what if I convert the _variant_t to wstring,
the wstring will be like ""- a empty string?
http://msdn.microsoft.com/en-us/library/aa752280(v=vs.85).aspx
this is my code:
<pre class="prettyprint wstring MyProject::GetNodeImgSrc(VARIANT varSrc)
{
long lLength = 0;
IDispatch *ppvdisp = NULL;
IHTMLDocument2 *htmDoc = NULL;
IHTMLElementCollection* pElemColl = NULL;
IHTMLElement* pChElem = NULL;
wstring wstrImgSrcVal;

hash_map<wstring, wstring> hmStyles;

SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, (void**) &htmDoc);

VARIANT *param = NULL;
HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
param->vt = VT_BSTR;
param->bstrVal = SysAllocString(varSrc.bstrVal); //varSrc.bstrVal;

hr = SafeArrayUnaccessData(psaStrings);
hr = htmDoc->write(psaStrings);

hr = htmDoc->get_all(&pElemColl);
hr = pElemColl->get_length(&lLength);
for(long i = 0; i < lLength; i++)
{
_variant_t name(i);
_variant_t index(i);

hr = pElemColl->item(name, index, &ppvdisp);
if(ppvdisp && SUCCEEDED(hr))
{
hr = ppvdisp->QueryInterface(IID_IHTMLElement, (void **)&pChElem);

if(pChElem && SUCCEEDED(hr))
{
BSTR bstrTagName = NULL;

hr = pChElem->get_tagName(&bstrTagName);

if(SUCCEEDED(hr) && wstring(bstrTagName).compare(L"IMG") == 0)
{
_variant_t varValue;
long lFlags = 0;
BSTR bstrAttributeName = SysAllocString(_T("src"));

hr = pChElem->getAttribute(bstrAttributeName, lFlags, &varValue);
if(SUCCEEDED(hr))
{
// convert _variant_t to wstring
}

SysFreeString(bstrAttributeName);
}

if(bstrTagName)
SysFreeString(bstrTagName);

pChElem->Release();
} // if pChElem && SUCCEEDED(hr)

ppvdisp->Release();
}//if ppvdisp && SUCCEEDED(hr)
}//for

htmDoc->Release();
pElemColl->Release();

SafeArrayDestroy(psaStrings);

return wstrImgSrcVal;
}[/code]
<br/>

cheers
<br/>

View the full article
 
Back
Top