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();

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I tried, this but am getting an error. any ideas?error: template argument 1 is invalid Local<node::Buffer> js_buffer = node::Buffer::Copy(isolate,(const char *)binary_buffer ^ ../c.cpp:152:110: error: cannot convert ‘v8::Local<v8::Object>’ to ‘int’ in initialization fer = node::Buffer::Copy(isolate,(const char *)buffer,len).ToLocalChecked();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.