I am trying to build a Firefox extension, that needs to call native C code.
My C program code is:
#include<windows.h>
int add(int a, int b)
{
return(a + b);
}
and my JavaScript code is :
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;
when calling the binaryFile
function, I get the error
Couldn't find function symbol in library
Please help me out. tHanks in advance.
Here is my repository where complete code is been available
If your addon is a restartless addon, make sure to set <em:unpack>true</em:unpack>
. The addon MUST be unpacked.
Awesome, you're getting deep into addons! See this repo: https://github.com/Noitidart/fx-sapi-test That shows the code to main.cpp which is compiled into a DLL and then imported and used.
You have to expose your add
function.
By the way, if you were doing a bootstrap addon: Also try doing the ctypes.open
inside the startup()
function. But you aren't, you're doing an Addon SDK addon, so you should be ok. But for your import do this:
lib = ctypes.open(self.data.url('Win32Project1.dll'));
That way you don't have to know the absolute path. Especially because \\
file seperator is only for Windows. Unix like systems (MacOSX, Linux, ...) use /
.
If you need more help join the moz jsctypes IRC channel :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With