nwjs / nw-gyp

native addon build tool for node-webkit
MIT License
195 stars 46 forks source link

[BUG] win_delay_load_hook #100

Open ColonelBundy opened 7 years ago

ColonelBundy commented 7 years ago
node_modules\nw-gyp\src\win_delay_load_hook.cc(34): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
node_modules\nw-gyp\src\win_delay_load_hook.cc(35): error C2664: 'HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from 'const char [7]' to  'LPCWSTR' [node_modules\grpc\build\z.vcxp roj]

Solved using GetModuleHandleA instead of GetModuleHandle in nw-gyp/src/win_delay_load-hook.cc

I don't understand yet why GetModuleHandle fails but I thought it would be good to document this.

cecilpeng commented 6 years ago

same error when compiling diskusage

ccharly commented 6 years ago

Hi, from my understanding this is issue concerns unicode support as GetModuleHandle is actually a macro depending on which encoding you're using while compiling. Thus said, GetModuleHandle is either GetModuleHandleA when using standard char and GetModuleHandleW for wchar_t.

I've fixed it locally including #include <tchar.h> and replacing calls to GetModuleHandle(...) by GetModuleHandle(_T(...)).

Here is the link that address this kind of issue with encoding.

This seems to fix the problem when compiling node-pty on my windows machine. Here is the full code:

/*
 * When this file is linked to a DLL, it sets up a delay-load hook that
 * intervenes when the DLL is trying to load 'node.exe' or 'iojs.exe'
 * dynamically. Instead of trying to locate the .exe file it'll just return
 * a handle to the process image.
 *
 * This allows compiled addons to work when node.exe or iojs.exe is renamed.
 */

#ifdef _MSC_VER

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <windows.h>

#include <delayimp.h>
#include <string.h>
// Required for _T in GetModuleHandle(_T(...))
#include <tchar.h>

static HMODULE node_dll = NULL;
static HMODULE nw_dll = NULL;

static FARPROC WINAPI load_exe_hook(unsigned int event, DelayLoadInfo* info) {
  if (event == dliNotePreGetProcAddress) {
    FARPROC ret = NULL;
    ret = GetProcAddress(node_dll, info->dlp.szProcName);
    if (ret)
      return ret;
    ret = GetProcAddress(nw_dll, info->dlp.szProcName);
    return ret;
  }
  if (event == dliStartProcessing) {
    node_dll = GetModuleHandle(_T("node.dll"));
    nw_dll = GetModuleHandle(_T("nw.dll"));
    return NULL;
  }
  if (event != dliNotePreLoadLibrary)
    return NULL;

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

  return (FARPROC) node_dll;
}

decltype(__pfnDliNotifyHook2) __pfnDliNotifyHook2 = load_exe_hook;

#endif

Hope this helps! Let me know if you want a PR for that fix. Thanks.