From fb172f673ea811cfa7e0f4ebdb7caa84a338dc4b Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 12 Jun 2022 15:30:50 -0400 Subject: [PATCH] Switch to windows-rs --- Cargo.lock | 45 +++++++++++++++++++++++++++++++++------------ Cargo.toml | 16 ++++++++++++++-- src/main.rs | 35 ++++++++++++++++++++--------------- src/pid_to_exe.rs | 32 ++++++++++++++++---------------- 4 files changed, 83 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b11fa31..69ca531 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,7 +7,7 @@ name = "auto_mute_tool" version = "0.1.0" dependencies = [ "com", - "winapi", + "windows", ] [[package]] @@ -77,23 +77,44 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee" [[package]] -name = "winapi" -version = "0.3.9" +name = "windows" +version = "0.37.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +checksum = "57b543186b344cc61c85b5aab0d2e3adf4e0f99bc076eff9aa5927bcc0b8a647" dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_msvc", ] [[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" +name = "windows_aarch64_msvc" +version = "0.37.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +checksum = "2623277cb2d1c216ba3b578c0f3cf9cdebeddb6e66b1b218bb33596ea7769c3a" [[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" +name = "windows_i686_gnu" +version = "0.37.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +checksum = "d3925fd0b0b804730d44d4b6278c50f9699703ec49bcd628020f46f4ba07d9e1" + +[[package]] +name = "windows_i686_msvc" +version = "0.37.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce907ac74fe331b524c1298683efbf598bb031bc84d5e274db2083696d07c57c" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.37.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2babfba0828f2e6b32457d5341427dcbb577ceef556273229959ac23a10af33d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.37.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4dd6dc7df2d84cf7b33822ed5b86318fb1781948e9663bacd047fc9dd52259d" diff --git a/Cargo.toml b/Cargo.toml index ede4864..3cd6e58 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,17 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -com = "0.6.0" -winapi = {version = "0.3.9", features=["winuser", "processthreadsapi", "errhandlingapi", "winbase", "impl-default", "impl-debug"]} \ No newline at end of file +com = {version="0.6.0", features=["production"]} + +[dependencies.windows] +version = "0.37.0" +features = [ + "alloc", + "Data_Xml_Dom", + "Win32_UI", + "Win32_UI_Accessibility", + "Win32_Foundation", + "Win32_Security", + "Win32_System_Threading", + "Win32_UI_WindowsAndMessaging", +] \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index efd3735..04e0109 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,23 @@ mod pid_to_exe; -use std::ptr::null_mut; -use winapi::{ - shared::windef::{HWINEVENTHOOK__, HWND__}, - um::winuser::{ - DispatchMessageW, GetMessageW, GetWindowThreadProcessId, TranslateMessage, - EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_MINIMIZEEND, MSG, WINEVENT_OUTOFCONTEXT, - WINEVENT_SKIPOWNPROCESS, +use windows::Win32::{ + Foundation::{HINSTANCE, HWND}, + UI::{ + Accessibility::{SetWinEventHook, HWINEVENTHOOK}, + WindowsAndMessaging::{ + DispatchMessageW, GetMessageW, GetWindowThreadProcessId, TranslateMessage, + EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_MINIMIZEEND, MSG, WINEVENT_OUTOFCONTEXT, + WINEVENT_SKIPOWNPROCESS, + }, }, }; use crate::pid_to_exe::pid_to_exe_path; unsafe extern "system" fn win_event_proc( - _hook: *mut HWINEVENTHOOK__, + _hook: HWINEVENTHOOK, event: u32, - hwnd: *mut HWND__, + hwnd: HWND, _id_object: i32, _id_child: i32, _dw_event_thread: u32, @@ -28,21 +30,21 @@ unsafe extern "system" fn win_event_proc( } } -fn main() { +fn win_event_hook_loop() { unsafe { - winapi::um::winuser::SetWinEventHook( + SetWinEventHook( EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, - null_mut(), + HINSTANCE::default(), Some(win_event_proc), 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS, ); - winapi::um::winuser::SetWinEventHook( + SetWinEventHook( EVENT_SYSTEM_MINIMIZEEND, EVENT_SYSTEM_MINIMIZEEND, - null_mut(), + HINSTANCE::default(), Some(win_event_proc), 0, 0, @@ -50,9 +52,12 @@ fn main() { ); let mut msg: MSG = Default::default(); - while GetMessageW(&mut msg, null_mut(), 0, 0) > 0 { + while GetMessageW(&mut msg, HWND::default(), 0, 0).as_bool() { TranslateMessage(&msg); DispatchMessageW(&msg); } } } +fn main() { + win_event_hook_loop(); +} diff --git a/src/pid_to_exe.rs b/src/pid_to_exe.rs index 0cc7f3d..8a175fc 100644 --- a/src/pid_to_exe.rs +++ b/src/pid_to_exe.rs @@ -1,24 +1,24 @@ -use std::ptr::null_mut; +use std::error::Error; -use winapi::{ - shared::minwindef::{DWORD, FALSE, MAX_PATH}, - um::{ - errhandlingapi::GetLastError, - processthreadsapi::OpenProcess, - winbase::QueryFullProcessImageNameW, - winnt::{PROCESS_QUERY_INFORMATION, PROCESS_VM_READ}, +use windows::Win32::{ + Foundation::MAX_PATH, + System::Threading::{ + OpenProcess, QueryFullProcessImageNameW, PROCESS_QUERY_INFORMATION, PROCESS_VM_READ, }, }; -pub unsafe fn pid_to_exe_path(pid: u32) -> Result { - let mut exe_name = Vec::with_capacity(MAX_PATH); +pub unsafe fn pid_to_exe_path(pid: u32) -> Result> { + let mut exe_name: Vec = Vec::with_capacity(MAX_PATH as usize); let mut size: u32 = exe_name.capacity().try_into().unwrap(); - let process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); - if process == null_mut() { - return Err(GetLastError()); - } - if QueryFullProcessImageNameW(process, 0, exe_name.as_mut_ptr(), &mut size) == FALSE { - return Err(GetLastError()); + let process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pid); + if !QueryFullProcessImageNameW( + process?, + Default::default(), + windows::core::PWSTR(exe_name.as_mut_ptr()), + &mut size, + ).as_bool() + { + return Err(Box::new(windows::core::Error::from_win32())); } exe_name.set_len(size.try_into().unwrap()); let process_name = String::from_utf16_lossy(&exe_name);