Skip to content
This repository was archived by the owner on Apr 7, 2025. It is now read-only.

Commit 04c7ba7

Browse files
committed
Used a GUID-named mutex to find whether UIforETW is running
UIforETW used to look for a window with the title UI for ETW to see if it was already running, but this can accidentally find the wrong window. Using a named mutex is more robust - safe against anything but a malicious local attacker. With this change UIforETW will reliably launch/not-launch based on whether it is already running. In not-launch case it may not reliably activate the correct window but that is a low-severity edge case. This fixes issue #147
1 parent c3ac3e7 commit 04c7ba7

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

UIforETW/UIforETW.cpp

+26-17
Original file line numberDiff line numberDiff line change
@@ -191,26 +191,35 @@ BOOL CUIforETWApp::InitInstance()
191191

192192
SetRegistryKey(L"RandomASCII");
193193

194-
HWND prevWindow = FindWindow(NULL, L"UI for ETW");
195-
if (prevWindow)
196-
{
197-
// Only allow one copy to be running at a time.
198-
SetForegroundWindow(prevWindow);
194+
constexpr wchar_t identifier[] = L"{B7D2F8B8-2F28-4366-9D7A-691019D89185}";
195+
HANDLE mutex = CreateMutexW(nullptr, FALSE, identifier);
196+
// Only allow one copy to be running at a time.
197+
if (mutex && GetLastError() == ERROR_ALREADY_EXISTS) {
198+
// Activate the previous window if possible. Note that if you have another
199+
// window with this title (an explorer window for a UI for ETW folder for
200+
// instance) then the wrong window may be activated. See
201+
// https://github.com/google/UIforETW/issues/147 for details.
202+
HWND prevWindow = FindWindow(NULL, L"UI for ETW");
203+
if (prevWindow)
204+
{
205+
SetForegroundWindow(prevWindow);
206+
}
207+
208+
// Already running.
209+
return FALSE;
199210
}
200-
else
211+
212+
CUIforETWDlg dlg;
213+
m_pMainWnd = &dlg;
214+
const INT_PTR nResponse = dlg.DoModal();
215+
if (nResponse == -1)
201216
{
202-
CUIforETWDlg dlg;
203-
m_pMainWnd = &dlg;
204-
const INT_PTR nResponse = dlg.DoModal();
205-
if (nResponse == -1)
206-
{
207-
ATLTRACE("Warning: dialog creation failed, "
208-
"so application is terminating unexpectedly.\r\n");
217+
ATLTRACE("Warning: dialog creation failed, "
218+
"so application is terminating unexpectedly.\r\n");
209219

210-
ATLTRACE("Warning: if you are using MFC controls on the dialog, "
211-
"you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\r\n");
212-
std::terminate( );
213-
}
220+
ATLTRACE("Warning: if you are using MFC controls on the dialog, "
221+
"you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\r\n");
222+
std::terminate( );
214223
}
215224

216225
// Since the dialog has been closed, return FALSE so that we exit the

0 commit comments

Comments
 (0)