|
| 1 | +#ifdef _MSC_VER |
| 2 | + |
| 3 | +#ifndef WIN32_LEAN_AND_MEAN |
| 4 | +#define WIN32_LEAN_AND_MEAN |
| 5 | +#endif |
| 6 | + |
| 7 | +// clang-format off |
| 8 | +#include <windows.h> |
| 9 | +#include <delayimp.h> |
| 10 | +// clang-format on |
| 11 | + |
| 12 | +static HMODULE NODE_DLL = nullptr; // NOLINT(*-avoid-non-const-global-variables) |
| 13 | +static HMODULE NW_DLL = nullptr; // NOLINT(*-avoid-non-const-global-variables) |
| 14 | + |
| 15 | +/** |
| 16 | + * @brief A hook function for handling delay-loaded DLL events. |
| 17 | + * |
| 18 | + * This function is called by the delay load helper for various events during the |
| 19 | + * delay loading process. It handles pre-loading of libraries, retrieving procedure |
| 20 | + * addresses, and initialization tasks. |
| 21 | + * |
| 22 | + * @param event The delay load event type. Possible values include: |
| 23 | + * - dliNotePreGetProcAddress: Called before GetProcAddress is called |
| 24 | + * - dliStartProcessing: Called when delay loading begins |
| 25 | + * - dliNotePreLoadLibrary: Called before loading a delayed DLL |
| 26 | + * @param info A pointer to a DelayLoadInfo structure containing information about |
| 27 | + * the DLL being loaded and the requested function. |
| 28 | + * |
| 29 | + * @return FARPROC A function pointer if the event was handled successfully, |
| 30 | + * or nullptr if the event was not handled or an error occurred. |
| 31 | + */ |
| 32 | +static FARPROC WINAPI load_exe_hook(unsigned int event, DelayLoadInfo* info) { |
| 33 | + if (info == nullptr) { |
| 34 | + return nullptr; |
| 35 | + } |
| 36 | + |
| 37 | + switch (event) { |
| 38 | + // If pre-getting a procedure address. |
| 39 | + case dliNotePreGetProcAddress: { |
| 40 | + const char* procName = info->dlp.szProcName; |
| 41 | + if (procName == nullptr) { |
| 42 | + return nullptr; |
| 43 | + } |
| 44 | + FARPROC ret = GetProcAddress(NODE_DLL, procName); |
| 45 | + if (ret != nullptr) { |
| 46 | + return ret; |
| 47 | + } |
| 48 | + return GetProcAddress(NW_DLL, procName); |
| 49 | + } |
| 50 | + // If starting the delay loading process. |
| 51 | + case dliStartProcessing: { |
| 52 | + NODE_DLL = GetModuleHandleA("node.dll"); |
| 53 | + NW_DLL = GetModuleHandleA("nw.dll"); |
| 54 | + return nullptr; |
| 55 | + } |
| 56 | + // If pre-loading a delayed DLL. |
| 57 | + case dliNotePreLoadLibrary: { |
| 58 | + if (info->szDll == nullptr) { |
| 59 | + return nullptr; |
| 60 | + } |
| 61 | + if (_stricmp(info->szDll, "node.exe") != 0) { |
| 62 | + return nullptr; |
| 63 | + } |
| 64 | + if (NODE_DLL == nullptr) { |
| 65 | + NODE_DLL = GetModuleHandleA(nullptr); |
| 66 | + } |
| 67 | + // NOLINTNEXTLINE(*-pro-type-reinterpret-cast) |
| 68 | + return reinterpret_cast<FARPROC>(NODE_DLL); |
| 69 | + } |
| 70 | + // Othwese, return nullptr. |
| 71 | + default: |
| 72 | + return nullptr; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// NOLINTNEXTLINE(*-avoid-non-const-global-variables,*-reserved-identifier,cert-dcl51-cpp,cert-dcl37-c) |
| 77 | +decltype(__pfnDliNotifyHook2) __pfnDliNotifyHook2 = load_exe_hook; |
| 78 | + |
| 79 | +#endif |
0 commit comments