EzDevInfo.com

node-ffi

Node.js Foreign Function Interface

Uncaught Error: Module did not self-register

I try to use node-vlc with nw.js (v0.12.0-alpha2). When i launch my app without nw.js it works, but when i launch it with nw.js i got an error:

Uncaught Error: Module did not self-register.", source: /home/alexis/Bureau/dev/jukebox/node_modules/vlc/node_modules/ffi/node_modules/bindings/bindings.js (84)

I tried some commands with nw-gyp but it couldn't help me. I am on Ubuntu 14, 64-bit.


Source: (StackOverflow)

NodeJS / node-ffi: memory leak / variable scope

I am investigating a memory leak in my nodejs script.

Please consider the following block of code (the entire source code will be at the bottom). GetImage calls a C function that allocates a buffer and it is the caller's responsibility to free it. After the line res.send(data), will var dataPtr be freed during GC?

app.get('/test', function(req, res)
{
    res.contentType('image/jpeg');
    var lengthPtr = ref.alloc('uint');
    var dataPtr = ref.alloc('uchar*');
    lib.GetImage(dataPtr, lengthPtr);
    var length = lengthPtr.deref();
    var data = ref.reinterpret(ref.deref(dataPtr), length);
    res.send(data);
});

Error message after handling approx 20000 requests:

Error: reinterpret: Cannot reinterpret from NULL pointer
    at Object.reinterpret (/home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/ref/lib/ref.js:773:21)
    at Object.handle (/home/ltse/LinuxPort/Experiments/NodeAddon/ffi.js:20:17)
    at next_layer (/home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/express/lib/router/route.js:103:13)
    at Route.dispatch (/home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/express/lib/router/route.js:107:5)
    at /home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/express/lib/router/index.js:213:24
    at Function.proto.process_params (/home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/express/lib/router/index.js:284:12)
    at next (/home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/express/lib/router/index.js:207:19)
    at Layer.expressInit [as handle] (/home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/express/lib/middleware/init.js:23:5)
    at trim_prefix (/home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/express/lib/router/index.js:255:15)
    at /home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/express/lib/router/index.js:216:9

...same error message repeated many times...

Error: reinterpret: Cannot reinterpret from NULL pointer
    at Object.reinterpret (/home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/ref/lib/ref.js:773:21)
    at Object.handle (/home/ltse/LinuxPort/Experiments/NodeAddon/ffi.js:20:17)
    at next_layer (/home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/express/lib/router/route.js:103:13)
    at Route.dispatch (/home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/express/lib/router/route.js:107:5)
    at /home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/express/lib/router/index.js:213:24
    at Function.proto.process_params (/home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/express/lib/router/index.js:284:12)
    at next (/home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/express/lib/router/index.js:207:19)
    at Layer.expressInit [as handle] (/home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/express/lib/middleware/init.js:23:5)
    at trim_prefix (/home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/express/lib/router/index.js:255:15)
    at /home/ltse/LinuxPort/Experiments/NodeAddon/node_modules/express/lib/router/index.js:216:9
FATAL ERROR: Malloced operator new Allocation failed - process out of memory

Source code for the NodeJS stuff:

var memwatch = require('memwatch');
var express = require('express');
var app = express();

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

var lib = ffi.Library('libsample', 
{
    'GetImage' : ['int', ['uchar **', 'uint *']]
});

app.get('/test', function(req, res)
{
    res.contentType('image/jpeg');
    var lengthPtr = ref.alloc('uint');
    var dataPtr = ref.alloc('uchar*');
    lib.GetImage(dataPtr, lengthPtr);
    var length = lengthPtr.deref();
    var data = ref.reinterpret(ref.deref(dataPtr), length);
    res.send(data);
});

var server = app.listen(3000, function(){
    console.log('Listening on port 3000.');
})

// memwatch
console.log('Monitoring memory usage...');
memwatch.on('leak', function(info){
    console.log(info);
});

memwatch.on('stats', function(info){
    console.log(info);
});

Source code for the C shared library:

#include "sample.h"
#include <stdio.h>
#include <stdlib.h>

int GetImage(unsigned char** data, unsigned int * length)
{
    FILE *fp;
    unsigned int lSize;
    unsigned char *buffer;

    fp = fopen("***PATH TO JPG FILE***", "rb");
    fseek( fp , 0L , SEEK_END);
    lSize = ftell( fp );
    rewind( fp );

    buffer = calloc( 1, lSize+1 );

    fread( buffer, lSize, 1, fp);
    fclose(fp);

    *data = buffer;
    *length = lSize;

    return 0;
}

Command to build the shared library:

gcc -g -c -Wall -Werror -fpic sample.c
gcc -shared -g -o libsample.so sample.o

I used the apache benchmarking tool to crash the program:

ab -n 120000 -c 4 http://localhost:3000/test

Source: (StackOverflow)

Advertisements

node-ffi - Passing string pointer to C library

I have API in C library as bellow

EXPORT void test(char *a) {
    // Do something to change value of "a"
}

And I want to passing a pointer of string to that API with node-ffi and ref. I was tried many ways but unsuccessful. Someone else can help me resolve it?


Source: (StackOverflow)

Accessing C function from Node.js

I have a requirement where I have to access a C function and expose it as an API. I am supposed to deploy it on Node.js server. I don't know how to access C function using any node package. I have tried using node-ffi package but at the time of installation it gives me error:

gyp WARN install got an error, rolling back install
gyp ERR! configure error
gyp ERR! stack Error: incorrect header check
gyp ERR! stack     at Zlib._binding.onerror (zlib.js:295:17)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "node" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\niket_kumar\AppData\Roaming\npm\node_modules\node-ffi
gyp ERR! node -v v0.10.33
gyp ERR! node-gyp -v v1.0.1
gyp ERR! not ok

npm ERR! node-ffi@0.5.7 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-ffi@0.5.7 install script.

Note that I am using Python 2.7 and Windows 7 (32-bit).


Source: (StackOverflow)

How do I handle SlowBuffer in Node.js?

Now I'm developing a usb-serial application with its dll in Node.js. This dll returns INVALID_HANDLE_VALUE, if it fails to open com port. So I want to handle the ret value in Node.js. In this case, How do I handle below ?

I'm not sure how do I compare the ret value and SlowBuffer.

DLL

#define INVALID_HANDLE_VALUE ((HANDLE)(LONG_PTR)-1
typedef HANDLE  (*OPEN)(int);
__declspec(dllexport) HANDLE opencom(int ncom)

Node.js with node-ffi

var ffi = require('ffi');
var lib = ffi.Library('serialmw.dll', {
  'opencom' : ['pointer', ['int']]
});
var hcom = null;
hcom = lib.opencom(1);
console.log(hcom); // <SlowBuffer@0xFFFFFFFFFFFFFFFF >

Source: (StackOverflow)

node-ffi : Dynamic Linking Error

I have a C++ method (which role is killing some processes), in a library Risk.dll.

On the other hand, I am developing a web-application (using Nodejs and AngularJS), running on Google Chrome. When I click on a button through the browser, I would like to be able to call the C++ function, through my app.js file.

I have decided to use Node-ffi, but I have the error :

Error: Dynamic Linking Error: "Risk.dll":

Here is my code on javascript :

var ffi = require('ffi')
//Kill Server
socket.on('killserver', function(date){

    var cTest =  ffi.Library("Risk", {
        "DateModified":["long", ["string"] ]
    });

    var res = cTest.DateModified(date);
    console.log(res)
});

In C++, I have done

extern C{
}

And I want to add that my file Risk.dll is in the same file than the file server.js I run with Node when I want to use my app.

Any clue ?


Source: (StackOverflow)

Library Handles in node-ffi

I'm playing with integrating the gssapi into node. I need some help; I'm not sure how to represent this...

The gss_init_sec_context function contains many struct based parameters, some of which are nested pretty deeply. Take for instance this struct...

typedef struct gss_ctx_id_struct
{
  gss_OID mech;
#ifdef USE_KERBEROS5
  struct _gss_krb5_ctx_struct *krb5;
#endif
} gss_ctx_id_desc;

So based on this I'm guessing I need to implement some kind of Structure (with ref-struct) to represent the krb5 pointer (since kerberos5 is being used). So I looked at this _gss_krb_ctx_struct and saw this...

 typedef struct _gss_krb5_ctx_struct {
   Shishi *sh;
   Shishi_ap *ap;
   Shishi_tkt *tkt;
   Shishi_key *key;
   gss_name_t peerptr;
   int acceptor;
   uint32_t acceptseqnr;
   uint32_t initseqnr;
   OM_uint32 flags;
   int reqdone;
   int repdone;
 } _gss_krb5_ctx_desc, *_gss_krb5_ctx_t;

Where Shishi is a pointer to a library, and shishi_ap appears to be a pointer to a function. I'm not sure how to implement these things in node-ffi. Can someone give me some guidance here? It's entirely possible I may be in over my head here, but I'm willing to press forward with this if I can find someone to help me out.


Source: (StackOverflow)

Unable to pass string array inside structure to a C function using ffi

I am trying to pass a string array to my C function using ffi as

var ffi = require('ffi');
var ref=require('ref');
var ArrayType = require('ref-array');
var Struct = require('ref-struct');

var StringArray = ArrayType('string');
var myStructure=Struct({
'ulonglong' : ref.types.ulonglong,
'charptrptr' : StringArray,
'stringArr' : StringArray
});

var structObjNew=new myStructure();
structObjNew.ulonglong=123456;
structObjNew.charptrptr=["sample","ptrptr"]; 
structObjNew.stringArr=["sample1","ptrptr1"];

var libfactorial = ffi.Library('./stringArray', {
 'readArray': [ 'string', [ myStructurePtr]] //creating header definition 
});

var output=libfactorial.readArray(structObjNew.ref()); 

.C file for the above code

struct myStructure
 {
    unsigned long long ulonglong;
    char  **charptrptr;
    char stringArr[3][10];
 };
EXPORT char* readArray(struct myStructure *nameofstr) {
  printf("Value from first param from structure of type char**\n");
  printf("%s\n",nameofstr->charptrptr[0]);
  printf("%s\n",nameofstr->charptrptr[1]);

  printf("value form second param from structure of thype char[][]\n");
  printf("%s\n",nameofstr->stringArr[0]);
  printf("%s\n",nameofstr->stringArr[1]);
}

I am getting output as

Value from first param from structure of type char**

sample

ptrptr

value form second param from structure of thype char[][]

Any garbage value like ▲é┌

i.e When I am passing an array to char** type then i am getting correct output but when I am trying to pass it to char*[] or char[][] format declared inside structure i am getting garbage value.

But directly passing string array like ["aa","bb"] to char*[] parameter in C works fine . But it is not working for me when I am using it inside structure.


Source: (StackOverflow)

node-ffi module fail to load 32bit C dll - dynamic linking error

I'm having sone trouble trying to load a C 32bit DLL with nodejs-ffi module. This is my code:

When running, node throws an error on Dynanic_library.js: 74 Dynamic Linking Error

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

import idatabase = require('./IDatabase');
import idataset = require('./Dataset');

var dllpath = path.join(__dirname, '../../StoneBaseDLL.dll');
var dbpath = path.join(__dirname, '../../../database/metadata/metadata.db');

export class Stonebase implements idatabase.IDatabase
{
    dbHandler;
    dbConnection;

    constructor() {

        this.dbHandler = ffi.Library(dllpath, {
            'connect': ['void*', ['string']],
            'Execute': ['void*', ['string', 'void*']],
            'disconnect': ['void*', ['void*']],
            'EndOfResultSet': ['bool', ['void*']],
            'Next': ['void', ['void*']],
            'getNumberOfFields': ['int', ['void*']],
            'GetField_AsString': ['string', ['int', 'void*']],
            'GetField_AsChar': ['string', ['int', 'void*']],
        });

        this.dbConnection = this.dbHandler.connect(dbpath);
    }

    execute(query: string) {
        var data: Array<Array<string>>;
        var columns: Array<string>;
        data = [];
        columns = [];
        var i = 0;

        var dset = this.dbHandler.Execute(query, this.dbConnection);

        for (var j = 0; j < this.dbHandler.getNumberOfFields(dset); j++) {
            columns[j] = "column" + j;
        }

        while (!this.dbHandler.EndOfResultSet(dset)) {
            var line: Array<string>;
            line = [];
            for (var j = 0; j < this.dbHandler.getNumberOfFields(dset); j++) {
                line[j] = (<string>this.dbHandler.GetField_AsChar(j, dset));
            }
            data[i] = line;

            this.dbHandler.Next(dset);
            i++;
        }

        return new idataset.Dataset(data, columns);
    }

}

I'm using Typescript but I'm quite sure this has nothing to do with the problem

Thank you guys for any help!


Source: (StackOverflow)

install node-ffi - Need some guidelines to take out the binaries(i.e js or lib file etc...) alone & using it. node-ffi size is big ~7MB for my case

I did installation of node-ffi using npm install and my nodejs server code is running w/o any problem.

But when I check the size of 'node_modules/ffi' ( contains the entire source ) it comes around 7MB which is big for the environment am running nodejs server.

so the question is:

  1. is it possible to take out the binaries alone for any modules ( ofcource the dependent module binaries should also be taken ) ? Seeing 'build/Release' folder inside ffi. is that enough !!!

  2. is there a way to add the required npm modules as part of nodejs server compilation itself ( case where the required modules are well known)

Any useful weblink is very much helpful .


Source: (StackOverflow)

Calling a C++ function that accepts a strings and returns a vector of ints with Node FFI

Heres the documentation on the C++ function in the DLL.

std::vector<int> loadOCTInfo(const char* fileName)
Parameter fileName: a C-style string for the local or absolute path of a valid OCT file
Return: A vector of ints, where the first number is the number of rows in each image in the OCT file, the second number is the number of columns, and the third number is the number of slices

I was looking at the Node FFI tutorial, my main concern with calling the function is that Im not sure what to put for the return type of the function. The Node FFI documentation says

ffi.Library(libraryFile, { functionSymbol: [ returnType, [ arg1Type, arg2Type, ... ], ... ]);

But what should I put for return type which would be a vector of ints?


Source: (StackOverflow)

How to define a C function in node-ffi

I'm trying to implement libgit2 git_remote_ls api using node-ffi, and stuck where I had to define a callback C function.

Trying to replicate what's given here - https://github.com/libgit2/libgit2/blob/master/examples/network/ls-remote.c

git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT
callbacks.credentials = cred_acquire_cb
git_remote_connect(remote, GIT_DIRECTION_FETCH, &callbacks);

and in common,

int cred_acquire_cb(git_cred **out,
    const char * url,
    const char * username_from_url,
    unsigned int allowed_types,
    void *payload);

How do I handle this ?


Source: (StackOverflow)

node ffi struct variable array

I'm using node-ffi and I have a structure that needs to be passed. A few of the variables in the structure are arrays. How do I define this in the JavaScript wrapper?

C header:

typedef struct
{
    unsigned char myUChar;
    float fmyFloat;
    float arrayFloat[ARRAY_SIZE];
} sMyStruct;

JavaScript wrapper:

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

var sMyStruct = Struct({
    'myUChar': 'uchar',
    'fmyFloat': 'float',
    'arrayFloat[ARRAY_SIZE]': 'float'  // Would this work??
});

Is there a tutorial which indicates all of the variables that can be passed into node-ffi anyone can point me to? (For example can I actually pass 'uchar'?)


Source: (StackOverflow)

Working with parameter passed by reference in node-ffi

I would like to make a function that will return an integer as the result of the function call and will manipulate the parameter passed by reference to return an appropriate value.

All functions of the DLL library follow the same pattern that is return a integer as result of status of execution of the function and when necessary a reference parameter is passed to the function that will have the appropriate value.

For example, "myFunction" will return some one of the following integer values as status of execution function: -1, 0, 1, 2, 3, ..., 238; and will manipulate the parameter passed by reference to return 0 or 1.

For now, I'm returning a object that contains both the status (of the execution of the function) and the value (the parameter passed by reference):

...
// "binding" with dll functions
var lib = ffi.Library(filePath, {
  "myFunction": ["int", [ref.alloc("int")]]
});

MyClass.prototype.myFunction = function(callback) {
  var intPtr = ref.alloc('int');
  if (typeof callback === "undefined") {
    var i = lib.myFunction(intPtr);
    return {
      status: i,
      val: intPtr.deref()
    };
  } else {
    lib.myFunction.async(intPtr, function(err, res) {
      var resp = {
        status: res,
        val: intPtr.deref()
      };
      callback(err, resp);
    });
  }
};

...

// example of use
var myLibrary = require("../../lib/index.js"),
my = new myLibrary.MyClass();    
// synchronous
var resp = my.myFunction();
console.log(resp, my.CheckStatus(resp.status));
console.log("Value iS: " + resp.val);

...

This works fine, but I would like follow the same pattern of the library.

Any advice?


Source: (StackOverflow)

Using unsigned char buffer with node-ffi

I'm having trouble using a buffer with node-ffi to call the following library function:

int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) { ... }

http://www.intra2net.com/en/developer/libftdi/documentation/group__libftdi.html#ga72d87e30015c98bd0be22e7c8c873345

Which I have seen used by passing a 'unsigned char buf[size]' to as the buf argument..

So with ffi I try this:

ffi = require 'ffi'
ref = require 'ref'

ftdiContext = ref.types.void
ftdiContextPtr = ref.refType ftdiContext

ftdi = ffi.Library 'libftdi',
  'ftdi_init': [ 'int', [ ftdiContextPtr ] ]
  'ftdi_usb_open': [ 'int', [ ftdiContextPtr, 'int', 'int' ] ]
  'ftdi_usb_open_desc': [ 'int', [ ftdiContextPtr, 'int', 'int', 'string', 'string'] ]
  'ftdi_get_error_string': [ 'string', [ ftdiContextPtr ] ]
  'ftdi_set_bitmode': [ 'int', [ ftdiContextPtr, 'uchar', 'uchar' ] ]
  'ftdi_set_baudrate': [ 'int', [ ftdiContextPtr, 'int' ] ]
  'ftdi_read_data': [ 'int', [ ftdiContextPtr, ref.refType(ref.types.uchar), 'int' ] ]

BITMODE_RESET  = 0x00
BITMODE_BITBANG= 0x01
BITMODE_MPSSE  = 0x02
BITMODE_SYNCBB = 0x04
BITMODE_MCU    = 0x08
BITMODE_OPTO   = 0x10
BITMODE_CBUS   = 0x20
BITMODE_SYNCFF = 0x40

inputPin = 1
outputPin = 2

rxBaudRate = 9600
txBaudRate = 4500

rxBufSize = 10

buf = new Buffer rxBufSize

ctx = ref.alloc ftdiContextPtr

console.log ftdi.ftdi_init ctx
console.log ftdi.ftdi_usb_open_desc ctx, 0x0403, 0x6001, null, 'FTG4GJL1'
console.log ftdi.ftdi_set_bitmode ctx, inputPin << outputPin , BITMODE_BITBANG
console.log ftdi.ftdi_set_baudrate ctx, rxBaudRate
console.log ftdi.ftdi_read_data ctx, buf, buf.length
console.log ftdi.ftdi_get_error_string ctx

But the result I get is:

-22
usb bulk read failed

The ftdi_read_data C function makes a call to usb_bulk_read (from libusb) which takes 'char *' as it's buffer argument - I'm not sure if this is affecting anything..

http://www.intra2net.com/en/developer/libftdi/documentation/ftdi_8c_source.html#l01749


Source: (StackOverflow)