Switch to windows-rs
This commit is contained in:
45
Cargo.lock
generated
45
Cargo.lock
generated
@@ -7,7 +7,7 @@ name = "auto_mute_tool"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"com",
|
"com",
|
||||||
"winapi",
|
"windows",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -77,23 +77,44 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee"
|
checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi"
|
name = "windows"
|
||||||
version = "0.3.9"
|
version = "0.37.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
checksum = "57b543186b344cc61c85b5aab0d2e3adf4e0f99bc076eff9aa5927bcc0b8a647"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"winapi-i686-pc-windows-gnu",
|
"windows_aarch64_msvc",
|
||||||
"winapi-x86_64-pc-windows-gnu",
|
"windows_i686_gnu",
|
||||||
|
"windows_i686_msvc",
|
||||||
|
"windows_x86_64_gnu",
|
||||||
|
"windows_x86_64_msvc",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi-i686-pc-windows-gnu"
|
name = "windows_aarch64_msvc"
|
||||||
version = "0.4.0"
|
version = "0.37.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
checksum = "2623277cb2d1c216ba3b578c0f3cf9cdebeddb6e66b1b218bb33596ea7769c3a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi-x86_64-pc-windows-gnu"
|
name = "windows_i686_gnu"
|
||||||
version = "0.4.0"
|
version = "0.37.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
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"
|
||||||
|
|||||||
16
Cargo.toml
16
Cargo.toml
@@ -6,5 +6,17 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
com = "0.6.0"
|
com = {version="0.6.0", features=["production"]}
|
||||||
winapi = {version = "0.3.9", features=["winuser", "processthreadsapi", "errhandlingapi", "winbase", "impl-default", "impl-debug"]}
|
|
||||||
|
[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",
|
||||||
|
]
|
||||||
35
src/main.rs
35
src/main.rs
@@ -1,21 +1,23 @@
|
|||||||
mod pid_to_exe;
|
mod pid_to_exe;
|
||||||
|
|
||||||
use std::ptr::null_mut;
|
use windows::Win32::{
|
||||||
use winapi::{
|
Foundation::{HINSTANCE, HWND},
|
||||||
shared::windef::{HWINEVENTHOOK__, HWND__},
|
UI::{
|
||||||
um::winuser::{
|
Accessibility::{SetWinEventHook, HWINEVENTHOOK},
|
||||||
DispatchMessageW, GetMessageW, GetWindowThreadProcessId, TranslateMessage,
|
WindowsAndMessaging::{
|
||||||
EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_MINIMIZEEND, MSG, WINEVENT_OUTOFCONTEXT,
|
DispatchMessageW, GetMessageW, GetWindowThreadProcessId, TranslateMessage,
|
||||||
WINEVENT_SKIPOWNPROCESS,
|
EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_MINIMIZEEND, MSG, WINEVENT_OUTOFCONTEXT,
|
||||||
|
WINEVENT_SKIPOWNPROCESS,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::pid_to_exe::pid_to_exe_path;
|
use crate::pid_to_exe::pid_to_exe_path;
|
||||||
|
|
||||||
unsafe extern "system" fn win_event_proc(
|
unsafe extern "system" fn win_event_proc(
|
||||||
_hook: *mut HWINEVENTHOOK__,
|
_hook: HWINEVENTHOOK,
|
||||||
event: u32,
|
event: u32,
|
||||||
hwnd: *mut HWND__,
|
hwnd: HWND,
|
||||||
_id_object: i32,
|
_id_object: i32,
|
||||||
_id_child: i32,
|
_id_child: i32,
|
||||||
_dw_event_thread: u32,
|
_dw_event_thread: u32,
|
||||||
@@ -28,21 +30,21 @@ unsafe extern "system" fn win_event_proc(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn win_event_hook_loop() {
|
||||||
unsafe {
|
unsafe {
|
||||||
winapi::um::winuser::SetWinEventHook(
|
SetWinEventHook(
|
||||||
EVENT_SYSTEM_FOREGROUND,
|
EVENT_SYSTEM_FOREGROUND,
|
||||||
EVENT_SYSTEM_FOREGROUND,
|
EVENT_SYSTEM_FOREGROUND,
|
||||||
null_mut(),
|
HINSTANCE::default(),
|
||||||
Some(win_event_proc),
|
Some(win_event_proc),
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS,
|
WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS,
|
||||||
);
|
);
|
||||||
winapi::um::winuser::SetWinEventHook(
|
SetWinEventHook(
|
||||||
EVENT_SYSTEM_MINIMIZEEND,
|
EVENT_SYSTEM_MINIMIZEEND,
|
||||||
EVENT_SYSTEM_MINIMIZEEND,
|
EVENT_SYSTEM_MINIMIZEEND,
|
||||||
null_mut(),
|
HINSTANCE::default(),
|
||||||
Some(win_event_proc),
|
Some(win_event_proc),
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
@@ -50,9 +52,12 @@ fn main() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let mut msg: MSG = Default::default();
|
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);
|
TranslateMessage(&msg);
|
||||||
DispatchMessageW(&msg);
|
DispatchMessageW(&msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fn main() {
|
||||||
|
win_event_hook_loop();
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,24 +1,24 @@
|
|||||||
use std::ptr::null_mut;
|
use std::error::Error;
|
||||||
|
|
||||||
use winapi::{
|
use windows::Win32::{
|
||||||
shared::minwindef::{DWORD, FALSE, MAX_PATH},
|
Foundation::MAX_PATH,
|
||||||
um::{
|
System::Threading::{
|
||||||
errhandlingapi::GetLastError,
|
OpenProcess, QueryFullProcessImageNameW, PROCESS_QUERY_INFORMATION, PROCESS_VM_READ,
|
||||||
processthreadsapi::OpenProcess,
|
|
||||||
winbase::QueryFullProcessImageNameW,
|
|
||||||
winnt::{PROCESS_QUERY_INFORMATION, PROCESS_VM_READ},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub unsafe fn pid_to_exe_path(pid: u32) -> Result<String, DWORD> {
|
pub unsafe fn pid_to_exe_path(pid: u32) -> Result<String, Box<dyn Error>> {
|
||||||
let mut exe_name = Vec::with_capacity(MAX_PATH);
|
let mut exe_name: Vec<u16> = Vec::with_capacity(MAX_PATH as usize);
|
||||||
let mut size: u32 = exe_name.capacity().try_into().unwrap();
|
let mut size: u32 = exe_name.capacity().try_into().unwrap();
|
||||||
let process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
|
let process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pid);
|
||||||
if process == null_mut() {
|
if !QueryFullProcessImageNameW(
|
||||||
return Err(GetLastError());
|
process?,
|
||||||
}
|
Default::default(),
|
||||||
if QueryFullProcessImageNameW(process, 0, exe_name.as_mut_ptr(), &mut size) == FALSE {
|
windows::core::PWSTR(exe_name.as_mut_ptr()),
|
||||||
return Err(GetLastError());
|
&mut size,
|
||||||
|
).as_bool()
|
||||||
|
{
|
||||||
|
return Err(Box::new(windows::core::Error::from_win32()));
|
||||||
}
|
}
|
||||||
exe_name.set_len(size.try_into().unwrap());
|
exe_name.set_len(size.try_into().unwrap());
|
||||||
let process_name = String::from_utf16_lossy(&exe_name);
|
let process_name = String::from_utf16_lossy(&exe_name);
|
||||||
|
|||||||
Reference in New Issue
Block a user