Skip to content

Commit

Permalink
fix(wm): manually close process handles
Browse files Browse the repository at this point in the history
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
  • Loading branch information
LGUG2Z committed Jun 12, 2023
1 parent b0737ff commit 6df91d7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
10 changes: 10 additions & 0 deletions komorebi.generated.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ RunWait('komorebic.exe identify-tray-application class "CreativeCloudDesktopWind
; Adobe Photoshop
RunWait('komorebic.exe identify-border-overflow-application class "Photoshop"', , "Hide")

; Affinity Photo 2
RunWait('komorebic.exe manage-rule title "Affinity Photo 2"', , "Hide")
RunWait('komorebic.exe float-rule exe "Photo.exe"', , "Hide")

; Akiflow
; If you have disabled minimize/close to tray for this application, you can delete/comment out the next line
RunWait('komorebic.exe identify-tray-application exe "Akiflow.exe"', , "Hide")
Expand Down Expand Up @@ -57,6 +61,10 @@ RunWait('komorebic.exe identify-border-overflow-application exe "Cron.exe"', , "
; If you have disabled minimize/close to tray for this application, you can delete/comment out the next line
RunWait('komorebic.exe identify-tray-application exe "Cron.exe"', , "Hide")

; DS4Windows
; If you have disabled minimize/close to tray for this application, you can delete/comment out the next line
RunWait('komorebic.exe identify-tray-application exe "DS4Windows.exe"', , "Hide")

; Delphi applications
; Target hidden window spawned by Delphi applications
RunWait('komorebic.exe float-rule class "TApplication"', , "Hide")
Expand Down Expand Up @@ -388,6 +396,8 @@ RunWait('komorebic.exe identify-border-overflow-application class "vguiPopupWind
RunWait('komorebic.exe identify-border-overflow-application class "SDL_app"', , "Hide")
; If you have disabled minimize/close to tray for this application, you can delete/comment out the next line
RunWait('komorebic.exe identify-tray-application class "SDL_app"', , "Hide")
; Target notification toast popups
RunWait('komorebic.exe float-rule title "notificationtoasts_"', , "Hide")

; Stremio
; If you have disabled minimize/close to tray for this application, you can delete/comment out the next line
Expand Down
10 changes: 10 additions & 0 deletions komorebi.generated.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ komorebic.exe identify-tray-application class "CreativeCloudDesktopWindowClass"
# Adobe Photoshop
komorebic.exe identify-border-overflow-application class "Photoshop"

# Affinity Photo 2
komorebic.exe manage-rule title "Affinity Photo 2"
komorebic.exe float-rule exe "Photo.exe"

# Akiflow
# If you have disabled minimize/close to tray for this application, you can delete/comment out the next line
komorebic.exe identify-tray-application exe "Akiflow.exe"
Expand Down Expand Up @@ -57,6 +61,10 @@ komorebic.exe identify-border-overflow-application exe "Cron.exe"
# If you have disabled minimize/close to tray for this application, you can delete/comment out the next line
komorebic.exe identify-tray-application exe "Cron.exe"

# DS4Windows
# If you have disabled minimize/close to tray for this application, you can delete/comment out the next line
komorebic.exe identify-tray-application exe "DS4Windows.exe"

# Delphi applications
# Target hidden window spawned by Delphi applications
komorebic.exe float-rule class "TApplication"
Expand Down Expand Up @@ -388,6 +396,8 @@ komorebic.exe identify-border-overflow-application class "vguiPopupWindow"
komorebic.exe identify-border-overflow-application class "SDL_app"
# If you have disabled minimize/close to tray for this application, you can delete/comment out the next line
komorebic.exe identify-tray-application class "SDL_app"
# Target notification toast popups
komorebic.exe float-rule title "notificationtoasts_"

# Stremio
# If you have disabled minimize/close to tray for this application, you can delete/comment out the next line
Expand Down
5 changes: 4 additions & 1 deletion komorebi/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
5 changes: 5 additions & 0 deletions komorebi/src/windows_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 6df91d7

Please sign in to comment.