win_delay_load_hook.cc 875 B

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. * When this file is linked to a DLL, it sets up a delay-load hook that
  3. * intervenes when the DLL is trying to load 'node.exe' or 'iojs.exe'
  4. * dynamically. Instead of trying to locate the .exe file it'll just return
  5. * a handle to the process image.
  6. *
  7. * This allows compiled addons to work when node.exe or iojs.exe is renamed.
  8. */
  9. #ifdef _MSC_VER
  10. #ifndef WIN32_LEAN_AND_MEAN
  11. #define WIN32_LEAN_AND_MEAN
  12. #endif
  13. #include <windows.h>
  14. #include <delayimp.h>
  15. #include <string.h>
  16. static FARPROC WINAPI load_exe_hook(unsigned int event, DelayLoadInfo* info) {
  17. HMODULE m;
  18. if (event != dliNotePreLoadLibrary)
  19. return NULL;
  20. if (_stricmp(info->szDll, "iojs.exe") != 0 &&
  21. _stricmp(info->szDll, "node.exe") != 0)
  22. return NULL;
  23. m = GetModuleHandle(NULL);
  24. return (FARPROC) m;
  25. }
  26. decltype(__pfnDliNotifyHook2) __pfnDliNotifyHook2 = load_exe_hook;
  27. #endif