dynamically load IsWow64Process2

This commit is contained in:
Caelan Sayler
2024-06-10 21:56:07 +01:00
parent 32e7d81621
commit 4f250044b1
3 changed files with 43 additions and 8 deletions

11
src/Rust/Cargo.lock generated
View File

@@ -750,6 +750,16 @@ version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "libloading"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19"
dependencies = [
"cfg-if 1.0.0",
"windows-targets 0.52.5",
]
[[package]]
name = "libredox"
version = "0.0.1"
@@ -1623,6 +1633,7 @@ dependencies = [
"image",
"lazy_static",
"libc",
"libloading",
"log",
"memmap2",
"native-dialog",

View File

@@ -128,6 +128,7 @@ windows-sys = { version = "0.52", default-features = false, features = [
] }
normpath = "1.0.1"
webview2-com = "0.30"
libloading = "0.8"
[dev-dependencies]
tempfile = "3.9"

View File

@@ -1,3 +1,5 @@
use anyhow::Result;
#[derive(PartialEq, Debug, Clone, strum::IntoStaticStr)]
pub enum RuntimeArch {
X86,
@@ -7,7 +9,7 @@ pub enum RuntimeArch {
impl RuntimeArch {
#[cfg(target_os = "windows")]
fn from_u32(value: u32) -> Option<Self> {
fn from_u16(value: u16) -> Option<Self> {
match value {
0x014c => Some(RuntimeArch::X86),
0x8664 => Some(RuntimeArch::X64),
@@ -51,16 +53,12 @@ impl RuntimeArch {
#[cfg(target_os = "windows")]
fn check_arch_windows() -> Option<RuntimeArch> {
use windows::Win32::Foundation::{FALSE, TRUE};
use windows::Win32::System::SystemInformation::IMAGE_FILE_MACHINE;
use windows::Win32::System::Threading::{GetCurrentProcess, IsWow64Process, IsWow64Process2};
use windows::Win32::System::Threading::{GetCurrentProcess, IsWow64Process};
unsafe {
let handle = GetCurrentProcess();
let mut process_machine: IMAGE_FILE_MACHINE = Default::default();
let mut native_machine: IMAGE_FILE_MACHINE = Default::default();
if let Ok(()) = IsWow64Process2(handle, &mut process_machine, Some(&mut native_machine)) {
return RuntimeArch::from_u32(native_machine.0.into());
if let Ok(x) = is_wow64_process2(handle) {
return RuntimeArch::from_u16(x);
}
let mut iswow64 = FALSE;
@@ -80,6 +78,31 @@ fn check_arch_windows() -> Option<RuntimeArch> {
}
}
#[cfg(target_os = "windows")]
type IsWow64Process2Fn = unsafe extern "system" fn(
hProcess: windows::Win32::Foundation::HANDLE,
pprocessmachine: *mut windows::Win32::System::SystemInformation::IMAGE_FILE_MACHINE,
pnativemachine: *mut windows::Win32::System::SystemInformation::IMAGE_FILE_MACHINE,
) -> windows::Win32::Foundation::BOOL;
#[cfg(target_os = "windows")]
unsafe fn is_wow64_process2(handle: windows::Win32::Foundation::HANDLE) -> Result<u16> {
use windows::Win32::Foundation::TRUE;
use windows::Win32::System::SystemInformation::IMAGE_FILE_MACHINE;
let lib = libloading::Library::new("kernel32.dll")?;
let func: libloading::Symbol<IsWow64Process2Fn> = lib.get(b"IsWow64Process2")?;
let mut process_machine: IMAGE_FILE_MACHINE = Default::default();
let mut native_machine: IMAGE_FILE_MACHINE = Default::default();
let result = func(handle, &mut process_machine, &mut native_machine);
if result == TRUE {
Ok(native_machine.0)
} else {
Err(anyhow::anyhow!("IsWow64Process2 failed"))
}
}
#[test]
#[cfg(target_os = "windows")]
fn test_current_architecture() {