如何获取IHTMLWindow2 :: execScript的返回值? [英] How do I get the return value of IHTMLWindow2::execScript?

查看:297
本文介绍了如何获取IHTMLWindow2 :: execScript的返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 IHTMLWindow2 :: execScript 从C ++中获取Javascript函数的返回值,但是,它似乎永远不会起作用。以下是我正在尝试执行的示例。



C ++代码:

I've been trying to get the return value of a Javascript function from within C++ using IHTMLWindow2::execScript however, it never seems to work. The following is an example of what I am trying to perform.

C++ Code:

CComPtr<IHTMLWindow2> win;
doc2->get_parentWindow( &win);
CComVariant empty;

CComBSTR fullScript = L"function testFunction(){";
fullScript += L"return 2254;";
fullScript += L"}";
fullScript += L"testFunction();";

win->execScript(fullScript.m_str, L"JAVASCRIPT", &empty);





CComVariant空始终为空。所有示例都使用 INVOKE 来调用已嵌入HTML页面的javascript函数。在我的情况下,我需要动态获取未嵌入HTML页面的Javascript函数的返回值。



任何想法?在此先感谢!



The CComVariant empty is always empty. All the examples out there use INVOKE to call a javascript function which is already embedded within the HTML page. In my case, I need to get the return value of a Javascript function on the fly that isn't embedded within my HTML page.

Any ideas? Thanks in advance!

推荐答案

文章 JavaScript-call-from-C 介绍如何使用C ++中的javscript。但微软自IE 11开始说:从IE11开始,使用eval......
The article JavaScript-call-from-C enlights how to work with javscript from C++. But Microsoft says since IE 11: "Starting with IE11, use eval"...


在搜索了几个小时之后,我设法让它按需运行。以下是如何使其工作的片段(在 DOCUMENTCOMPLETE 中运行):



After searching around for a couple of hours, I managed to make it run as required. The following is a snippet for how to make it work (runs within DOCUMENTCOMPLETE):

// Get IHTMLWindow
CComPtr<ihtmlwindow2> win;
doc2->get_parentWindow( &win);

if (win == NULL) return E_FAIL;
			
// Get IHTMLWindow dispatch
CComDispatchDriver dispWindow;
win->QueryInterface(&dispWindow);
			
if (dispWindow == NULL) return E_FAIL;
			
// Define JAVAScript
CComBSTR fullScript = L"function testFunction(){";
fullScript += L"alert('JS function called!')";
fullScript += L"return  true;";
fullScript += L"}testFunction();";
CComVariant script(fullScript.m_str);
CComVariant result;
			
// Execute JavaSctript
dispWindow.Invoke1(L"eval", &script, &result);
			
result.ChangeType(VT_BSTR);
			
// Print Result
LOG(L"%s", result.bstrVal);
</ihtmlwindow2>


这篇关于如何获取IHTMLWindow2 :: execScript的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆