How to convert javascript string to unsigned_char js in js-ctypes

288 Views Asked by At

I need to load a dll file with js-ctypes code. Here is header code in dll

typedef unsigned char BYTE;
extern "C" __declspec(dllexport) BYTE* foo(BYTE* a, const char* b);

And then, i load it with js-ctypes code:

var foo = lib.declare("foo", ctypes.default_abi, ctypes.unsigned_char.ptr, ctypes.unsigned_char.ptr, ctypes.char.ptr);

And when i called this function, i got an error

var a = ctypes.unsigned_char.ptr("a");
var b = ctypes.char.ptr("b");
var result = foo(a, b);

TypeError: can't convert the string "a" to the type ctypes.unsigned_char.ptr

Can anybody help me to solve this?

1

There are 1 best solutions below

0
Noitidart On

Your declare is correct. But to call it you should do it like this:

var a_str = 'a';
var a = ctypes.unsigned_char.array(a_str.length)(a_str);
var b_str = 'b';
var b = ctypes.char.array(b_str.length)(b_str); // `doing b.readString()` will give you "b"
var result = foo(a.address(), b.address());
console.log('result:', result, result.toString());

var result_str = result.readString();
console.log('result_str:', result_str);