Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Node.js Buffer to C++ addon

Tags:

c++

node.js

v8

test.js

buf = new Buffer(100);
for (var i = 0; i < 100; i++) buf[i] = i
addon.myFync(buf);

addon.cpp

Handle<Value> set(const Arguments& args) {
    char *buf = SOMETHING(args[0]);
    return Undefined();
}

How to get the pointer to a data of the buffer inside the C++ function?

What should I write in place of SOMETHING(args[0])?

I have node_buffer.h opened in my editor, but I cannot figure out.

Node version = v0.10.29

like image 822
exebook Avatar asked May 02 '26 03:05

exebook


1 Answers

You can do:

char* buf = node::Buffer::Data(args[0]);

to directly access the bytes of a Buffer.

like image 195
loganfsmyth Avatar answered May 04 '26 15:05

loganfsmyth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!