nodejs / node-api-headers

Repository used to make the N-API headers more accessible
MIT License
32 stars 13 forks source link

How does the addon compile by mingw64 execute on the electron #28

Open hileez opened 1 year ago

hileez commented 1 year ago

I used mingw64 to compile the node-api addon, and there was no problem executing it in nodejs, but there was an error prompt in Electron, I don't know how to solve it.

[5876:0518/221657.727:ERROR:crashpad_client_win.cc(844)] not connected
NickNaso commented 1 year ago

Hi @supercoderlee, is your project / repo public? Could you create a reproducible example so we can investigate better your problem? How do you compile your code?

hileez commented 1 year ago

@NickNaso This is my example. addon.cc

#include <assert.h>
#include <node_api.h>

static napi_value Method(napi_env env, napi_callback_info info) {
    napi_status status;
    napi_value world;
    status = napi_create_string_utf8(env, "hello", 5, &world);
    assert(status == napi_ok);
    return world;
}

#define DECLARE_NAPI_METHOD(name, func)                                        \
  { name, 0, func, 0, 0, 0, napi_default, 0 }

static napi_value Initialize(napi_env env, napi_value exports) {
    napi_status status;
    napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method);
    status = napi_define_properties(env, exports, 1, &desc);
    assert(status == napi_ok);
    return exports;
}

extern "C" {
     NAPI_EXTERN  napi_value napi_register_module_v1(napi_env env, napi_value exports) {
        return (napi_value) Initialize(env, exports);
    }
}

build

g++ -shared -o build/Release/addon.node addon.cc -I include -L lib -lnode_api

node_api is lib/libnode_api.a

index.js

var addon = require('bindings')("addon");
console.log(addon.hello()); // 'world'
NickNaso commented 1 month ago

Hi @supercoderlee, I hope that you solved your problem. It seems that you need to compile win_delay_load_hook when you compile for Electron like reported in the documentation here: https://www.electronjs.org/docs/latest/tutorial/using-native-node-modules#a-note-about-win_delay_load_hook You can find the source code of the hook here: https://github.com/cmake-js/cmake-js/blob/master/lib/cpp/win_delay_load_hook.cc or here: https://github.com/nodejs/node-gyp/blob/main/src/win_delay_load_hook.cc