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 ...
随机推荐
- UVa 11077 Find the Permutations(置换+递推)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35431 [思路] 置换+递推 将一个排列看作一个置换,分解为k个循 ...
- HDU 2089 不要62(挖个坑=-=)
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- [转载]Div和Table的区别
1:速度和加载方式方面的区别 div 和 table 的差异不是速度,而是加载方式,速度只能是指网络速度,如果速度足够快,是没有差异的: div 的加载方式是即读即加载,遇到 <div> ...
- Windbg扩展的一些参考文章
Windbg脚本和扩展工具开篇http://www.cnblogs.com/pugang/archive/2012/11/30/2796617.html WinDbg简单扩展DLL http://ww ...
- 关键字 final
package com.zyw.reusableClass; import java.util.Random; /** * Created by zyw on 2016/3/26. * from th ...
- [git] git 的基本认知
版本管理 ( Version Control ) 版本管理系统是一个记录文件变更的系统,让你在一段时间后可以恢复指定版本的文件.版本管理系统大致可分为三类:独立的本地版本管理系统.中心化版本管理系统. ...
- python 3 处理HTTP 请求的包
http http: https://docs.python.org/3/library/http.html http是一个包,里面含有多个模块:http.client,http.server,htt ...
- DevExpress LookUpEdit 下拉框基本操作
<span style="font-size:14px;"> ArrayList list = new ArrayList(); //遍历皮肤,放到列表中 foreac ...
- (三)phpcms之文件目录
刚刚接触phpcms,先从它的目录结构说起. 如下图所示,是phpcms的主目录结构: 其中api是接口目录,这个接口不是很明白.大概其是把别的内容加入进来,比如论坛啊什么的. caches是缓存文件 ...
- 免费WiFi,仅仅为好久没联系的你们
昨日,认识五年的朋友搬来与我一起住了,说不上来,没有激动,仅仅是突然感觉生活又多了一点生机.兴致上来,晚上立马联系了已经近四个月没有联系的好友,才知道他们的生活也因这几个月发生了翻天覆地的变化.究竟什 ...