Search code examples
c++node.jstypesnode-ffifile-pointer

What is the reference type for Node-FFI for a c++ file pointer?


I can't figure out which 'ref' module type to use in this situation.

I have a DLL function that returns a bool and takes a file pointer as a parameter:

__declspec(dllexport) BOOL __stdcall GB_Build( FILE *fname )
{ return Greenhouse.Build(fname) == true ? TRUE : FALSE; }

And a Node-FFI binding:

var ffi = require('ffi');
var ref = require('ref');

var greenbuildlib = null;

greenbuildlib = './PGD/GreenBuild_DLL.dll';

var greenbuild = ffi.Library(greenbuildlib, {
    "GB_GetBayLength": ['double', []],
    "GB_SetBayLength": ['void', ['double']],
    "GB_Build": ['bool', [ref.types.Object]],
});

module.exports = greenbuild;

The "GB_Build" function creates a json file, and returns a bool based on if the file was created or not. fname stands for "file name" and is a C++ nullptr in the C++ code within the DLL.

I am wondering what the ref.type would be to properly pass a file pointer in Node-FFI. I've tried null pointers, null, string, and Object, but all of them crash the application when I try this in the client-code:

var file_name = "test.json";
greenbuild.GB_Build(file_name)

Thank you for any help. I couldn't find another question on stackoverflow about file pointers in Node-FFI.


Solution

  • For anybody else with this problem, since this question got no responses, the answer seems to be that Node-FFI does not have a file pointer at all. You must change the source code for C or C++ to use char * instead.