Skip to content

Commit 52ceec3

Browse files
committed
win: optionally allow node.exe/iojs.exe to be renamed
On Windows, when node or io.js attempts to dynamically load a compiled addon, the compiled addon tries to load node.exe or iojs.exe again - depending on which import library the module used when it was linked. This causes many compiled addons to break when node.exe or iojs.exe are renamed, because when the binary has been renamed the addon DLL can't find the (right) .exe file to load its imports from. This patch gives compiled addon developers an option to overcome this restriction by compiling a delay-load hook into their binary. The delay-load hook ensures that whenever a module tries to load imports from node.exe/iojs.exe, it'll just look at the process image, thereby making the addon work regardless of what name the node/iojs binary has. To enable this feature, the addon developer must set the 'win_delay_load_hook' option to 'true' in their binding.gyp file, like this: ``` { 'targets': [ { 'target_name': 'ernie', 'win_delay_load_hook': 'true', ... ``` Bug: nodejs/node#751 Bug: nodejs/node#965
1 parent abad2b5 commit 52ceec3

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

addon.gypi

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
'target_defaults': {
33
'type': 'loadable_module',
4+
'win_delay_load_hook': 'false',
45
'product_prefix': '',
6+
57
'include_dirs': [
68
'<(node_root_dir)/src',
79
'<(node_root_dir)/deps/uv/include',
@@ -13,11 +15,34 @@
1315
'product_extension': 'node',
1416
'defines': [ 'BUILDING_NODE_EXTENSION' ],
1517
}],
18+
1619
['_type=="static_library"', {
1720
# set to `1` to *disable* the -T thin archive 'ld' flag.
1821
# older linkers don't support this flag.
1922
'standalone_static_library': '<(standalone_static_library)'
2023
}],
24+
25+
['_win_delay_load_hook=="true"', {
26+
# If the addon specifies `'win_delay_load_hook': 'true'` in its
27+
# binding.gyp, link a delay-load hook into the DLL. This hook ensures
28+
# that the addon will work regardless of whether the node/iojs binary
29+
# is named node.exe, iojs.exe, or something else.
30+
'conditions': [
31+
[ 'OS=="win"', {
32+
'sources': [
33+
'src/win_delay_load_hook.c',
34+
],
35+
'msvs_settings': {
36+
'VCLinkerTool': {
37+
'DelayLoadDLLs': [ 'iojs.exe', 'node.exe' ],
38+
# Don't print a linker warning when no imports from either .exe
39+
# are used.
40+
'AdditionalOptions': [ '/ignore:4199' ],
41+
},
42+
},
43+
}],
44+
],
45+
}],
2146
],
2247

2348
'conditions': [

src/win_delay_load_hook.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* 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' or 'iojs.exe'
4+
* dynamically. Instead of trying to locate the .exe file it'll just return
5+
* a handle to the process image.
6+
*
7+
* This allows compiled addons to work when node.exe or iojs.exe is renamed.
8+
*/
9+
10+
#ifdef _MSC_VER
11+
12+
#define WIN32_LEAN_AND_MEAN
13+
#include <windows.h>
14+
15+
#include <delayimp.h>
16+
#include <string.h>
17+
18+
static FARPROC WINAPI load_exe_hook(unsigned int event, DelayLoadInfo* info) {
19+
if (event != dliNotePreLoadLibrary)
20+
return NULL;
21+
22+
if (_stricmp(info->szDll, "iojs.exe") != 0 &&
23+
_stricmp(info->szDll, "node.exe") != 0)
24+
return NULL;
25+
26+
HMODULE m = GetModuleHandle(NULL);
27+
return (FARPROC) m;
28+
}
29+
30+
PfnDliHook __pfnDliNotifyHook2 = load_exe_hook;
31+
32+
#endif

0 commit comments

Comments
 (0)