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

How to call native C code, using the js-ctypes Firefox extension?

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

我的C程序代码是:

#include<windows.h>
int add(int a, int b)
{
    return(a + b);
}

我的 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;

调用binaryFile函数时,出现错误

Couldn't find function symbol in library

请帮帮我。 提前致谢。

如果您的插件是无需重启的插件,请务必设置 <em:unpack>true</em:unpack>。插件必须解压。

太棒了,您正在深入了解插件!请参阅此 repo:https://github.com/Noitidart/fx-sapi-test 这显示了 main.cpp 的代码,它被编译成一个 DLL,然后导入并使用。

您必须公开您的 add 功能。

顺便说一句,如果你正在做一个 bootstrap 插件:也可以尝试在 startup() 函数中做 ctypes.open。但你不是,你正在做一个 Addon SDK 插件,所以你应该没问题。但是对于您的导入,请执行以下操作:

lib = ctypes.open(self.data.url('Win32Project1.dll'));

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

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

这是我的 repository 完整代码可用