I am working with a webbrowser host on c++, I managed to sink event and I am running this void on DISPID_DOCUMENTCOMPLETE:
void DocumentComplete(LPDISPATCH pDisp, VARIANT *url)
{
READYSTATE rState;
iBrowser->get_ReadyState(&rState);
if(rState == READYSTATE_COMPLETE)
{
HRESULT hr;
IDispatch *pHtmlDoc = NULL;
IHTMLDocument2 *pDocument = NULL;
IHTMLElement *pBody = NULL;
IHTMLElement *lpParentElm = NULL;
BSTR bstrHTMLText;
hr = iBrowser->get_Document(&pHtmlDoc);
hr = pHtmlDoc->QueryInterface(IID_IHTMLDocument2, (void**)&pDocument);
if( (FAILED(hr)) || !pDocument)
{
MessageBox(NULL, "QueryInterface failed", "WebBrowser", MB_OK);
}
hr = pDocument->get_body( &pBody );
if( (!SUCCEEDED(hr)) || !pBody)
{
MessageBox(NULL, "get_body failed", "WebBrowser", MB_OK);
}
pBody->get_parentElement(&lpParentElm);
lpParentElm->get_outerHTML(&bstrHTMLText);
_bstr_t bstr_t(bstrHTMLText);
std::string sTemp(bstr_t);
MessageBox(NULL, sTemp.c_str(), "WebBrowser", MB_OK);
}
}
I don’t much about c++, I built this code from watching other codes in google. Now I know I have to use ->Release, but do I have to use all these?:
pHtmlDoc->Release();
pDocument->Release();
pBody->Release();
lpParentElm->Release();
iBrowser->Release();
Because on the examples I used to build my code it was using it only for the IHTMLElement(s).
Yes, you do have to call
Release()on those pointers, otherwise objects will leak. The same goes forBSTRs.You’ll be much better off if you use smart pointers for that –
ATL::CComPtr/ATL::CComBSTRor_com_ptr_t/_bstr_t.