Skip to content

How to invoke the JavaScript callback within shared library rather than addon #1287

@wanglin86769

Description

@wanglin86769

Hello, I am using node-addon-api to wrap a shared library so that JavaScript code can use the functions from the shared library. The problem is that some functions in the shared library have callback arguments, but I did not succeed to pass the JavaScript callback to the shared library via addon.
For example, my code snippet as follows does not work, and the callback that passed to get_value() within the shared library does not execute at all.

Napi::Value Callback_Test(const Napi::CallbackInfo& info) {
  if (!hGetProcIDDLL) {
    std::cout << "could not load the dynamic library" << std::endl;
    exit(EXIT_FAILURE);
  }

  CREATE_CHANNEL create_channel = (CREATE_CHANNEL)GetProcAddress(hGetProcIDDLL, "createChannel");
  if (!create_channel) {
    std::cout << "could not locate the function" << std::endl;
    exit(EXIT_FAILURE);
  }

  Napi::Env env = info.Env();

  Napi::Function cb = info[0].As<Napi::Function>();

  void * ptr_chan = NULL;
  create_channel(&ptr_chan);
  get_value(ptr_chan, 100, cb);
    
  return Napi::Number::New(env, 0);
}
var addon = require('bindings')('hello');

addon.load("C:\\Users\\wanglin\\source\\repos\\Dll3\\x64\\Debug\\Dll1.dll");

let myCallback = function myFunction(args) {
    console.log('*** Entering callback! ***');
    console.log(args);
}

addon.callback_test(myCallback);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions