本文介绍了如何获取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!

推荐答案


// 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的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 15:05