Skip to content

Commit

Permalink
feat: windows 支持获取window process id (#135)
Browse files Browse the repository at this point in the history
* feat: windows 支持获取window process id

* chore: update version
  • Loading branch information
nashaofu authored May 26, 2024
1 parent f5675c9 commit d4b249c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
6 changes: 6 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
14 changes: 11 additions & 3 deletions src/windows/impl_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -192,10 +192,17 @@ fn get_module_basename(box_process_handle: BoxProcessHandle) -> XCapResult<Strin
}
}

fn get_app_name(hwnd: HWND) -> XCapResult<String> {
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<String> {
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) {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d4b249c

Please sign in to comment.