Skip to content

Commit a1394ef

Browse files
committed
build: also intercept libnode.dll in the delay-load hook
Covers addons linked against a shared-library Node import lib. In this repo's own build the generated import library is NAME NODE.EXE, so the new name is inert here; /ignore:4199 silences LNK4199 for whichever of the two names an import table does not reference (node-gyp does the same).
1 parent 507943b commit a1394ef

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ function(add_node_api_cts_addon ADDON_NAME)
4444
# Delay-load the node.exe imports and resolve them against the host
4545
# process at runtime, so the addons can be loaded by any Node-API host
4646
# executable regardless of its file name (the node-gyp approach).
47+
# libnode.dll covers addons linked against a shared-library Node build;
48+
# /ignore:4199 silences the warning for whichever of the two names the
49+
# import library does not reference (node-gyp does the same).
4750
target_sources(${ADDON_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/src/win_delay_load_hook.cc)
4851
target_link_libraries(${ADDON_NAME} PRIVATE delayimp)
49-
target_link_options(${ADDON_NAME} PRIVATE "/DELAYLOAD:NODE.EXE")
52+
target_link_options(${ADDON_NAME} PRIVATE
53+
"/DELAYLOAD:NODE.EXE" "/DELAYLOAD:LIBNODE.DLL" "/ignore:4199")
5054
endif()
5155
target_compile_features(${ADDON_NAME} PRIVATE cxx_std_17)
5256
target_compile_definitions(${ADDON_NAME} PRIVATE ADDON_NAME=${ADDON_NAME})

‎src/win_delay_load_hook.cc‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* When this file is linked to a DLL, it sets up a delay-load hook that
3-
* intervenes when the DLL is trying to load 'node.exe' dynamically. Instead
4-
* of trying to locate the .exe file it'll just return a handle to the
5-
* process image.
3+
* intervenes when the DLL is trying to load 'node.exe' or 'libnode.dll'
4+
* dynamically. Instead of trying to locate the file it'll just return a
5+
* handle to the process image (or to libnode.dll if the host loaded one).
66
*
77
* This allows the test addons to load into any Node-API host executable,
88
* regardless of its file name. Same approach as node-gyp's
@@ -25,7 +25,8 @@ static FARPROC WINAPI load_exe_hook(unsigned int event, DelayLoadInfo* info) {
2525
if (event != dliNotePreLoadLibrary)
2626
return NULL;
2727

28-
if (_stricmp(info->szDll, "node.exe") != 0)
28+
if (_stricmp(info->szDll, "node.exe") != 0 &&
29+
_stricmp(info->szDll, "libnode.dll") != 0)
2930
return NULL;
3031

3132
// Prefer libnode.dll to support a Node.js built as a shared library.

0 commit comments

Comments
 (0)