11
11
12
12
#define WIN32_LEAN_AND_MEAN
13
13
#include <windows.h>
14
-
14
+ #include <Shlwapi.h>
15
15
#include <delayimp.h>
16
16
#include <string.h>
17
17
18
18
static FARPROC WINAPI load_exe_hook (unsigned int event , DelayLoadInfo * info ) {
19
- HMODULE m ;
19
+
20
20
if (event != dliNotePreLoadLibrary )
21
21
return NULL ;
22
22
@@ -25,13 +25,34 @@ static FARPROC WINAPI load_exe_hook(unsigned int event, DelayLoadInfo* info) {
25
25
_stricmp (info -> szDll , "node.dll" ) != 0 )
26
26
return NULL ;
27
27
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 ;
32
52
}
33
53
34
- return (FARPROC ) m ;
54
+ // Fallback to the current executable, which must statically link to node.lib.
55
+ return (FARPROC )processModule ;
35
56
}
36
57
37
58
PfnDliHook __pfnDliNotifyHook2 = load_exe_hook ;
0 commit comments