win_delay_load_hook.cc 872 B

123456789101112131415161718192021222324252627282930313233343536373839
  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 the host executable
  4. * dynamically. Instead of trying to locate the .exe file it'll just
  5. * return a handle to the process image.
  6. *
  7. * This allows compiled addons to work when the host executable is renamed.
  8. */
  9. #ifdef _MSC_VER
  10. #pragma managed(push, off)
  11. #ifndef WIN32_LEAN_AND_MEAN
  12. #define WIN32_LEAN_AND_MEAN
  13. #endif
  14. #include <windows.h>
  15. #include <delayimp.h>
  16. #include <string.h>
  17. static FARPROC WINAPI load_exe_hook(unsigned int event, DelayLoadInfo* info) {
  18. HMODULE m;
  19. if (event != dliNotePreLoadLibrary)
  20. return NULL;
  21. if (_stricmp(info->szDll, HOST_BINARY) != 0)
  22. return NULL;
  23. m = GetModuleHandle(NULL);
  24. return (FARPROC) m;
  25. }
  26. decltype(__pfnDliNotifyHook2) __pfnDliNotifyHook2 = load_exe_hook;
  27. #pragma managed(pop)
  28. #endif