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
1818static 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
3758PfnDliHook __pfnDliNotifyHook2 = load_exe_hook ;
0 commit comments