4

I am trying to create an mshtml document object from an html buffer. But when the following code is executed it is invoking internet explorer window. How do I prevent it invoking IE.

#include <atlbase.h>
#include <mshtml.h>


CoInitialize(NULL);

CString strHTMLCode = _T("<html><head><script language=\"JavaScript\">{top.location.href=\"index.php\"}</script></head><body></body></html>");

CComPtr<IHTMLDocument2> pDoc;
HRESULT hr = CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, (void**)&pDoc);

SAFEARRAY* psa = SafeArrayCreateVector(VT_VARIANT, 0, 1);
VARIANT *param;

hr = SafeArrayAccessData(psa, (LPVOID*)¶m);
param->vt = VT_BSTR;
param->bstrVal = strHTMLCode.AllocSysString();

hr = pDoc->write(psa); //This line invoks internet explorer window.
hr = pDoc->close();
Shino C G
  • 1,237
  • 11
  • 17
  • What are you trying to accomplish? Have you noticed that If you remove the javascript, IE is not launched? – LLucasAlday May 01 '09 at 00:15
  • I was writing an application to parse web pages using IHTMLDocument2 interface. I observed that when I parse certain web pages which contins java script, it invoked the IE window from my application. Basically I want to parse web pages silently using IHTMLDocument2. – Shino C G May 04 '09 at 06:26

2 Answers2

2
OLECHAR szHTML[] = OLESTR("<HTML><BODY>Hello World!</BODY></HTML>");

IHTMLDocument2 *pDoc = NULL;
CoInitialize(NULL);
CoCreateInstance(CLSID_HTMLDocument,
               NULL,
               CLSCTX_INPROC_SERVER,
               IID_IHTMLDocument2,
               (LPVOID *)&pDoc);
if(pDoc)
{
    IPersistStreamInit *pPersist = NULL;
    pDoc->QueryInterface(IID_IPersistStreamInit, (LPVOID *) &pPersist);
    if(pPersist)
    {
        IMarkupServices *pMarkSvr = NULL;
        pPersist->InitNew();
        pPersist->Release();
        pDoc->QueryInterface(IID_IMarkupServices, (LPVOID *)&pMarkSvr);
        if(pMarkSvr)
        {
            IMarkupContainer *pMkContainer = NULL;
            IMarkupPointer *pMkStart = NULL;
            IMarkupPointer *pMkFinish = NULL;
            pMarkSvr->CreateMarkupPointer(&pMkStart);
            pMarkSvr->CreateMarkupPointer(&pMkFinish);
            pMarkSvr->ParseString(szHTML, 0, &pMkContainer, pMkStart, pMkFinish);
            if(pMkContainer)
            {
                IHTMLDocument2 *pNewDoc = NULL;
                pMkContainer->QueryInterface(IID_IHTMLDocument, (LPVOID *)&pNewDoc);
                if(pNewDoc)
                {
                    // do anything with pNewDoc, in this case
                    // get the body innerText.
                    IHTMLElement *pBody;
                    pNewDoc->get_body(&pBody);
                    if(pBody)
                    {
                        BSTR strText;
                        pBody->get_innerText(&strText);
                        ShowMessage(strText);
                        pBody->Release();
                        SysFreeString(strText);
                    }
                    pNewDoc->Release();
                }
                pMkContainer->Release();
            }
            if(pMkStart)
                pMkStart->Release();
            if(pMkFinish)
                pMkFinish->Release();
            pMarkSvr->Release();
        }
    }
    pDoc->Release();
}
CoUninitialize();

refer: http://www.codeguru.com/cpp/i-n/ieprogram/article.php/c4385/Lightweight-HTML-Parsing-Using-MSHTML.htm#more

Jichao
  • 40,341
  • 47
  • 125
  • 198
1

Please try the codes below.

CoInitialize(NULL);

CString strHTMLCode = "...";
CComPtr<IHTMLDocument2> pDoc;
HRESULT hr = CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, (void**)&pDoc);

// get persist stream init
ComQIPtr<IPersistStreamInit> psi = doc;

// allocate memory
HGLOBAL hMem = ::GlobalAlloc(GPTR, strHTMLCode.GetLength() * sizeof(TCHAR));
::GlobalLock(hMem);
::CopyMemory(hMem, (LPCTSTR)strHTMLCode, strHTMLCode.GetLength() *  * sizeof(TCHAR));

// create stream
IStream* stream = NULL;
HRESULT hr = ::CreateStreamOnHGlobal(hMem, FALSE, &stream);
if (SUCCEEDED(hr))
{
    // load html string
    psi->Load(stream);
    stream->Release();
}

// free memory
::GlobalUnlock(hMem);
::GlobalFree(hMem);
wenqiang
  • 954
  • 1
  • 5
  • 14
  • 1
    With IpersistStreamInit.Load etc., the DOM is not availableright away, you need to wait for readystate == complete (or the respective event), and this does not seem to occur without the calling thread processing windows messages. – peterchen Jul 28 '15 at 14:48