Skip to content

Commit

Permalink
refactor getting process handle of window
Browse files Browse the repository at this point in the history
  • Loading branch information
eriforce committed Aug 27, 2024
1 parent 452ff29 commit ed5e418
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
10 changes: 1 addition & 9 deletions src/Magpie.App/ScalingService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,16 +330,8 @@ 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 失败");
return false;
}

Expand Down
13 changes: 9 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,15 @@ 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) {
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 Win32Utils::GetWndProcessHandle(HWND hWnd) noexcept;

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

static UINT GetWindowShowCmd(HWND hWnd) noexcept;
Expand Down

0 comments on commit ed5e418

Please sign in to comment.