feat(napi): switch to windows crate
This commit is contained in:
parent
f57651f749
commit
a0c443fdfb
2 changed files with 12 additions and 48 deletions
|
@ -31,7 +31,7 @@ ctor = "0.1"
|
||||||
napi-sys = {version = "1.1.2", path = "../sys"}
|
napi-sys = {version = "1.1.2", path = "../sys"}
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
winapi = {version = "0.3.9", features = ["winuser", "minwindef", "ntdef", "libloaderapi"]}
|
windows = {version = "0.27", features = ["Win32_System_WindowsProgramming", "Win32_System_LibraryLoader", "Win32_Foundation"]}
|
||||||
|
|
||||||
[dependencies.encoding_rs]
|
[dependencies.encoding_rs]
|
||||||
optional = true
|
optional = true
|
||||||
|
|
|
@ -10,70 +10,34 @@
|
||||||
//! [win_delay_load_hook.cc]: https://github.com/nodejs/node-gyp/blob/e18a61afc1669d4897e6c5c8a6694f4995a0f4d6/src/win_delay_load_hook.cc
|
//! [win_delay_load_hook.cc]: https://github.com/nodejs/node-gyp/blob/e18a61afc1669d4897e6c5c8a6694f4995a0f4d6/src/win_delay_load_hook.cc
|
||||||
|
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use std::ptr::null_mut;
|
|
||||||
use winapi::shared::minwindef::{BOOL, DWORD, FARPROC, HMODULE, LPVOID};
|
use windows::Win32::Foundation::{HINSTANCE, PSTR};
|
||||||
use winapi::shared::ntdef::LPCSTR;
|
use windows::Win32::System::LibraryLoader::GetModuleHandleA;
|
||||||
use winapi::um::libloaderapi::GetModuleHandleA;
|
use windows::Win32::System::WindowsProgramming::{DELAYLOAD_INFO, PDELAYLOAD_FAILURE_DLL_CALLBACK};
|
||||||
|
|
||||||
// Structures hand-copied from
|
// Structures hand-copied from
|
||||||
// https://docs.microsoft.com/en-us/cpp/build/reference/structure-and-constant-definitions
|
// https://docs.microsoft.com/en-us/cpp/build/reference/structure-and-constant-definitions
|
||||||
|
|
||||||
#[repr(C)]
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
struct DelayLoadProc {
|
|
||||||
fImportByName: BOOL,
|
|
||||||
// Technically this is `union{LPCSTR; DWORD;}` but we don't access it anyways.
|
|
||||||
szProcName: LPCSTR,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[repr(C)]
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
struct DelayLoadInfo {
|
|
||||||
/// size of structure
|
|
||||||
cb: DWORD,
|
|
||||||
/// raw form of data (everything is there)
|
|
||||||
/// Officially a pointer to ImgDelayDescr but we don't access it.
|
|
||||||
pidd: LPVOID,
|
|
||||||
/// points to address of function to load
|
|
||||||
ppfn: *mut FARPROC,
|
|
||||||
/// name of dll
|
|
||||||
szDll: LPCSTR,
|
|
||||||
/// name or ordinal of procedure
|
|
||||||
dlp: DelayLoadProc,
|
|
||||||
/// the hInstance of the library we have loaded
|
|
||||||
hmodCur: HMODULE,
|
|
||||||
/// the actual function that will be called
|
|
||||||
pfnCur: FARPROC,
|
|
||||||
/// error received (if an error notification)
|
|
||||||
dwLastError: DWORD,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
type PfnDliHook = unsafe extern "C" fn(dliNotify: usize, pdli: *const DelayLoadInfo) -> FARPROC;
|
|
||||||
|
|
||||||
const HOST_BINARIES: &[&[u8]] = &[b"node.exe", b"electron.exe"];
|
const HOST_BINARIES: &[&[u8]] = &[b"node.exe", b"electron.exe"];
|
||||||
|
|
||||||
unsafe extern "C" fn load_exe_hook(event: usize, info: *const DelayLoadInfo) -> FARPROC {
|
unsafe extern "C" fn load_exe_hook(event: u32, info: *const DELAYLOAD_INFO) -> HINSTANCE {
|
||||||
if event != 0x01
|
if event != 0x01
|
||||||
/* dliNotePreLoadLibrary */
|
/* dliNotePreLoadLibrary */
|
||||||
{
|
{
|
||||||
return null_mut();
|
return HINSTANCE::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
let dll_name = CStr::from_ptr((*info).szDll);
|
let dll_name = CStr::from_ptr((*info).TargetDllName.0 as *mut i8);
|
||||||
if !HOST_BINARIES
|
if !HOST_BINARIES
|
||||||
.iter()
|
.iter()
|
||||||
.any(|&host_name| host_name == dll_name.to_bytes())
|
.any(|&host_name| host_name == dll_name.to_bytes())
|
||||||
{
|
{
|
||||||
return null_mut();
|
return HINSTANCE::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
let exe_handle = GetModuleHandleA(null_mut());
|
GetModuleHandleA(PSTR::default())
|
||||||
|
|
||||||
// PfnDliHook sometimes has to return a FARPROC, sometimes an HMODULE, but only one
|
|
||||||
// of them could be specified in the header, so we have to cast our HMODULE to that.
|
|
||||||
exe_handle as FARPROC
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
static mut __pfnDliNotifyHook2: *mut PfnDliHook = load_exe_hook as *mut PfnDliHook;
|
static mut __pfnDliNotifyHook2: *mut PDELAYLOAD_FAILURE_DLL_CALLBACK =
|
||||||
|
load_exe_hook as *mut PDELAYLOAD_FAILURE_DLL_CALLBACK;
|
||||||
|
|
Loading…
Reference in a new issue