Skip to content

Commit

Permalink
重构获取窗口进程句柄 (Blinue#987)
Browse files Browse the repository at this point in the history
* refactor getting process handle of window

* log errors when failed to get process handle
  • Loading branch information
eriforce committed Sep 8, 2024
1 parent ef24841 commit 68a7ae2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
11 changes: 2 additions & 9 deletions src/Magpie.App/ScalingService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,16 +342,9 @@ void ScalingService::_ScaleForegroundWindow() {
}

static bool GetWindowIntegrityLevel(HWND hWnd, DWORD& integrityLevel) noexcept {
DWORD processId;
if (!GetWindowThreadProcessId(hWnd, &processId)) {
Logger::Get().Win32Error("GetWindowThreadProcessId 失败");
return false;
}

wil::unique_process_handle hProc(
OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processId));
wil::unique_process_handle hProc = Win32Utils::GetWndProcessHandle(hWnd);
if (!hProc) {
Logger::Get().Win32Error("OpenProcess 失败");
Logger::Get().Error("GetWndProcessHandle 失败");
return false;
}

Expand Down
14 changes: 10 additions & 4 deletions src/Shared/Win32Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ std::wstring Win32Utils::GetWndTitle(HWND hWnd) noexcept {
return title;
}

std::wstring Win32Utils::GetPathOfWnd(HWND hWnd) noexcept {
wil::unique_process_handle Win32Utils::GetWndProcessHandle(HWND hWnd) noexcept {
wil::unique_process_handle hProc;

if (DWORD dwProcId = 0; GetWindowThreadProcessId(hWnd, &dwProcId)) {
Expand All @@ -58,10 +58,16 @@ std::wstring Win32Utils::GetPathOfWnd(HWND hWnd) noexcept {
Logger::Get().Win32Error("GetProcessHandleFromHwnd 失败");
}
}
}

if (!hProc) {
return {};
}
return hProc;
}

std::wstring Win32Utils::GetPathOfWnd(HWND hWnd) noexcept {
wil::unique_process_handle hProc = GetWndProcessHandle(hWnd);
if (!hProc) {
Logger::Get().Error("GetWndProcessHandle 失败");
return {};
}

std::wstring fileName;
Expand Down
2 changes: 2 additions & 0 deletions src/Shared/Win32Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ struct Win32Utils {

static std::wstring GetWndTitle(HWND hWnd) noexcept;

static wil::unique_process_handle GetWndProcessHandle(HWND hWnd) noexcept;

static std::wstring GetPathOfWnd(HWND hWnd) noexcept;

static UINT GetWindowShowCmd(HWND hWnd) noexcept;
Expand Down

0 comments on commit 68a7ae2

Please sign in to comment.