1

When I do from a C++ Addon to NodeJS

void MyFunction(const FunctionCallbackInfo<Value>& args)
{
  Isolate       *isolate = args.GetIsolate();
  unsigned char *buffer  = "something";
  unsigned int   len     = 9;
  args.GetReturnValue().Set(buffer);
}

I get the error:

.node-gyp/9.2.0/include/node/v8.h:162:37: 
error: cannot convert ‘v8::Primitive*’ to ‘unsigned char** volatile’ in assignment

I've also looked here: how to deliver c++ array to node.js using v8 native addon but there is no answer about returning an unsigned char * from C++ to NodeJS. Instead both examples show how to route buffers from NodeJS into the C++ addon (and convert them to char *)

I would like on 'the other side' in my nodejs to have

const buffer = myaddon.myfunc();
user1709076
  • 2,538
  • 9
  • 38
  • 59

1 Answers1

1

A node.js buffer is represented as a node::Buffer object.
To create one from a byte array, use node::Buffer::Copy - for example:

Local<node::Buffer> js_buffer =
    node::Buffer::Copy(isolate,
        (const char *)buffer,
        buffer_size)
    .ToLocalChecked();
user253751
  • 57,427
  • 7
  • 48
  • 90
  • 2
    Thanks. I tried, this but am getting an error. any ideas?error: template argument 1 is invalid Local js_buffer = node::Buffer::Copy(isolate,(const char *)binary_buffer ^ ../c.cpp:152:110: error: cannot convert ‘v8::Local’ to ‘int’ in initialization fer = node::Buffer::Copy(isolate,(const char *)buffer,len).ToLocalChecked(); – user1709076 Jan 16 '18 at 17:40