Question

I have created a BHO in VC++ refering below link

http://msdn.microsoft.com/en-us/library/bb250489%28v=vs.85%29.aspx

now i need to add input tag while page load completes, i tried with below code

void STDMETHODCALLTYPE CTestBHO::OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL)
{

    if (SUCCEEDED(hr))
    {
    responseCode=200;
    setResponseCode(pDisp);
    }
}

void CRealBrowserBHO::setResponseCode(IDispatch *pDisp)
{
HRESULT hr = S_OK;
CComPtr<IDispatch> spDispDoc;
hr = m_spWebBrowser->get_Document(&spDispDoc);
if (SUCCEEDED(hr))
    {
        BSTR inputElement=L"<input type=\"text\" name=\"ResponseCode\" value=\"200\">";
        IHTMLElement *pTRElmt = NULL;
        CComQIPtr<IHTMLDocument2> spHTMLDoc = spDispDoc;
        HRESULT hr1=spHTMLDoc->get_body(&pTRElmt);
        if (spHTMLDoc != NULL)
        {
            HRESULT hr=pTRElmt->insertAdjacentHTML(L"beforeEnd",inputElement);
        }
    }
}

But the tag didnt inserted on my web page.

What goes wrong, please point me what i missed here

Thanks, Ramachandran.

Was it helpful?

Solution

Code that should work:

void CRealBrowserBHO::setResponseCode(IDispatch *pDisp)
{
    HRESULT hr;
    CComPtr<IDispatch> spDispDoc;
    hr = m_spWebBrowser->get_Document(&spDispDoc);
    if (SUCCEEDED(hr) && spDispDoc)
    {
        CComPtr<IHTMLDocument2> spHTMLDoc;
        hr = spDispDoc.QueryInterface<IHTMLDocument2>( &spHTMLDoc );
        if (SUCCEEDED(hr) && spHTMLDoc) 
        {
            CComPtr<IHTMLElement> spBodyElmt;
            hr = spHTMLDoc->get_body(&spBodyElmt);
            if (SUCCEEDED(hr) && spBodyElmt)
            {
                CComBSTR inputElement( L"<input type='text' name='ResponseCode' value='200'/>" );
                hr = spBodyElmt->insertAdjacentHTML( CComBSTR( L"beforeEnd" ), inputElement);
            }
        }
    }
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top