如何使用js-ctypes Firefox扩展调用本机C代码?

Sha*_* Us 9 javascript c firefox-addon jsctypes

我正在尝试构建一个需要调用本机C代码的Firefox扩展.

我的C程序代码是:

#include<windows.h>
int add(int a, int b)
{
    return(a + b);
}
Run Code Online (Sandbox Code Playgroud)

我的JavaScript代码是:

var {Cu} = require('chrome');
var self  = require('sdk/self');
Cu.import("resource://gre/modules/ctypes.jsm");
var lib;
var puts;
lib = ctypes.open('G:\\Shankar\\Project\\Maidsafe\\Firefox\\addon-sdk-1.17\\jsctype_sample\\data\\Win32Project1.dll');

try {
    puts = lib.declare("add", /* function name */
        ctypes.default_abi, /* call ABI */
        ctypes.int32_t, /* return type */
        ctypes.int32_t, /* argument type */
        ctypes.int32_t /* argument type */
    );
} catch (e) {
    console.log('Érror'+ e);
}

function binaryFile() {        
    var ret = puts(1, 2);
    dump(ret);
    lib.close();
};

exports.binaryFile = binaryFile;
Run Code Online (Sandbox Code Playgroud)

在调用binaryFile函数时,我收到错误

Couldn't find function symbol in library
Run Code Online (Sandbox Code Playgroud)

请帮帮我.提前致谢.

Sha*_* Us 6

这是我的存储库,其中提供了完整的代码


Noi*_*art 5

如果你的插件是一个无重启的插件,请务必设置<em:unpack>true</em:unpack>.必须解压缩插件.

太棒了,你已经深入了解插件!请参阅此repo:https://github.com/Noitidart/fx-sapi-test它显示main.cpp的代码,该代码被编译为DLL然后导入和使用.

你必须公开你的add功能.

顺便说一句,如果你正在做一个bootstrap插件:也尝试ctypes.openstartup()函数内部做.但你不是,你正在做一个Addon SDK插件,所以你应该没问题.但是对于你的导入这样做:

lib = ctypes.open(self.data.url('Win32Project1.dll'));
Run Code Online (Sandbox Code Playgroud)

这样你就不必知道绝对路径了.特别是因为\\文件分隔符仅适用于Windows.类Unix系统(MacOSX,Linux,...)使用/.

如果您需要更多帮助,请加入moz jsctypes IRC频道:)

  • 我自己用var dataUrl = self.data.url("udp_client.dll")来解决它; dataUrl = Services.io.newURI(dataUrl,null,null).QueryInterface(Ci.nsIFileURL).file.path;.谢谢 (3认同)