diff --git a/native/windows/src/addon.cc b/native/windows/src/addon.cc index b401c2d587..4d332aad74 100644 --- a/native/windows/src/addon.cc +++ b/native/windows/src/addon.cc @@ -1,5 +1,7 @@ #include "authority.hpp" +#include + namespace rea { static const napi_type_tag RESOURCE_TAG = {0x45fcbf0b49e84fe3ULL, 0x9e3e5074130de843ULL}; @@ -121,3 +123,13 @@ static napi_value initialize(napi_env env, napi_value exports) { } // namespace rea NAPI_MODULE(NODE_GYP_MODULE_NAME, rea::initialize) + +// Node-API symbols are delay-imported from "node.exe". Resolve them against the +// host executable so Node.js, Bun, and other Node-API hosts load the addon +// regardless of their executable file name. +static FARPROC WINAPI reaHostHook(unsigned event, PDelayLoadInfo info) { + if (event != dliNotePreLoadLibrary || _stricmp(info->szDll, "node.exe") != 0) return nullptr; + return reinterpret_cast(GetModuleHandleW(nullptr)); +} + +decltype(__pfnDliNotifyHook2) __pfnDliNotifyHook2 = reaHostHook; diff --git a/scripts/build-windows-native.mjs b/scripts/build-windows-native.mjs index c77d5e7d34..152f3eddb3 100644 --- a/scripts/build-windows-native.mjs +++ b/scripts/build-windows-native.mjs @@ -84,7 +84,8 @@ if (!(await exists(join(headers, "node_api.h")))) { } const artifact = "rea-windows-x64.node"; // MSVC short-import libraries do not produce a correct Node import table with -// GNU ld. Generate a GNU import library for the exact stable Node-API symbols. +// GNU ld. Generate a GNU delay-import library for the exact stable Node-API +// symbols; addon.cc binds it to the host executable, whatever its file name. const sources = ["addon.cc", "filesystem.cc", "process.cc"]; const sourceText = ( await Promise.all( @@ -106,7 +107,7 @@ await writeFile( definition, `LIBRARY node.exe\nEXPORTS\n${symbols.join("\n")}\n`, ); -run("x86_64-w64-mingw32-dlltool", ["-d", definition, "-l", library]); +run("x86_64-w64-mingw32-dlltool", ["-d", definition, "-y", library]); run(compiler, [ "-std=c++17", "-O2",