diff --git a/Cargo.toml b/Cargo.toml index 587bfee..57c8b7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,9 +15,4 @@ Get os native machine id without root permission. winreg = "0.52" [target.'cfg(target_os = "illumos")'.dependencies] -libc = "0.2.155" - -# [target.'cfg(windows)'.build-dependencies] -[build-dependencies] -cc = "1.0" -bindgen = "0.69" +libc = "0.2.155" \ No newline at end of file diff --git a/build.rs b/build.rs deleted file mode 100644 index 21606ff..0000000 --- a/build.rs +++ /dev/null @@ -1,12 +0,0 @@ -use std::env; -fn main() { - let target_os = env::var("CARGO_CFG_TARGET_OS"); - if target_os.is_ok() && target_os.unwrap() == "windows" { - use cc::Build; - // println!("cargo:rustc-link-lib=Kernel.a"); - Build::new() - .file("src/win.cpp") - .cpp_link_stdlib("stdc++") - .compile("machine-uid"); - } -} diff --git a/src/lib.rs b/src/lib.rs index e0b39d6..792ecd1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -162,19 +162,46 @@ mod machine_id { #[cfg(target_os = "windows")] pub mod machine_id { use std::error::Error; - use std::ffi::c_int; + use std::ffi::c_void; use winreg::enums::{HKEY_LOCAL_MACHINE, KEY_READ, KEY_WOW64_64KEY}; use winreg::RegKey; - extern "C" { - fn MachineUidIsWow64() -> c_int; + type BOOL = i32; + type HANDLE = *mut c_void; + type PBOOL = *mut BOOL; + + extern "system" { + fn GetModuleHandleA(lpModuleName: *const u8) -> HANDLE; + fn GetProcAddress(hModule: HANDLE, lpProcName: *const u8) -> *const c_void; + fn GetCurrentProcess() -> HANDLE; + } + + const KERNEL32: *const u8 = b"kernel32.dll\0".as_ptr(); + const ISWOW64PROCESS: *const u8 = b"IsWow64Process\0".as_ptr(); + + type LpfnIswow64process = unsafe extern "system" fn(HANDLE, PBOOL) -> BOOL; + + fn machine_uid_is_wow64() -> bool { + unsafe { + let mut b_is_wow64: BOOL = 0; + let h_module = GetModuleHandleA(KERNEL32); + let fn_is_wow64_process: Option = + std::mem::transmute(GetProcAddress(h_module, ISWOW64PROCESS)); + + if let Some(fn_is_wow64_process) = fn_is_wow64_process { + if fn_is_wow64_process(GetCurrentProcess(), &mut b_is_wow64) == 0 { + // Handle error if needed + } + } + b_is_wow64 == 1 + } } /// Return machine id pub fn get_machine_id() -> Result> { let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); - let flag = if unsafe { MachineUidIsWow64() == 1 } && cfg!(target_pointer_width = "32") { + let flag = if machine_uid_is_wow64() && cfg!(target_pointer_width = "32") { KEY_READ | KEY_WOW64_64KEY } else { KEY_READ diff --git a/src/win.cpp b/src/win.cpp deleted file mode 100644 index 764c217..0000000 --- a/src/win.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include - -// https://learn.microsoft.com/en-us/windows/win32/api/wow64apiset/nf-wow64apiset-iswow64process - -typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); - -static LPFN_ISWOW64PROCESS fnIsWow64Process; - -extern "C" BOOL MachineUidIsWow64() -{ - BOOL bIsWow64 = FALSE; - - //IsWow64Process is not available on all supported versions of Windows. - //Use GetModuleHandle to get a handle to the DLL that contains the function - //and GetProcAddress to get a pointer to the function if available. - - fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress( - GetModuleHandle(TEXT("kernel32")),"IsWow64Process"); - - if(NULL != fnIsWow64Process) - { - if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64)) - { - //handle error - } - } - return bIsWow64; -} \ No newline at end of file