Skip to content

Commit

Permalink
feat: windows 支持获取window process id
Browse files Browse the repository at this point in the history
  • Loading branch information
nashaofu committed May 26, 2024
1 parent f5675c9 commit 917520e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ impl Window {
pub fn title(&self) -> &str {
&self.impl_window.title
}
/// 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 917520e

Please sign in to comment.