Skip to content

Commit f13599f

Browse files
committed
Fix inherited by @piscisaureus's work:
* nodejs/node#1251 and * nodejs/node#1266 Note: this is a disableable/optional feature and it is disabled by default (since there are chances that MSVCR chokes on linking, given if the module exports data) Issue URL: #4. PR URL: rvagg#5.
1 parent 952ec63 commit f13599f

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

addon.gypi

+25
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 has the 'win_delay_load_hook' option set to 'true', link a
27+
# delay-load hook into the DLL. That hook ensures that the addon
28+
# will work regardless of whether the node/iojs binary is named
29+
# 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

+32
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)