gpt4 book ai didi

c++ - IHTMLDocument2::write() 的问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:58:35 24 4
gpt4 key购买 nike

我正在尝试从 html 缓冲区创建一个 mshtml 文档对象。但是,当执行以下代码时,它会调用 Internet Explorer 窗口。如何防止它调用 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();

最佳答案

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();

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

关于c++ - IHTMLDocument2::write() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/724746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com