Pass Node.js Buffer to C++ addon

半城伤御伤魂 提交于 2020-01-02 07:23:08

问题


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


回答1:


You can do:

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

to directly access the bytes of a Buffer.




回答2:


According to the node.js node binding documentation the 'arg[0]' value argument can be accessed as:

String::AsciiValue v(args[0]->ToString());


来源:https://stackoverflow.com/questions/28951978/pass-node-js-buffer-to-c-addon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!