Create a simple js-ctypes example
js-ctypes 为Firefox extension访问由C/C++编写的native code提供了一种通用的方法。
从Firefox 4 开始支持js-ctypes,不同版本的之间有极好的兼容性。即使Firefox不断升级,也不需要修改native code,因此js-ctypes大受FF extension 开发者的青睐。
本文将会用一个简单的例子说明在Windows平台如何load dll 文件以及调用简单API。
Native Code:
这部分定义了一个求和的API, 将传入的两位参数相加,并返回结果。我们通过.def文件来export API,如下:
AddTest.cpp, to define logic of AddTest method.
int WINAPI AddTest(int a, int b)
{
return a + b;
}
AddTest.def, to export AddTest method.
LIBRARY "ctypes_simpleEx"
EXPORTS
AddTest
JS Code:
这部分的目的是通过运行extension,获取2+5的和。包括三部分:
1. 获取dll的绝对路径
2. 加载dll
3. 声明函数
4. 调用函数,求和
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/ctypes.jsm");
var cr = Components.classes['@mozilla.org/chrome/chrome-registry;1'].getService(Components.interfaces.nsIChromeRegistry);
// get path of lib
var chromeURI_myLib = Services.io.newURI("chrome://burningfish/content/resources/ctypes_simpleEx.dll", null, null);
var localFile_myLib = cr.convertChromeURL(chromeURI_myLib);
var jarPath_myLib = localFile_myLib.spec;
var filePath_myLib = localFile_myLib.path;
if(OATS.OpenScript.Util.StringUtil.startWith(filePath_myLib,"/")){
filePath_myLib=filePath_myLib.substring(1);
}
//load files
var myLib;
try{
myLib = ctypes.open(filePath_myLib);
}catch(ex){
console.log("Load natice file error:" + ex);
}
// declare the AddTest method
this.add = myLib.declare("AddTest", ctypes.winapi_abi,
ctypes.int, // return type
ctypes.int, // a
ctypes.int // b
);
// call AddTest, and get result of 2+5
var rez = this.add(2, 5); // rez is 7
alert("2+5 result is " + rez);
JS Code需要注意的两个地方:
1. ABI
Mozilla提供三种ABI(Application Binary Interface), ctypes.default_abi, ctypes.stdcall_abi, ctypes.winapi_abi.
三种ABI都适用Windows平台,具体用哪个ABI视native code 方法的定义方式而定。本例子native部分定义的是WINAPI方法,声明函数的时候需要用ctypes.winapi_abi。
如果是stdcall定义的函数,需要用ctypes.stdcall_abi。这两种之外的定义方式都用ctypes.default_abi。
2. 类型
Mozilla官方文档有对ctypes非常详细的描述,https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/js-ctypes_reference/ctypes。
声明函数的时候需要将native code的类型与ctypes类型严格对应起来。
例子中使用的参数类型和返回类型都是int, 使用ctypes.int 对应。
ctypes还提供了ctypes.int32_t, ctypes.int64_t, 在32位application 里面可以使用ctypes.int32_t, 64位application里面使用ctypes.int64_t。
上述描述了js-cypes简单调用dll method的例子,后续还会将指针、回调函数使用等的方法跟大家分享。
笔者是js-ctypes初学者,以上是学习中的一点经验和收获,分享给大家,若有不对之处,还请不惜赐教。
Create a simple js-ctypes example的更多相关文章
- Create a simple REST web service with Python--转载
今日尝试用python建立一个restful服务. 原文地址:http://www.dreamsyssoft.com/python-scripting-tutorial/create-simple-r ...
- simple js
simple js 题目描述:小宁发现了一个网页,但却一直输不对密码.(Flag格式为 Cyberpeace{xxxxxxxxx} ) 打开题目后,有一个Enter password框,要求输入密码, ...
- [Tools] Create a Simple CLI Tool in Node.js with CAC
Command-line tools can help you with all sorts of tasks. This lesson covers the very basics of setti ...
- [Angular 2] Create a simple search Pipe
This lesson shows you how to create a component and pass its properties as it updates into a Pipe to ...
- CHtmlEditCtrl(1) : Use CHtmlEditCtrl to Create a Simple HTML Editor
I needed a lightweight HTML editor to generate "rich text" emails, so I decided to explore ...
- create a simple COM object
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAArsAAAGYCAIAAADN0b3QAAAgAElEQVR4nO29749c1b2nW/4Lzh8wUr
- Artix-7 50T FPGA试用笔记之Create a simple MicroBlaze System
前言:之前笔者的试用博文提到安富利这块板子非常适合MicroBlaze开发,同时网上关于MicroBlaze的资料非常少(或含糊不清),没有一篇能完整介绍VIVADO SDK的设计流程,所以笔者带来这 ...
- [Angular] Create a simple *ngFor
In this post, we are going to create our own structure directive *ngFor. What it should looks like i ...
- [Angular] Using directive to create a simple Credit card validator
We will use 'HostListener' and 'HostBinding' to accomplish the task. The HTML: <label> Credit ...
随机推荐
- asterisk错误排查
1.查看某个模块是否被包含在编译选项里了: See menuselect.makeoptsIf you see chan_sip in the MENUSELECT_CHANNELS option, ...
- Django中的Form(二)
一.保存用户输入内容 如果用户输入一张表单提交后出现错误时,会出现重现填写的情况.我们可以把用户输入的信息保存下来,并返回到前台页面,这样用户就无需再次输入. views.py # coding:ut ...
- 广州Uber优步司机奖励政策(1月18日~1月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- poj 1321 棋盘问题【dfs】
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28308 Accepted: 13996 Descriptio ...
- 新站上线啦,Html5Think,H5优秀资源的收集、学习、分享和交流
最近闲来做了个H5资源站,刚刚有点资源,可以访问交流下. 栏目: H5网站模板 H5动画特效 H5资源工具 H5学习资料 致力于H5的学习,通过各个H5优秀案例的学习,逐步完善自己的H5体系,有朝一日 ...
- UML应用:业务内涵的分析抽象&表达
上一篇,架构设计的UML图形思考 ,简介了图形思考设计.表达设计对于架构师的重要意义,以及简介了使用统一建模语言UML描写叙述类以及类之间的继承关系,这样的描写叙述还停留在写代码,表达的但是说是怎样写 ...
- 粗谈pcap_next_ex()
pcap_next_ex(pcap_t* p,struct pcap_pkthdr** pkt_header,const u_char** pkt_data) 功能: 从interface或离线记 ...
- 使用Partitioner实现输出到多个文件
1.需求 按学生的年龄段,将数据输出到不同的文件.这里我们分为三个年龄段:小于等于20岁.大于20岁小于等于50岁和大于50岁 2.实现 1.编写Partitioner,代码如下 public sta ...
- Elasticsearch .Net Client NEST 索引DataSet数据
NEST 索引DataSet数据,先序列化然后转成dynamic 类型进行索引: /// <summary> /// 索引dataset /// </summary> /// ...
- Eclipse 每行 80 字符限制的提示线
有时候希望eclipse和C++编辑器之类有条对齐线 打开 Eclipse, Windows -> Prefereces -> General -> Editors -> Te ...