fix(wm): manually close process handles

ZiN on Discord noted that handles returned by OpenProcess must be closed
manually (this is not implemented in the Drop trait for handle, it
seems)

https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocess#remarks
This commit is contained in:
LGUG2Z
2023-06-12 16:46:24 -07:00
parent b0737ff603
commit 6df91d7d40
4 changed files with 29 additions and 1 deletions
+4 -1
View File
@@ -383,7 +383,10 @@ impl Window {
pub fn exe(self) -> Result<String> {
let (process_id, _) = WindowsApi::window_thread_process_id(self.hwnd());
WindowsApi::exe(WindowsApi::process_handle(process_id)?)
let handle = WindowsApi::process_handle(process_id)?;
let exe = WindowsApi::exe(handle);
WindowsApi::close_process(handle)?;
exe
}
pub fn class(self) -> Result<String> {
+5
View File
@@ -11,6 +11,7 @@ use color_eyre::Result;
use windows::core::Result as WindowsCrateResult;
use windows::core::PCSTR;
use windows::core::PWSTR;
use windows::Win32::Foundation::CloseHandle;
use windows::Win32::Foundation::BOOL;
use windows::Win32::Foundation::COLORREF;
use windows::Win32::Foundation::HANDLE;
@@ -560,6 +561,10 @@ impl WindowsApi {
unsafe { OpenProcess(access_rights, inherit_handle, process_id) }.process()
}
pub fn close_process(handle: HANDLE) -> Result<()> {
unsafe { CloseHandle(handle) }.ok().process()
}
pub fn process_handle(process_id: u32) -> Result<HANDLE> {
Self::open_process(PROCESS_QUERY_INFORMATION, false, process_id)
}