Calling C library from NodeJs

157 views
Skip to first unread message

Leopoldo Belmonte

unread,
Sep 24, 2015, 11:24:08 AM9/24/15
to nodejs

Could anyone help me to solve a problem with nodejs and C library (call to some dll methods)?


My dll have these methods:

void Open(char *Path);
int Execute(TINData *InData,TOUData *OutData);
void Close(void);

with these data structures:

typedef struct {
      char Value[8+1];
      char Type [1+1];
      int Id;
      unsigned char Parity;
} TINData

typedef struct {
      char Cash[8+1];
      char Telephone[11+1];
      char CallType[3+1];
      char CallResult[2+1];
      char Description[24+1];
} TOUTData

My nodejs code:

var ffi = require('ffi');
var ref = require('ref');
var StructType = require('ref-struct');



var TINData = StructType({
      'Value': 'string',
      'Type': 'string',
      'Id': 'int',
      'Parity': 'string'
});

var TOUTData = StructType({
      'Cash': 'string',
      'Telephone': 'string',
      'CallType': 'string',
      'CallResult': 'string',
      'Description': 'string'
});




var mylibrary = ffi.Library('OurLib.dll', {
      'Open' : ['void', ['string']],
      'Execute' : ['void', [TINData, TOUTData]],
      'Close' : ['void', ['void']]
});

myLibrary.Open('myConnection');

var myTINData = new TINData();           
myTINData.Value = '00000010';
myTINData.Type = '1';
myTINData.Id = 123;
myTINData.Parity = '0';

Nathan Rajlich

unread,
Sep 24, 2015, 8:40:37 PM9/24/15
to nodejs
Rather than using "string" type for those fixed-length strings, use this little Type that I whipped up (FixedString: https://gist.github.com/TooTallNate/0fe04681493a3c32da51). That way, the resulting struct size/alignment will match what it would be in C.

On top of that, you're going to need to pass in a reference to the TINData and TOUTData structs. The way you have it defined right now you're passing the structs in by value, which is wrong. So do `var TINDataPtr = ref.refType(TINDataPtr);` and use that for the first argument. Do the same thing for the OUT one. And then instead of passing myTINData directly in, you would call `myTINData.ref()` to pass a pointer to the struct rather than the value.

Hope that helps. Cheers!

Leopoldo Belmonte

unread,
Sep 28, 2015, 4:56:57 PM9/28/15
to nodejs
Hi Nathan, thank you for your answer!!!

I have modified my code:
var ffi = require('ffi');
var ref = require('ref');
var StructType = require('ref-struct');
var fixedString = require('ref-fixed-string');



var TINData = new StructType({

      'Value': 'string',
      'Type': 'string',
      'Id': 'int',
      'Parity': 'string'
});

var TINDataPtr = ref.refType(TINData);
var TOUTData = new StructType({

      'Cash': 'string',
      'Telephone': 'string',
      'CallType': 'string',
      'CallResult': 'string',
      'Description': 'string'
});

var TOUTDataPtr = ref.refType(TOUTData);



var mylibrary = ffi.Library('OurLib.dll', {
      'Open' : ['void', ['string']],

      'Execute' : ['void', [TINDataPtr, TOUTDataPtr ]],

'Close' : ['void', ['void']] }); myLibrary.Open('myConnection'); var myTINData = new TINData(); myTINData.Value = '00000010'; myTINData.Type = '1'; myTINData.Id = 123; myTINData.Parity = '0';
var myTOUTData = new TOUTData();
var i = mylibrary.Execute(myTINData.ref(), myTOUTData.ref());

    myLibrary.Close();
var pOutputData = myTOUTData.ref();



Now the debug file is filled correctly, but I get this error "Malloced operator new Allocation failed - process out of memory" in the Execute method call. Maybe I haven't enough memory for nodejs execution?

Thank you
Leo

Nathan Rajlich

unread,
Sep 28, 2015, 11:42:04 PM9/28/15
to nodejs
You don't seem to actually be *using* `fixedString` anywhere. Sorry, I should have explained better. Rather than specifying "string" as the type inside the struct types, you need to create instances of the FixedType, and specify the amount of bytes that should be allocated for that field.

So `char Value[8+1]` should become `'Value': FixedString(8+1)`.

Also I'm not sure why you're getting a reference to the `myTOUTData` instance on the last line, but I would suspect you would simply inspect the values in the struct at that point to see what the Execute() function put in that space.

Hope that helps!

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/5320263c-0f36-4322-b95f-6cbc2d0cec85%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Leopoldo Belmonte

unread,
Sep 29, 2015, 8:13:03 AM9/29/15
to nodejs
Hi nathan I'm sorry,I forgot to paste this code:

var TINData = new StructType();
TPOSData.defineProperty('Value', new fixedString(8+1));
TPOSData.defineProperty('Type', new fixedString(1+1));
TPOSData.defineProperty('Id', 'int');
TPOSData.defineProperty('Id', 'string');
var TOUTData = new StructType();
TOUTData.defineProperty('Telephone', new fixedString(11+1));
TOUTData.defineProperty('CallType', new fixedString(3+1));
TOUTData.defineProperty('CallResult', new fixedString(2+1));
TOUTData.defineProperty('Description', new fixedString(24+1));

I am getting a reference to the myTOUTData to inspect the values, to see if they are correctly in their positions ,so I am able to parse the result.

I am developing using eclipse with node plugin , and I have usually this error  "Malloced operator new Allocation failed - process out of memory"
thank you for your help
Leo

...
Reply all
Reply to author
Forward
0 new messages