IHTMLDocument2::close() hangs

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I select some of the elements (IHTMLElement), get its outer_html and
write it to a IHTMLDocument2. Here is the sample code.


void WriteBSTR2IHTMLDoc2(BSTR htmlBstr, CComPtr<IHTMLDocument2>
pHtmlDoc)
{
VARIANT *param;

SAFEARRAY *sfArray;

sfArray = SafeArrayCreateVector(VT_VARIANT, 0, 1);

if (sfArray == NULL) {

goto cleanup;

}

SafeArrayAccessData(sfArray,(LPVOID*) & param);

param->vt = VT_BSTR;

param->bstrVal = htmlBstr;

SafeArrayUnaccessData(sfArray);

pHtmlDoc->write(sfArray);


cleanup:

if (sfArray != NULL) {

SafeArrayDestroy(sfArray);

}
//SysFreeString(htmlBstr);
}

HRESULT GetSelectedDocument(CComPtr<IHTMLDocument2> &pDoc)
{
CSelectedElement selElement;
CComPtr<IHTMLDocument2> doc2;
CComPtr<IHTMLDocument3> doc3;
CComPtr<IHTMLElementCollection> coll;
CComPtr<IDispatch> iDisp;
VARIANT vIdx;
CComPtr<IHTMLElement> spDocElement, spBodyElement, spElement;
long nLength;
HRESULT result;
vIdx.vt = VT_UINT;
result = doc2.CoCreateInstance( CLSID_HTMLDocument );
CComVariant dummy;
CComPtr< IDispatch > doc;
BSTR bstr, tagName;
doc2->open ( CComBSTR( L"text/html" )
, CComVariant( CComBSTR( L"replace" ) )
, dummy
, dummy
, &doc
);

if ( SUCCEEDED( result ) && doc2 )
{



bstr = SysAllocString(L"<html>");
WriteBSTR2IHTMLDoc2(bstr, doc2);
//SysFreeString(bstr);
m_doc2->QueryInterface(IID_IHTMLDocument3, (void**)&doc3);
doc3->get_documentElement(&spDocElement);


if(SUCCEEDED(spDocElement->get_children(&iDisp)) &&
SUCCEEDED(iDisp->QueryInterface(IID_IHTMLElementCollection,
(LPVOID*)&coll)) && coll != NULL)
{
iDisp.Release();

if(SUCCEEDED(result = coll->get_length(&nLength)))
{

/* GET HEAD TAG AND PRINT OUTER HTML */
for(int i=0;i<nLength;i++)
{
vIdx.intVal = i;
if(SUCCEEDED(result = coll->item(vIdx, vIdx, &iDisp)) && iDisp !=
NULL && SUCCEEDED(
iDisp->QueryInterface(IID_IHTMLElement, (LPVOID*)&spElement))
&&
spElement != NULL)
{
spElement->get_tagName(&tagName);
if(_tcsicmp(tagName, L"HEAD") == 0)
{
spElement->get_outerHTML(&bstr);
WriteBSTR2IHTMLDoc2(bstr, doc2);
SysFreeString(tagName);
iDisp.Release();
spElement.Release();
break;
}
SysFreeString(tagName);
}
iDisp.Release();
spElement.Release();
}


/* GET BODY TAG AND PASS IT TO THE FUNC */
spBodyElement = NULL;
for(int i=0;i<nLength;i++)
{
vIdx.intVal = i;
if(SUCCEEDED(result = coll->item(vIdx, vIdx, &iDisp)) && iDisp !=
NULL && SUCCEEDED(
iDisp->QueryInterface(IID_IHTMLElement, (LPVOID*)&spElement))
&&
spElement != NULL)
{
spElement->get_tagName(&tagName);
if(_tcsicmp(tagName, L"BODY") == 0)
{
spBodyElement = spElement;
SysFreeString(tagName);
iDisp.Release();
spElement.Release();
break;
}
SysFreeString(tagName);
}
iDisp.Release();
spElement.Release();
}

/* BODY ELEMENT FOUND */
if(spBodyElement != NULL)
{
std::string sBodyHtml;
listSelectedElements.sort();
sBodyHtml = GetSelectedElements(spBodyElement, doc2);
bstr = A2BSTR(sBodyHtml.c_str());
WriteBSTR2IHTMLDoc2(bstr, doc2);
}

}
}

bstr = SysAllocString(L"</html>");
WriteBSTR2IHTMLDoc2(bstr, doc2);
result = doc2->close();
pDoc = doc2;
htmlDump(doc2);
}
else
pDoc = NULL;

return result;

}


The problem is with doc2->close(); The whole browser window just hangs
at this point on some the sites like gmail.com and works perfectly
fine with the other. The close operation takes forever to complete and
the CPU utilization of iexplore becomes atleast 50%. Whats wrong with
gmail.com or I have missed something?

.