How to call function with arguments( char *, int *) in below code using node-ffi?

715 Views Asked by At

C++ third party library function

int getUCode( char* sample, int* size);

Usage

char Code[256] = {0};
length = sizeof(Code);
ret = getUCode(Code, &length);

Trying to Call above getUCode function using ffi-napi. But getting empty buffer.

const charPointer = ref.refType('char')

"getUCode": [
    "int32", [charPointer ,"int32"]

var str = Buffer.alloc(256)
var Len = 256
var val = lib.getUCode(str,Len);
1

There are 1 best solutions below

0
offef On

We can use ref.refType to tell integer pointer in function signature

    var int = ref.types.int;
    var intPtr = ref.refType(int);
    var cstring = ref.types.CString;
    
    "getUCode": [
        "int32", [ cstring ,intPtr]
      ],

Use Buffer to pass actual value in function calls

    var str = Buffer.alloc(256)
    const Len = ref.alloc(int, 256)
    var val =  lib.getUCode(str, len)