Currently I am by importing the C++ .dll using ffi-napi calling the functions in NodeJS directly, but it requires me to define all the custom data types in NodeJS if I want to pass them as function arguments.
I am looking for an alternate method to call functions from a C++ .dll that uses custom data types or a different approach with ffi-napi.
Below is my current implementation using ffi-napi:
C++ Header:
Enum1 some_function (
Enum2 var1,
Struct1* var2,
Struct1* var3,
Enum3* var4);
);
NodeJS:
var ref = require('ref-napi');
var ffi = require('ffi-napi');
var Struct = require('ref-struct-napi');
var Enum = require('enum')
var Union = require('ref-union-napi')
var somelib = new ffi.Library("somedll.dll", {
"some_function": [
Enum1, [Enum2, Struct1*, Struct1*, Enum3*] // ignore correctness for simplicity
]});
I can define the custom data types (Enum1, Struct1, Enum3, etc.) in NodeJS using the modules shown above since NodeJS does not support these natively. Even using ref.refType(Type) to get pointers.
The difficulty is that these Struct types can be nested. For example, Struct1 in the C++ Header is defined:
typedef struct Struct1 {
Struct2 var1;
Struct3 var2;
Struct4 var3;
};
Struct1 has 3 other Structs within. I will just show how Struct4 is defined in C++ Header:
typedef struct Struct4 {
Enum4 var1;
Struct5 var2;
Struct6 var3;
union {
Struct7* var4;
Struct8* var5;
Struct9* var6;
Struct10* var7;
};
These structs may also contain a few other data types.
When I need to call some_function, I will need to pass variables of the respective argument data types
somelib.some_function(Enum2Var, &Struct1Var1, &Struct1Var2, &Enum3Var); // ignore correctness for simplicity
I am wondering if there is a better implementation than having to manually define all of these data types in NodeJS to correctly use these functions.