TooTallNate / ref

Turn Buffer instances into "pointers"
http://tootallnate.github.com/ref
453 stars 141 forks source link

Something weird is happing with ffi, ref #36

Closed kimtn-bitmark closed 9 years ago

kimtn-bitmark commented 9 years ago

I' trying to passing a pointer variable to C function from node with ffi, ref

In C file:

EXPORT void test(char *a) {
  printf("%s\n", a);

  a[0] = 122;

  printf("%s\n", a);
}

In js file:

var ref = require("ref");
var ffi = require('../');

// typedef
var stringPtr = ref.refType(ref.types.CString);

var libencrypt = ffi.Library('./mylib', {
  'test': ['void', [stringPtr]]
})

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

var in_put1 = ref.alloc(ref.types.CString, process.argv[2]);
var test = libencrypt.test(in_put1)
console.log(in_put1.deref());

I run this command:

node mytest.js abc

And the result look like:


z�
==== :

In my expected the result should be:

abc
zbc
==== :zbc

I'm using: ffi: 1.3.2 ref: 1.0.2 node: 0.10

TooTallNate commented 9 years ago

Your stringPtr is really a char **. Ditch the refType() call, and just pass in ref.types.CString directly.

kimtn-bitmark commented 9 years ago

I was use ref.types.CString, but the result does not like my expect. It's look like we are passing a String not a pointer of String. Purpose of my function are convert the first character to "z" but i don't want to use return value for that function(because it's should be fine when i want to pass multi input)

Example: Input: abc --> Output: zbc