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的更多相关文章

  1. Create a simple REST web service with Python--转载

    今日尝试用python建立一个restful服务. 原文地址:http://www.dreamsyssoft.com/python-scripting-tutorial/create-simple-r ...

  2. simple js

    simple js 题目描述:小宁发现了一个网页,但却一直输不对密码.(Flag格式为 Cyberpeace{xxxxxxxxx} ) 打开题目后,有一个Enter password框,要求输入密码, ...

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. create a simple COM object

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAArsAAAGYCAIAAADN0b3QAAAgAElEQVR4nO29749c1b2nW/4Lzh8wUr

  7. Artix-7 50T FPGA试用笔记之Create a simple MicroBlaze System

    前言:之前笔者的试用博文提到安富利这块板子非常适合MicroBlaze开发,同时网上关于MicroBlaze的资料非常少(或含糊不清),没有一篇能完整介绍VIVADO SDK的设计流程,所以笔者带来这 ...

  8. [Angular] Create a simple *ngFor

    In this post, we are going to create our own structure directive *ngFor. What it should looks like i ...

  9. [Angular] Using directive to create a simple Credit card validator

    We will use 'HostListener' and 'HostBinding' to accomplish the task. The HTML: <label> Credit ...

随机推荐

  1. Windows Server 2008 网站访问PHP响应慢的解决方法

    公司新上了一个网站,但是在配置完PHP环境之后却发现了问题,访问HTML速度飞快,而访问PHP网页时就要卡顿1秒,响应很慢的样子,排除了带宽的因素之后,在百度上搜了一圈竟然解决了,现在将方法转载给大家 ...

  2. http 协议的过程

    当你输入某个网址的时候发生了什么? 首先:你该知道 a.http协议是应用层协议,他是浏览器像服务器请求网页,服务器返回网页的过程,他是基于tcp协议的. 1.假设随便输入输入域名 http://ww ...

  3. [svn] 数据库操作残留,无法进行操作的解决方法

    WINDOWS环境下的解决方法: 1: 下载sqlite3数据库工具,放置于SVN的同级目录 2: CMD路径转移到Sqlite3目录 3: 残留操作选择: sqlite3 .svn/wc.db &q ...

  4. OpenStack Havana 部署在Ubuntu 12.04 Server 【OVS+GRE】(三)——计算节点的安装

    序:OpenStack Havana 部署在Ubuntu 12.04 Server [OVS+GRE] 计算节点: 1.准备结点 安装好ubuntu 12.04 Server 64bits后,进入ro ...

  5. Ural 1046 Geometrical Dreams(解方程+计算几何)

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1046 参考博客:http://hi.baidu.com/cloudygoose/item ...

  6. 真机测试---iOS证书(.p12)和描述文件(.mobileprovision)

    iOS证书和描述文件: 证书类型 使用场景 开发(Development)证书和描述文件 用于开发测试,在starain中打包后可在真机环境通过Safari调试 发布(Distribution)证书和 ...

  7. list,set,map,数组间的相互转换

    1.list转set Set set =  new  HashSet( new  ArrayList()); 2.set转list List list =  new  ArrayList( new   ...

  8. GWT中实现跳转及不同entrypoint怎么互相访问

    怎么跳转? 跳转这个概念这里指的是从一个web页面跳转到另一个web页面,如果我们使用gwt来开发web,很自然的我们会想到怎么从一个gwt做的页面跳转到另一个gwt做的页面. 但从网上的gwt例子来 ...

  9. IAP内购 返回的产品数量为0

    上个月搞IAP,提交到appstore审核被拒,根据附件截图 可以知道是请求产品信息的时候,产品数量返回0了. 返回产品数量为0 要么是Itunes Connect 里面的Contracts Tax ...

  10. VirtualBox 运行失败

    运行 VirtualBox --help 安装 VirtualBox 后 运行 报错内核没加载问题 需要设置环境变量 内核加载的环境变量 export KERN_DIR=/usr/src/kernel ...