Skip to content

Commit a7fcf44

Browse files
author
Justin Chase
committed
Update load_exe_hook based on feedback
1 parent 8d9ee68 commit a7fcf44

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

addon.gypi

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
'sources': [
4343
'<(node_gyp_dir)/src/win_delay_load_hook.c',
4444
],
45+
'libraries': [
46+
'-lShlwapi.lib'
47+
],
4548
'msvs_settings': {
4649
'VCLinkerTool': {
4750
'DelayLoadDLLs': [ 'iojs.exe', 'node.exe', 'node.dll' ],

src/win_delay_load_hook.c

+28-7
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
#define WIN32_LEAN_AND_MEAN
1313
#include <windows.h>
14-
14+
#include <Shlwapi.h>
1515
#include <delayimp.h>
1616
#include <string.h>
1717

1818
static FARPROC WINAPI load_exe_hook(unsigned int event, DelayLoadInfo* info) {
19-
HMODULE m;
19+
2020
if (event != dliNotePreLoadLibrary)
2121
return NULL;
2222

@@ -25,13 +25,34 @@ static FARPROC WINAPI load_exe_hook(unsigned int event, DelayLoadInfo* info) {
2525
_stricmp(info->szDll, "node.dll") != 0)
2626
return NULL;
2727

28-
m = GetModuleHandle("node.dll");
29-
if (m == NULL)
30-
{
31-
m = GetModuleHandle(NULL);
28+
// Get a handle to the current process executable.
29+
HMODULE processModule = GetModuleHandle(NULL);
30+
31+
// Get the path to the executable.
32+
TCHAR processPath[_MAX_PATH];
33+
GetModuleFileName(processModule, processPath, _MAX_PATH);
34+
35+
// Get the name of the current executable.
36+
LPSTR processName = PathFindFileName(&processPath[0]);
37+
38+
// If the current process is node or iojs, then just return the proccess module.
39+
if (_stricmp(processName, "node.exe") == 0 ||
40+
_stricmp(processName, "iojs.exe") == 0) {
41+
return (FARPROC)processModule;
42+
}
43+
44+
// If it is another process, attempt to load 'node.dll' from the same directory.
45+
PathRemoveFileSpec(&processPath[0]);
46+
PathAppend(&processPath[0], "node.dll");
47+
48+
HMODULE nodeDllModule = GetModuleHandle(&processPath[0]);
49+
if(nodeDllModule != NULL) {
50+
// This application has a node.dll in the same directory as the executable, use that.
51+
return (FARPROC)nodeDllModule;
3252
}
3353

34-
return (FARPROC) m;
54+
// Fallback to the current executable, which must statically link to node.lib.
55+
return (FARPROC)processModule;
3556
}
3657

3758
PfnDliHook __pfnDliNotifyHook2 = load_exe_hook;

0 commit comments

Comments
 (0)