如题调用javascript接口document.write( )函数然后引起了Windows安全警告
函数代码如下
VOID ParseHtml(WCHAR *lpHtml)
{
BOOL bInitEx = FALSE;
IHTMLDocument2 *lpDocument2 = NULL;
SAFEARRAY *lpSafeArray = NULL;
VARIANT *lpVariant = NULL;
BSTR bstrHtml = NULL;
IHTMLElementCollection *lpCollection = NULL;
BSTR bstrOutPut = NULL;
BSTR designMode = NULL;
IHTMLElement *lpElement = NULL;
IDispatch *lpDispatch = NULL;
BSTR bstrScriptCode = NULL;
BSTR bstrScriptlanguage = NULL;
IHTMLWindow2 *lpWindow2 = NULL;
if (NULL == lpHtml)
{
goto exit;
}
// 初始化COM组件
if (FALSE == SUCCEEDED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
{
// 该线程成功初始化COM组件, 添加初始化状态控制用于关闭COM组件而不会导致影响其他函数调用COM组件
bInitEx = TRUE;
}
// 创建COM组件对象
if (FALSE == SUCCEEDED(CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, (VOID**)&lpDocument2)))
{
goto exit;
}
//bstrScriptCode = SysAllocString(L"function confirm(str){return true;}");
//bstrScriptlanguage = SysAllocString(L"javascript");
//lpDocument2->get_parentWindow(&lpWindow2);
//if (FALSE == SUCCEEDED(lpWindow2->execScript(bstrScriptCode, bstrScriptlanguage, NULL)))
//{
// goto exit;
//}
//designMode = SysAllocString(L"On");
//if (FALSE == SUCCEEDED(lpDocument2->put_designMode(designMode)))
//{
// goto exit;
//}
// 创建SafeArrayCreateVector数组 参数为类型,最低维度,数组中元素个数
lpSafeArray = SafeArrayCreateVector(VT_VARIANT, 0, 1);
if (NULL == lpSafeArray)
{
goto exit;
}
// 用SafeArrayAccessData创建能够直接读写SafeArray缓冲区
if (FALSE == SUCCEEDED(SafeArrayAccessData(lpSafeArray, (VOID**)&lpVariant)))
{
goto exit;
}
// 设置VARIANT类型为BSTR
bstrHtml = SysAllocString(lpHtml);
if (NULL == bstrHtml)
{
goto exit;
}
lpVariant->vt = VT_BSTR;
lpVariant->bstrVal = bstrHtml;
//将得到的网页内容写到文档流中
if (FALSE == SUCCEEDED(lpDocument2->write(lpSafeArray)))
{
goto exit;
}
if (FALSE == SUCCEEDED(lpDocument2->close()))
{
goto exit;
}
lpDocument2->get_title(&bstrOutPut);
lpDocument2->get_body(&lpElement);
lpElement->get_innerText(&bstrOutPut);
//bstrOutPut = SysAllocString(L"12345678");
WriteFileExtension(L"D:\\Projects\\PARSEHTML\\Debug\\test.txt", bstrOutPut, -1, FALSE);
//lpElement->get_all(&lpDispatch);
//lpDispatch->QueryInterface(IID_IHTMLElement, (void**)&lpElement);
//lpElement->get_innerText(&bstrOutPut);
//lpElement->QueryInterface(IID_IHTMLElementCollection, (void**)&lpCollection);
//lpCollection->toString(&bstrOutPut);
//lpElement->toString(&bstrOutPut);
//WriteFileExtension(L"D:\\Projects\\PARSEHTML\\Debug\\test.txt", bstrOutPut, -1);
//lpDocument2->get_all(&lpCollection);
//if (NULL == lpCollection)
//{
// goto exit;
//}
exit:
if (DISP_E_ARRAYISLOCKED == SafeArrayDestroy(lpSafeArray))
{
// goto exit;
}
if (NULL == bstrHtml)
{
SysFreeString(bstrHtml);
}
if (FALSE != bInitEx)
{
CoUninitialize();
}
}
函数代码如下
VOID ParseHtml(WCHAR *lpHtml)
{
BOOL bInitEx = FALSE;
IHTMLDocument2 *lpDocument2 = NULL;
SAFEARRAY *lpSafeArray = NULL;
VARIANT *lpVariant = NULL;
BSTR bstrHtml = NULL;
IHTMLElementCollection *lpCollection = NULL;
BSTR bstrOutPut = NULL;
BSTR designMode = NULL;
IHTMLElement *lpElement = NULL;
IDispatch *lpDispatch = NULL;
BSTR bstrScriptCode = NULL;
BSTR bstrScriptlanguage = NULL;
IHTMLWindow2 *lpWindow2 = NULL;
if (NULL == lpHtml)
{
goto exit;
}
// 初始化COM组件
if (FALSE == SUCCEEDED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
{
// 该线程成功初始化COM组件, 添加初始化状态控制用于关闭COM组件而不会导致影响其他函数调用COM组件
bInitEx = TRUE;
}
// 创建COM组件对象
if (FALSE == SUCCEEDED(CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, (VOID**)&lpDocument2)))
{
goto exit;
}
//bstrScriptCode = SysAllocString(L"function confirm(str){return true;}");
//bstrScriptlanguage = SysAllocString(L"javascript");
//lpDocument2->get_parentWindow(&lpWindow2);
//if (FALSE == SUCCEEDED(lpWindow2->execScript(bstrScriptCode, bstrScriptlanguage, NULL)))
//{
// goto exit;
//}
//designMode = SysAllocString(L"On");
//if (FALSE == SUCCEEDED(lpDocument2->put_designMode(designMode)))
//{
// goto exit;
//}
// 创建SafeArrayCreateVector数组 参数为类型,最低维度,数组中元素个数
lpSafeArray = SafeArrayCreateVector(VT_VARIANT, 0, 1);
if (NULL == lpSafeArray)
{
goto exit;
}
// 用SafeArrayAccessData创建能够直接读写SafeArray缓冲区
if (FALSE == SUCCEEDED(SafeArrayAccessData(lpSafeArray, (VOID**)&lpVariant)))
{
goto exit;
}
// 设置VARIANT类型为BSTR
bstrHtml = SysAllocString(lpHtml);
if (NULL == bstrHtml)
{
goto exit;
}
lpVariant->vt = VT_BSTR;
lpVariant->bstrVal = bstrHtml;
//将得到的网页内容写到文档流中
if (FALSE == SUCCEEDED(lpDocument2->write(lpSafeArray)))
{
goto exit;
}
if (FALSE == SUCCEEDED(lpDocument2->close()))
{
goto exit;
}
lpDocument2->get_title(&bstrOutPut);
lpDocument2->get_body(&lpElement);
lpElement->get_innerText(&bstrOutPut);
//bstrOutPut = SysAllocString(L"12345678");
WriteFileExtension(L"D:\\Projects\\PARSEHTML\\Debug\\test.txt", bstrOutPut, -1, FALSE);
//lpElement->get_all(&lpDispatch);
//lpDispatch->QueryInterface(IID_IHTMLElement, (void**)&lpElement);
//lpElement->get_innerText(&bstrOutPut);
//lpElement->QueryInterface(IID_IHTMLElementCollection, (void**)&lpCollection);
//lpCollection->toString(&bstrOutPut);
//lpElement->toString(&bstrOutPut);
//WriteFileExtension(L"D:\\Projects\\PARSEHTML\\Debug\\test.txt", bstrOutPut, -1);
//lpDocument2->get_all(&lpCollection);
//if (NULL == lpCollection)
//{
// goto exit;
//}
exit:
if (DISP_E_ARRAYISLOCKED == SafeArrayDestroy(lpSafeArray))
{
// goto exit;
}
if (NULL == bstrHtml)
{
SysFreeString(bstrHtml);
}
if (FALSE != bInitEx)
{
CoUninitialize();
}
}