-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 🔨 Move get pid function to the windows folder because it's …
…a general thing
1 parent
d1cd621
commit d2ce3fd
Showing
3 changed files
with
69 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
pub mod processes; | ||
pub mod users; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// SPDX-FileCopyrightText: 2023 The WAG development team | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
use std::{ | ||
error::Error, | ||
ffi::OsString, | ||
fmt::{Display, Formatter, Result as FormatterResult}, | ||
os::windows::ffi::OsStringExt, | ||
}; | ||
use windows::{ | ||
core::Owned, | ||
Win32::{ | ||
Foundation::HANDLE, | ||
System::Diagnostics::ToolHelp::{ | ||
CreateToolhelp32Snapshot, Process32FirstW, Process32NextW, PROCESSENTRY32W, | ||
TH32CS_SNAPPROCESS, | ||
}, | ||
}, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub struct ProcessNotFound; | ||
|
||
impl Error for ProcessNotFound {} | ||
|
||
impl Display for ProcessNotFound { | ||
fn fmt(&self, formatter: &mut Formatter) -> FormatterResult { | ||
write!(formatter, "ProcessNotFound") | ||
} | ||
} | ||
|
||
pub fn get_pid(name: &str) -> Result<u32, Box<dyn Error>> { | ||
let snapshot: Owned<HANDLE> = | ||
unsafe { Owned::new(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)?) }; | ||
let mut process_entry: PROCESSENTRY32W = PROCESSENTRY32W { | ||
dwSize: size_of::<PROCESSENTRY32W>() as u32, | ||
..Default::default() | ||
}; | ||
|
||
unsafe { | ||
Process32FirstW(*snapshot, &mut process_entry)?; | ||
} | ||
|
||
loop { | ||
if OsString::from_wide( | ||
process_entry | ||
.szExeFile | ||
.into_iter() | ||
.take_while(|&byte| byte != 0) | ||
.collect::<Vec<_>>() | ||
.as_slice(), | ||
) == name | ||
{ | ||
return Ok(process_entry.th32ProcessID); | ||
} | ||
|
||
if unsafe { Process32NextW(*snapshot, &mut process_entry) }.is_err() { | ||
break; | ||
} | ||
} | ||
|
||
Err(Box::new(ProcessNotFound)) | ||
} |