Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 771
Shankar Us Avatar asked Feb 09 '15 13:02

Shankar Us


2 Answers

Here is my repository where complete code is been available

like image 200
Shankar Us Avatar answered Nov 18 '22 12:11

Shankar Us


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 :)

  • https://client00.chat.mibbit.com/?url=irc%3A%2F%2Firc.mozilla.org%2F%23jsctypes
  • irc://moznet/jsctypes
like image 43
Noitidart Avatar answered Nov 18 '22 14:11

Noitidart