Skip to content

Passing char pointer #501

@adnan-kamili

Description

@adnan-kamili

I have a simple C function with the following signature:

int GetMetadata(const char* key, char* value, size_t length);

I want to wrap this function using napi. I tried the following:

Napi::Value getMetadata(const Napi::CallbackInfo &info)
{
    Napi::Env env = info.Env();
    // some validations...
    string arg0 = info[0].As<Napi::String>();
    uint32_t arg2 = info[2].As<Napi::Number>().Uint32Value;
    char metadataValue[256] = "\0";
    Napi::Number status = Napi::Number::New(env, GetMetadata(arg0.c_str(), metadataValue, arg2));
    // how to return the value of metadataValue to calling function
    return status;
}

From Javascript, I would be passing in a buffer of length 256. How do I assign metadataValue to buffer passed in through JS?

const buffer = Buffer.alloc(256);
MyAddon.GetMetadata("someKey", buffer);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions