Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build binaries that can load in electron by default. #653

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions addon.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,19 @@
# If the addon specifies `'win_delay_load_hook': 'true'` in its
# binding.gyp, link a delay-load hook into the DLL. This hook ensures
# that the addon will work regardless of whether the node/iojs binary
# is named node.exe, iojs.exe, or something else.
# is named node.exe, iojs.exe, node.dll or something else.
'conditions': [
[ 'OS=="win"', {
'sources': [
'<(node_gyp_dir)/src/win_delay_load_hook.c',
],
'libraries': [
'-lShlwapi.lib'
],
'msvs_settings': {
'VCLinkerTool': {
'DelayLoadDLLs': [ 'iojs.exe', 'node.exe' ],
# Don't print a linker warning when no imports from either .exe
'DelayLoadDLLs': [ 'iojs.exe', 'node.exe', 'node.dll' ],
# Don't print a linker warning when no imports from delay loaded binary
# are used.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment could be updated from either .exe => from delay-loaded binary

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, thanks.

'AdditionalOptions': [ '/ignore:4199' ],
},
Expand Down
37 changes: 32 additions & 5 deletions src/win_delay_load_hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,48 @@

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include <Shlwapi.h>
#include <delayimp.h>
#include <string.h>

static FARPROC WINAPI load_exe_hook(unsigned int event, DelayLoadInfo* info) {
HMODULE m;

if (event != dliNotePreLoadLibrary)
return NULL;

if (_stricmp(info->szDll, "iojs.exe") != 0 &&
_stricmp(info->szDll, "node.exe") != 0)
_stricmp(info->szDll, "node.exe") != 0 &&
_stricmp(info->szDll, "node.dll") != 0)
return NULL;

m = GetModuleHandle(NULL);
return (FARPROC) m;
// Get a handle to the current process executable.
HMODULE processModule = GetModuleHandle(NULL);

// Get the path to the executable.
TCHAR processPath[_MAX_PATH];
GetModuleFileName(processModule, processPath, _MAX_PATH);

// Get the name of the current executable.
LPSTR processName = PathFindFileName(processPath);

// If the current process is node or iojs, then just return the proccess module.
if (_stricmp(processName, "node.exe") == 0 ||
_stricmp(processName, "iojs.exe") == 0) {
return (FARPROC)processModule;
}

// If it is another process, attempt to load 'node.dll' from the same directory.
PathRemoveFileSpec(processPath);
PathAppend(processPath, "node.dll");

HMODULE nodeDllModule = GetModuleHandle(processPath);
if(nodeDllModule != NULL) {
// This application has a node.dll in the same directory as the executable, use that.
return (FARPROC)nodeDllModule;
}

// Fallback to the current executable, which must statically link to node.lib.
return (FARPROC)processModule;
}

PfnDliHook __pfnDliNotifyHook2 = load_exe_hook;
Expand Down