JavaScript Calls from C++
来源:互联网 发布:金山软件股票 编辑:程序博客网 时间:2024/06/16 10:29
最近要用到相关技术,先贴在这,有空再翻页。
本文转自:
http://www.codeguru.com/Cpp/I-N/ieprogram/article.php/c4399
http://www.codeguru.com/cpp/i-n/ieprogram/article.php/c4399/JavaScript-Calls-from-C.htm
http://www.codeproject.com/KB/COM/jscalls.aspx
Introduction
Sometimes, when we are using the IE Browser Control inside of a C++ application, we need to access the HTML elements. We can do it by using standard COM objects such as IWebBrowser2
, IHTMLDocument2
, and so forth. By doing this, we easily can implement features such as click button, click anchor, get input string, get HTML text, and so on. Unfortunately, Microsoft did not provide similar objects for JavaScript. In any case, it is possible to make a control for the JavaScript object inside an HTML page by using a traditional COM approach. This article describes the class CWebPage
that allows you to do it and a technique to call a JavaScript function from C++ code.
How to Do This
As the result of using the presented class, it will be easy to call any JavaScript function from C++ code. To implement this feature, we should get a pointer to the IHTMLDocument2
interface. If we are using the CHtmlView
class from MFC, we can get one by using member function CHtmlView::GetHtmlDocument()
. In the case of using the IWebBrowser
or IWebBrowser2
components, the function get_Document
will bring us the desired interface. Here is an example:
The rest of the things will be done by the CWebPage
class. Here is an example of a JavaScript call without parameters.
m_webPage.CallJScript("Welcome");
The example of the JavaScript call with two parameters will look like this:
m_webPage.CallJScript("Miltiply","2.34","3.32");
The Class Implementation
Calling Technique
The previously mentioned technique splits the following steps:
- Getting a pointer to the
IHTMLDocument2
interface. - Getting
IDispatch
for a JavaScript object in an HTML document. - Getting
DISPID
for a given name of a JavaScript function. - Putting parameters to the
DISPPARAM
structure. - Calling a JavaScript function by using the Invoke method of the
IDispatch
interface.
Here is an example of getting a IDispatch
pointer to the Java Scripts objects:
And here is the final function to call JavaScript:
Notes About the Demo
To call a JavaScript function from the demo, you should select the function in the tree of the left window. After this, press the ! button on the menu bar.
Downloads
Download demo project - 34 Kb
Download source - 3 Kb
- JavaScript Calls from C++
- JavaScript Calls from C++
- Some examples of BLAS and LAPACK calls from C++.
- Writing Remote Procedural Calls (RPCs) in C
- Comparing system calls and C libraries
- Data type conversions for API calls from Visual Basic
- 猴年马月去了(remove server calls from ws2_32)
- Who Stole gettimeofday() System Calls From Oracle strace() sessions?
- JavaScript: Use a Web Proxy for Cross-Domain XMLHttpRequest Calls
- Debugging PL-SQL calls from Java Session Using Eclipse and SQL Developer
- javaScript与from表单
- Calling Wicket from Javascript,
- JavaScript Stack from Scratch
- recursive calls
- linux calls
- recursive calls
- Send a BOOL value from JavaScript to Objective-C(UIWebview interact with app)
- JavaScript Web Service Calls教程,及运行时出现版本不匹配错误
- div+css浏览器兼容问题解决方法
- 蒙特卡罗算法计算圆周率PI(C++)
- 关于C/C++函数声明中参数个数的疑问
- js 选中变色
- 超链接-盒子模型
- JavaScript Calls from C++
- 突发起来的感受——自由
- 练习-类的创建与调用
- 用正则表达式删除重复字符
- 在opensolaris下设置DNS和DHCP
- 深入研究Servlet线程安全性问题
- 软件开发这点事儿--软件开发工具手册》--软件程序员专业技能
- JAVA常用操作语句----项目中的总结一
- 思过