Farmer
Farmer

Reputation: 10983

Binding data using Node.js FFI (javascript & c)

I'm using node ffi to call a c library, and I have some issues using this code :

This c program create a folder in the current directory and return 1 if no error.

#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>

int create(int dir) 
{
int id;
id = mkdir(dir, S_IRWXU);   
if (id == -1)
  return 1;
else
  return 2;
}

the javascript code I'm using to call this c program is :

var FFI = require("../lib/ffi");
var libsender = new FFI.Library("./libsender", {"create": [ "int", [ "int" ] ] });


if (process.argv.length < 3) {
console.log("Arguments: " + process.argv[0] + " " + process.argv[1] + " <max>");
process.exit();
}

var output;
output = libsender.create(process.argv[2])
console.log("Your output: " + output);

This program is working just if I use int instead of char in the create function (like shown), but I need to use char * in this function. So if I change the function to create(char * dir) I't does not work.

I tried to change the call in the js from

{"create": [ "int", ["int"] ] });

to

{"create": [ "int", ["char *"] ] });.

can you help with that ?

Thanks

Upvotes: 1

Views: 2475

Answers (1)

VisaMasterCard
VisaMasterCard

Reputation: 1696

Try to change int create(int dir) to int create(char* dir), and…

var libsender = new FFI.Library("./libsender", { "create": [ "int", [ "int" ] ]

…to…

var libsender = new FFI.Library("./libsender", { "create": [ "int", [ "string" ] ]

Upvotes: 2

Related Questions