Js-ctypes third part dll with string return

265 views Asked by At

I am using a DLL with js-ctypes that is made in C.

The method return a string, but when I try access the content of the pointer firefox crashes!

The following code works:

Function declaration:

var getStr = lib.declare("getString", 
            ctypes.default_abi,
            ctypes.char.ptr,
            ctypes.int32_t
            );

Function call:

let number = new ctypes.int32_t(1);
var str = getStr(number);
console.log(str.toString());
str.readString();

The console.log output's:

ctypes.char.ptr(ctypes.UInt64("0x64ff5b48"))

But this code don't work:

Function declaration:

var Core = {
    init : function(){
        this.lib = ctypes.open("library");
        this.getStr = this.lib.declare("getString",
                                       ctypes.default_abi,
                                       ctypes.char.ptr,
                                       ctypes.int32_t);
    },

    close : function(){
        this.lib.close();
    }

}

Function call

Core.init();
var number = new ctypes.int32_t(1);
var result = Core.getStr(number);
console.log(result.toString());
result.readString();

The console.log output's:

ctypes.char.ptr(ctypes.UInt64("0x64ff5b48"))

The same thing !

With this way firefox crashes. Anyone know how to solve this? I was doing this way for modulation of the addon.

2

There are 2 answers

2
Raphael On BEST ANSWER

I found the problem! Thank you Noitidart. In the second example I was closing the library before the str.readString(). It makes firefox crash. I tried to reduce the code on the question post and forgot about this detail, I am sorry.

6
Noitidart On

Try casting str to known length like this: var strCasted = ctypes.cast(str, ctypes.char.array(100).ptr); then try reading string like this: var jsStr = strCasted.contents.readString(); that should do the trick if not jump on #jsctypes moz channel we'll discuss it then update back here with the solution. Paste this to your url bar: irc://moznet/jsctypes

This tutorial on casting should help: https://gist.github.com/Noitidart/081ef49002a90fe43005#comment-1470308