Some quick and dirty code to reimplement window
switch detection and process name lookup
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
99
Cargo.lock
generated
Normal file
99
Cargo.lock
generated
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "auto_mute_tool"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"com",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "com"
|
||||||
|
version = "0.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7e17887fd17353b65b1b2ef1c526c83e26cd72e74f598a8dc1bee13a48f3d9f6"
|
||||||
|
dependencies = [
|
||||||
|
"com_macros",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "com_macros"
|
||||||
|
version = "0.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d375883580a668c7481ea6631fc1a8863e33cc335bf56bfad8d7e6d4b04b13a5"
|
||||||
|
dependencies = [
|
||||||
|
"com_macros_support",
|
||||||
|
"proc-macro2",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "com_macros_support"
|
||||||
|
version = "0.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ad899a1087a9296d5644792d7cb72b8e34c1bec8e7d4fbc002230169a6e8710c"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.39"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "1.0.96"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0748dd251e24453cb8717f0354206b91557e4ec8703673a4b30208f2abaf1ebf"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi"
|
||||||
|
version = "0.3.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-i686-pc-windows-gnu",
|
||||||
|
"winapi-x86_64-pc-windows-gnu",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-i686-pc-windows-gnu"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||||
10
Cargo.toml
Normal file
10
Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "auto_mute_tool"
|
||||||
|
version = "0.1.0"
|
||||||
|
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"]}
|
||||||
58
src/main.rs
Normal file
58
src/main.rs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
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 crate::pid_to_exe::pid_to_exe_path;
|
||||||
|
|
||||||
|
unsafe extern "system" fn win_event_proc(
|
||||||
|
_hook: *mut HWINEVENTHOOK__,
|
||||||
|
event: u32,
|
||||||
|
hwnd: *mut HWND__,
|
||||||
|
_id_object: i32,
|
||||||
|
_id_child: i32,
|
||||||
|
_dw_event_thread: u32,
|
||||||
|
_dwms_event_time: u32,
|
||||||
|
) {
|
||||||
|
if event == EVENT_SYSTEM_FOREGROUND || event == EVENT_SYSTEM_MINIMIZEEND {
|
||||||
|
let mut pid: u32 = 0;
|
||||||
|
GetWindowThreadProcessId(hwnd, &mut pid);
|
||||||
|
println!("{:?}", pid_to_exe_path(pid));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
unsafe {
|
||||||
|
winapi::um::winuser::SetWinEventHook(
|
||||||
|
EVENT_SYSTEM_FOREGROUND,
|
||||||
|
EVENT_SYSTEM_FOREGROUND,
|
||||||
|
null_mut(),
|
||||||
|
Some(win_event_proc),
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS,
|
||||||
|
);
|
||||||
|
winapi::um::winuser::SetWinEventHook(
|
||||||
|
EVENT_SYSTEM_MINIMIZEEND,
|
||||||
|
EVENT_SYSTEM_MINIMIZEEND,
|
||||||
|
null_mut(),
|
||||||
|
Some(win_event_proc),
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS,
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut msg: MSG = Default::default();
|
||||||
|
while GetMessageW(&mut msg, null_mut(), 0, 0) > 0 {
|
||||||
|
TranslateMessage(&msg);
|
||||||
|
DispatchMessageW(&msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
26
src/pid_to_exe.rs
Normal file
26
src/pid_to_exe.rs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
use std::ptr::null_mut;
|
||||||
|
|
||||||
|
use winapi::{
|
||||||
|
shared::minwindef::{DWORD, FALSE, MAX_PATH},
|
||||||
|
um::{
|
||||||
|
errhandlingapi::GetLastError,
|
||||||
|
processthreadsapi::OpenProcess,
|
||||||
|
winbase::QueryFullProcessImageNameW,
|
||||||
|
winnt::{PROCESS_QUERY_INFORMATION, PROCESS_VM_READ},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub unsafe fn pid_to_exe_path(pid: u32) -> Result<String, DWORD> {
|
||||||
|
let mut exe_name = Vec::with_capacity(MAX_PATH);
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
exe_name.set_len(size.try_into().unwrap());
|
||||||
|
let process_name = String::from_utf16_lossy(&exe_name);
|
||||||
|
return Ok(process_name);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user