2
* Copyright (c) 2024 Huawei Device Co., Ltd.
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
* you may not use this file except in compliance with the License.
5
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
* See the License for the specific language governing permissions and
13
* limitations under the License.
16
// Heavily inspired by node-gyp
19
* When this file is linked to a DLL, it sets up a delay-load hook that
20
* intervenes when the DLL is trying to load the host executable
21
* dynamically. Instead of trying to locate the .exe file it'll just
22
* return a handle to the process image.
24
* This allows compiled addons to work when the host executable is renamed.
29
#pragma managed(push, off)
31
#ifndef WIN32_LEAN_AND_MEAN
32
#define WIN32_LEAN_AND_MEAN
41
static FARPROC WINAPI load_exe_hook(unsigned int event, PDelayLoadInfo info) {
42
if (event != dliNotePreLoadLibrary) {
46
if (_stricmp(info->szDll, "node.exe") != 0) {
47
// Case-insensitive comparision is necessary
51
HMODULE thisModule = GetModuleHandle(NULL);
52
return reinterpret_cast<FARPROC>(thisModule);
55
ExternC const PfnDliHook __pfnDliNotifyHook2 = load_exe_hook;