|
| 1 | +#pragma once |
| 2 | +#include <CommonUtilities/win/Handle.h> |
| 3 | +#include <CommonUtilities/win/ProcessMapBuilder.h> |
| 4 | +#include <CommonUtilities/win/WinAPI.h> |
| 5 | +#include <array> |
| 6 | +#include <algorithm> |
| 7 | +#include <string> |
| 8 | +#include <string_view> |
| 9 | +#include <utility> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +namespace p2c::client::util |
| 13 | +{ |
| 14 | + constexpr const wchar_t* UiBrowserProcessMutexPrefix = L"Local\\IntelPresentMon."; |
| 15 | + constexpr const char* DefaultUiBrowserProcessMutexSuffix = "UiBrowserProcess"; |
| 16 | + constexpr const wchar_t* UiBrowserWindowClassName = L"BrowserWindowClass"; |
| 17 | + constexpr const wchar_t* UiBrowserWindowTitle = L"Intel PresentMon"; |
| 18 | + constexpr const wchar_t* UiBrowserWindowMutexSuffixProperty = L"IntelPresentMon.UiMutexSuffixAtom"; |
| 19 | + constexpr int UiAlreadyRunningExitCode = 2; |
| 20 | + |
| 21 | + inline std::wstring MakeUiBrowserProcessMutexName(std::string_view suffix) |
| 22 | + { |
| 23 | + std::wstring name{ UiBrowserProcessMutexPrefix }; |
| 24 | + if (suffix.empty()) { |
| 25 | + suffix = DefaultUiBrowserProcessMutexSuffix; |
| 26 | + } |
| 27 | + name.append(suffix.begin(), suffix.end()); |
| 28 | + return name; |
| 29 | + } |
| 30 | + |
| 31 | + inline bool IsUiBrowserProcessActive(std::string_view mutexSuffix) |
| 32 | + { |
| 33 | + const auto mutexName = MakeUiBrowserProcessMutexName(mutexSuffix); |
| 34 | + ::pmon::util::win::Handle hMutex{ OpenMutexW(SYNCHRONIZE, FALSE, mutexName.c_str()) }; |
| 35 | + return bool(hMutex); |
| 36 | + } |
| 37 | + |
| 38 | + inline std::pair<::pmon::util::win::Handle, bool> TryAcquireUiBrowserProcessMutex(std::string_view mutexSuffix) |
| 39 | + { |
| 40 | + const auto mutexName = MakeUiBrowserProcessMutexName(mutexSuffix); |
| 41 | + ::pmon::util::win::Handle hMutex{ CreateMutexW(nullptr, FALSE, mutexName.c_str()) }; |
| 42 | + if (!hMutex) { |
| 43 | + return { {}, false }; |
| 44 | + } |
| 45 | + return { std::move(hMutex), GetLastError() != ERROR_ALREADY_EXISTS }; |
| 46 | + } |
| 47 | + |
| 48 | + inline void SetUiBrowserWindowMutexSuffix(HWND hWnd, std::string_view mutexSuffix) |
| 49 | + { |
| 50 | + if (mutexSuffix.empty()) { |
| 51 | + mutexSuffix = DefaultUiBrowserProcessMutexSuffix; |
| 52 | + } |
| 53 | + const ATOM suffixAtom = GlobalAddAtomA(std::string{ mutexSuffix }.c_str()); |
| 54 | + if (suffixAtom != 0) { |
| 55 | + if (!SetPropW(hWnd, UiBrowserWindowMutexSuffixProperty, (HANDLE)(ULONG_PTR)suffixAtom)) { |
| 56 | + GlobalDeleteAtom(suffixAtom); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + inline void ClearUiBrowserWindowMutexSuffix(HWND hWnd) |
| 62 | + { |
| 63 | + if (const auto suffixAtom = (ATOM)(ULONG_PTR)RemovePropW(hWnd, UiBrowserWindowMutexSuffixProperty)) { |
| 64 | + GlobalDeleteAtom(suffixAtom); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + inline bool UiBrowserWindowMutexSuffixMatches(HWND hWnd, std::string_view mutexSuffix) |
| 69 | + { |
| 70 | + if (mutexSuffix.empty()) { |
| 71 | + mutexSuffix = DefaultUiBrowserProcessMutexSuffix; |
| 72 | + } |
| 73 | + const auto suffixAtom = (ATOM)(ULONG_PTR)GetPropW(hWnd, UiBrowserWindowMutexSuffixProperty); |
| 74 | + if (suffixAtom == 0) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + std::array<char, MAX_PATH> suffix{}; |
| 78 | + if (GlobalGetAtomNameA(suffixAtom, suffix.data(), (int)suffix.size()) == 0) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + return std::string_view{ suffix.data() } == mutexSuffix; |
| 82 | + } |
| 83 | + |
| 84 | + inline BOOL CALLBACK FindUiBrowserWindowCallback(HWND hWnd, LPARAM lParam) |
| 85 | + { |
| 86 | + auto& params = *reinterpret_cast<std::pair<std::string_view, HWND>*>(lParam); |
| 87 | + if (!IsWindowVisible(hWnd)) { |
| 88 | + return TRUE; |
| 89 | + } |
| 90 | + |
| 91 | + std::array<wchar_t, MAX_PATH> className{}; |
| 92 | + if (GetClassNameW(hWnd, className.data(), (int)className.size()) == 0 || |
| 93 | + std::wstring_view{ className.data() } != UiBrowserWindowClassName) { |
| 94 | + return TRUE; |
| 95 | + } |
| 96 | + |
| 97 | + std::array<wchar_t, MAX_PATH> title{}; |
| 98 | + if (GetWindowTextW(hWnd, title.data(), (int)title.size()) == 0 || |
| 99 | + std::wstring_view{ title.data() } != UiBrowserWindowTitle) { |
| 100 | + return TRUE; |
| 101 | + } |
| 102 | + |
| 103 | + if (!UiBrowserWindowMutexSuffixMatches(hWnd, params.first)) { |
| 104 | + return TRUE; |
| 105 | + } |
| 106 | + |
| 107 | + params.second = hWnd; |
| 108 | + return FALSE; |
| 109 | + } |
| 110 | + |
| 111 | + inline HWND FindUiBrowserWindow(std::string_view mutexSuffix) |
| 112 | + { |
| 113 | + std::pair<std::string_view, HWND> params{ mutexSuffix, nullptr }; |
| 114 | + EnumWindows(FindUiBrowserWindowCallback, reinterpret_cast<LPARAM>(¶ms)); |
| 115 | + return params.second; |
| 116 | + } |
| 117 | + |
| 118 | + inline DWORD FindUiBrowserWindowProcessId(std::string_view mutexSuffix) |
| 119 | + { |
| 120 | + const auto hWnd = FindUiBrowserWindow(mutexSuffix); |
| 121 | + if (hWnd == nullptr) { |
| 122 | + return 0; |
| 123 | + } |
| 124 | + DWORD processId = 0; |
| 125 | + GetWindowThreadProcessId(hWnd, &processId); |
| 126 | + return processId; |
| 127 | + } |
| 128 | + |
| 129 | + inline DWORD FindUiInstanceRootProcessId(std::string_view mutexSuffix) |
| 130 | + { |
| 131 | + const auto uiProcessId = FindUiBrowserWindowProcessId(mutexSuffix); |
| 132 | + if (uiProcessId == 0) { |
| 133 | + return 0; |
| 134 | + } |
| 135 | + |
| 136 | + const ::pmon::util::win::ProcessMapBuilder processMap; |
| 137 | + const auto& processes = processMap.Peek(); |
| 138 | + const auto uiIt = processes.find(uiProcessId); |
| 139 | + if (uiIt == processes.end()) { |
| 140 | + return uiProcessId; |
| 141 | + } |
| 142 | + |
| 143 | + const auto parentIt = processes.find(uiIt->second.parentId); |
| 144 | + if (parentIt == processes.end()) { |
| 145 | + return uiProcessId; |
| 146 | + } |
| 147 | + |
| 148 | + if (parentIt->second.name == L"PresentMon.exe") { |
| 149 | + return parentIt->second.pid; |
| 150 | + } |
| 151 | + return uiProcessId; |
| 152 | + } |
| 153 | + |
| 154 | + inline bool TerminateUiInstanceProcessTree(std::string_view mutexSuffix) |
| 155 | + { |
| 156 | + const auto rootProcessId = FindUiInstanceRootProcessId(mutexSuffix); |
| 157 | + if (rootProcessId == 0 || rootProcessId == GetCurrentProcessId()) { |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + const ::pmon::util::win::ProcessMapBuilder processMap; |
| 162 | + const auto& processes = processMap.Peek(); |
| 163 | + if (!processes.contains(rootProcessId)) { |
| 164 | + return false; |
| 165 | + } |
| 166 | + |
| 167 | + auto processIds = processMap.GetChildTreePostOrder(rootProcessId); |
| 168 | + std::erase(processIds, GetCurrentProcessId()); |
| 169 | + |
| 170 | + bool terminatedAny = false; |
| 171 | + std::vector<::pmon::util::win::Handle> handles; |
| 172 | + for (const auto processId : processIds) { |
| 173 | + ::pmon::util::win::Handle process{ OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, FALSE, processId) }; |
| 174 | + if (process) { |
| 175 | + terminatedAny = TerminateProcess(process.Get(), 1) != FALSE || terminatedAny; |
| 176 | + handles.push_back(std::move(process)); |
| 177 | + } |
| 178 | + } |
| 179 | + for (const auto& process : handles) { |
| 180 | + WaitForSingleObject(process.Get(), 5000); |
| 181 | + } |
| 182 | + return terminatedAny; |
| 183 | + } |
| 184 | + |
| 185 | + inline bool BringUiBrowserWindowToFront(std::string_view mutexSuffix) |
| 186 | + { |
| 187 | + const auto hWnd = FindUiBrowserWindow(mutexSuffix); |
| 188 | + if (hWnd == nullptr) { |
| 189 | + return false; |
| 190 | + } |
| 191 | + if (IsIconic(hWnd)) { |
| 192 | + ShowWindow(hWnd, SW_RESTORE); |
| 193 | + } |
| 194 | + else { |
| 195 | + ShowWindow(hWnd, SW_SHOW); |
| 196 | + } |
| 197 | + return SetForegroundWindow(hWnd) != FALSE; |
| 198 | + } |
| 199 | +} |
0 commit comments