Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Delphi stdcall function with pAnisChar from node js

I have a legacy Delphi dll which requires a json string as input (pAnsiChar) and returns an int as success or failure. I have managed to connect to the dll from nodejs using node-ffi. However, i am getting return int value points to invalid json string.

Could someone point me in the direction as to how to call a Delphi dll with pAnsiChar as function arguments from node

Thanks

like image 870
user3234437 Avatar asked Jan 12 '23 09:01

user3234437


2 Answers

So far as I can tell, Node FFI does not currently allow you to control the calling convention. And the default is cdecl. So on the Delphi side it looks like this:

function MyFunction(str: PAnsiChar): Integer; cdecl;

On the node-ffi side I think it looks like this:

var ffi = require('ffi');
var mylib = ffi.Library('libname', {
  'MyFunction': [ 'int', [ 'string' ] ]
});
var retval = mylib.MyFunction("some string");

If you cannot modify the legacy DLL then I'm afraid that you may need to wrap it in a DLL that does nothing other than export cdecl functions, and then pass them through to the legacy DLL's stdcall functions.

like image 118
David Heffernan Avatar answered Jan 22 '23 08:01

David Heffernan


PAnsiChar in Delphi is a char* in C/C++. In the FFI declaration for the DLL function, simply declare the PAnsiChar parameter as a "string", which is a null-terminated char* in FFI.

For example, given this Delphi function:

function ProcessJson(Json: PAnsiChar): Integer; stdcall;

The node.js code would look something like this:

var ffi = require('ffi');

var mydll = ffi.Library('mydll', {
  'ProcessJson': [ 'int', [ 'string' ] ]
});

var ret = mydll.ProcessJson("json content here");
like image 20
Remy Lebeau Avatar answered Jan 22 '23 10:01

Remy Lebeau