I think you're close here... So one of the slightly confusing points is
that Buffer instances already have 1 level of indirection by nature
(they're a pointer to some process memory), so when you do:

var lengthpointer = ref.alloc('uint *');

You're really creating a Buffer that "can store a 'uint *'", so it itself
is a "uint **" Buffer. Does that make sense? You're probably getting NaN
for the length since when you call .deref() you're actually dereffing to a
"uint *" Buffer, rather than a "uint" Number value.

So on that note, try something like this (there's basically just 1 level of
indirection removed from the variables):

  var mylib = ffi.Library('/path/to/mylib', {
    makeData: ['int', ['char **', 'uint *']],
  });

  var lengthpointer = ref.alloc('uint');
  var datapointer = ref.alloc('char *');
  mylib.makeData(datapointer, lengthpointer);
  var length = lengthpointer.deref();
  var data = ref.reinterpret(datapointer.deref(), length);

Warning, untested! But let me know if that works out for you. FWIW I think
you want a `datapointer.readCString()` call somewhere in there as well
(instead of ref.reinterpret() perhaps?).

Finally, this code might be able to help you out since it seems similar:
https://github.com/TooTallNate/NodObjC/blob/78197b330f1c8ca42f9185c298f346f4314e5099/lib/core.js#L150-L160

Cheers!


On Wed, Jun 19, 2013 at 2:20 AM, Ryan Schmidt <[email protected]>wrote:

> This is my first day of trying to use node-ffi, and I'm going mad trying
> to get it to work with a function that has this signature:
>
> int makeData(char **data, unsigned int *length);
>
> This function will return a pointer to some data and an unsigned int
> saying how many bytes long the data is. The data might be text or binary.
>
> How do I define this function in node-ffi? What kind of variables do I
> need to declare and pass to it? And how do I get the data out in the end? I
> have spent hours on this already, finding very little information on Google
> and only a single example in the node-ffi repository (which doesn't cover
> this use case), and sparse documentation.
>
> What I have at the moment is:
>
> var mylib = ffi.Library('/path/to/mylib', {
>   makeData: ['int', ['char **', 'uint *']],
> });
>
> var lengthpointer = ref.alloc('uint *');
> var datapointer = ref.alloc('char *');
> mylib.makeData(datapointer, lengthpointer);
> var length = lengthpointer.deref();
> var data = ref.reinterpret(datapointer.deref(), length);
>
> The problem is that length is always NaN. If I set length to some integer
> instead, then the reinterpreting of the buffer works and I get that amount
> of data.
>
> A surprise is that if I alloc() datapointer before lengthpointer instead
> of after, deref()ing lengthpointer causes a segmentation fault. I really
> don't understand why the order in which I declare some variables would have
> this effect.
>
> If I declare datapointer as alloc('char **') (to match the function's
> signature) instead of 'char *' I also get a segmentation fault when
> deref()ing lengthpointer.
>
> This is on OS X 10.8.4 with node 0.10.12 and node-ffi 1.2.5.
>
> --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> 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 post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> 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 [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
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 post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to