From d4b249cd3133ade194a0be3af02419498cd5894a Mon Sep 17 00:00:00 2001 From: nashaofu Date: Sun, 26 May 2024 16:33:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20windows=20=E6=94=AF=E6=8C=81=E8=8E=B7?= =?UTF-8?q?=E5=8F=96window=20process=20id=20(#135)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: windows 支持获取window process id * chore: update version --- Cargo.toml | 2 +- src/window.rs | 6 ++++++ src/windows/impl_window.rs | 14 +++++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a9ea8e6..7573935 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xcap" -version = "0.0.9" +version = "0.0.10" edition = "2021" description = "XCap is a cross-platform screen capture library written in Rust. It supports Linux (X11, Wayland), MacOS, and Windows. XCap supports screenshot and video recording (to be implemented)." license = "Apache-2.0" diff --git a/src/window.rs b/src/window.rs index 94926ae..a41bff4 100644 --- a/src/window.rs +++ b/src/window.rs @@ -37,6 +37,12 @@ impl Window { pub fn title(&self) -> &str { &self.impl_window.title } + + #[cfg(target_os = "windows")] + /// The window process id + pub fn process_id(&self) -> u32 { + self.impl_window.process_id + } /// The window current monitor pub fn current_monitor(&self) -> Monitor { Monitor::new(self.impl_window.current_monitor.to_owned()) diff --git a/src/windows/impl_window.rs b/src/windows/impl_window.rs index ad967d3..653cee8 100644 --- a/src/windows/impl_window.rs +++ b/src/windows/impl_window.rs @@ -41,6 +41,7 @@ pub(crate) struct ImplWindow { pub id: u32, pub title: String, pub app_name: String, + pub process_id: u32, pub current_monitor: ImplMonitor, pub x: i32, pub y: i32, @@ -119,8 +120,7 @@ fn is_valid_window(hwnd: HWND) -> bool { // windows owned by the current process. Consumers should either ensure that // the thread running their message loop never waits on this operation, or use // the option to exclude these windows from the source list. - let mut lp_dw_process_id = 0; - GetWindowThreadProcessId(hwnd, Some(&mut lp_dw_process_id)); + let lp_dw_process_id = get_process_id(hwnd); if lp_dw_process_id == GetCurrentProcessId() { return false; } @@ -192,10 +192,17 @@ fn get_module_basename(box_process_handle: BoxProcessHandle) -> XCapResult XCapResult { +fn get_process_id(hwnd: HWND) -> u32 { unsafe { let mut lp_dw_process_id = 0; GetWindowThreadProcessId(hwnd, Some(&mut lp_dw_process_id)); + lp_dw_process_id + } +} + +fn get_app_name(hwnd: HWND) -> XCapResult { + unsafe { + let lp_dw_process_id = get_process_id(hwnd); let box_process_handle = match BoxProcessHandle::open(PROCESS_ALL_ACCESS, false, lp_dw_process_id) { @@ -311,6 +318,7 @@ impl ImplWindow { id: hwnd.0 as u32, title, app_name, + process_id: get_process_id(hwnd), current_monitor: ImplMonitor::new(hmonitor)?, x: rc_window.left, y: rc_window.top,