Skip to content

Commit

Permalink
Fix poor detection of a dupe instance
Browse files Browse the repository at this point in the history
Old FindWindow() function could mistake an Explorer folder 'UI for ETW'
for another instance of 'UI for ETW' (since window title is identical)
New custom function registers a unique window message that allows to
uniquely identify another instance of 'UI for ETW'
Bonus: restore the minimized window of 'UI for ETW'

Resolves: google#147
  • Loading branch information
eugenesvk committed Feb 8, 2021
1 parent 04c7ba7 commit eecede0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
39 changes: 31 additions & 8 deletions UIforETW/UIforETW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,25 @@ CUIforETWApp::CUIforETWApp() noexcept

CUIforETWApp theApp;

BOOL CALLBACK CUIforETWApp::findDupeWindow(HWND hWnd, LPARAM lParam)
{
DWORD_PTR result;
LRESULT msgRet = SendMessageTimeout(hWnd, uwmAreYouMe, 0, 0,
SMTO_BLOCK | // Don't process any other requests until the function returns
SMTO_ABORTIFHUNG, // No waiting if the thread appears to not respond
200, &result); // Timeout in ms
if(msgRet == 0) // failed to send message to this window, ignore it
{
return TRUE;
}
if(result == uwmAreYouMe) // found a dupe window
{
HWND * target = (HWND *)lParam;
*target = hWnd;
return FALSE; // stop window enumeration
}
return TRUE; // not a dupe window, continue window enumeration
}

// CUIforETWApp initialization

Expand All @@ -191,18 +210,22 @@ BOOL CUIforETWApp::InitInstance()

SetRegistryKey(L"RandomASCII");

constexpr wchar_t identifier[] = L"{B7D2F8B8-2F28-4366-9D7A-691019D89185}";
constexpr wchar_t identifier[] = L"Global\\{B7D2F8B8-2F28-4366-9D7A-691019D89185}";
HANDLE mutex = CreateMutexW(nullptr, FALSE, identifier);
// Only allow one copy to be running at a time.
if (mutex && GetLastError() == ERROR_ALREADY_EXISTS) {
// Activate the previous window if possible. Note that if you have another
// window with this title (an explorer window for a UI for ETW folder for
// instance) then the wrong window may be activated. See
// https://github.com/google/UIforETW/issues/147 for details.
HWND prevWindow = FindWindow(NULL, L"UI for ETW");
if (prevWindow)
if (mutex && GetLastError() == ERROR_ALREADY_EXISTS ||
GetLastError() == ERROR_ACCESS_DENIED) // created in another session
{
// Activate the previous window
HWND prevWindow = NULL;
EnumWindows(findDupeWindow,(LPARAM)&prevWindow);
if (prevWindow != NULL)
{
SetForegroundWindow(prevWindow);
if (IsIconic(prevWindow)) // restore minimized window
{
ShowWindow(prevWindow, SW_RESTORE);
}
}

// Already running.
Expand Down
2 changes: 2 additions & 0 deletions UIforETW/UIforETW.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ class CUIforETWApp : public CWinApp
virtual BOOL InitInstance() override;
afx_msg void OnHelp() noexcept;

static BOOL CALLBACK findDupeWindow(HWND hWnd, LPARAM lParam);
// Implementation

DECLARE_MESSAGE_MAP()
};

inline const UINT uwmAreYouMe = RegisterWindowMessage(L"678fd291e05741edb987c0a4f5650866");
extern CUIforETWApp theApp;
6 changes: 6 additions & 0 deletions UIforETW/UIforETWDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ BEGIN_MESSAGE_MAP(CUIforETWDlg, CDialog)
ON_WM_ACTIVATE()
ON_WM_TIMER()
ON_BN_CLICKED(IDC_CLRTRACING, &CUIforETWDlg::OnBnClickedClrtracing)
ON_REGISTERED_MESSAGE(uwmAreYouMe, &CUIforETWDlg::OnAreYouMe)
END_MESSAGE_MAP()


Expand Down Expand Up @@ -2538,3 +2539,8 @@ void CUIforETWDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
}
CDialog::OnActivate(nState, pWndOther, bMinimized);
}

LRESULT CUIforETWDlg::OnAreYouMe(WPARAM, LPARAM) noexcept
{
return uwmAreYouMe;
}
1 change: 1 addition & 0 deletions UIforETW/UIforETWDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ class CUIforETWDlg : public CDialog
bool bShutdownCompleted_ = false;

// Generated message map functions
afx_msg LRESULT OnAreYouMe(WPARAM, LPARAM) noexcept;
virtual BOOL OnInitDialog() override;
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
Expand Down

0 comments on commit eecede0

Please sign in to comment.