[CLEANMGR] Only allow one instance per drive (#7832)

CORE-18941
This commit is contained in:
Whindmar Saksit 2025-03-29 16:58:18 +01:00 committed by GitHub
parent 21b3382f31
commit 467dec4d16
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 2 deletions

View file

@ -25,6 +25,19 @@ public:
LRESULT OnInitDialog(UINT, WPARAM, LPARAM, BOOL&)
{
// Try to find an existing instance of this dialog
WCHAR buf[300];
GetWindowTextW(buf, _countof(buf));
for (HWND hNext = NULL, hFind; (hFind = ::FindWindowExW(NULL, hNext, NULL, buf)) != NULL; hNext = hFind)
{
if (hFind != *this && ::IsWindowVisible(hFind))
{
::SetForegroundWindow(hFind);
EndDialog(IDCANCEL);
return FALSE;
}
}
CWindow cbo = GetDlgItem(IDC_DRIVES);
WCHAR VolumeNameBuffer[MAX_PATH + 1];
CStringW Tmp;

View file

@ -247,6 +247,22 @@ public:
return true;
}
static inline UINT GetWindowProcessId(_In_ HWND hWnd)
{
DWORD pid;
return GetWindowThreadProcessId(hWnd, &pid) ? pid : 0;
}
static BOOL CALLBACK EnumSingleInstanceCallback(_In_ HWND hWnd, _In_ LPARAM lParam)
{
if (::IsWindowVisible(hWnd) && (LPARAM)GetWindowProcessId(hWnd) == lParam)
{
::SetForegroundWindow(hWnd);
return FALSE;
}
return TRUE;
}
HRESULT Run(_In_ int nShowCmd) throw()
{
if (m_Drive == UNICODE_NULL)
@ -257,6 +273,19 @@ public:
if (m_Drive == UNICODE_NULL)
return E_FAIL;
CStringW Title;
Title.Format(IDS_PROPERTIES_MAIN_TITLE, m_Drive);
HWND hWndInstance = ::CreateWindowExW(WS_EX_TOOLWINDOW, WC_STATIC, Title, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
for (HWND hNext = NULL, hFind; (hFind = ::FindWindowExW(NULL, hNext, WC_STATIC, Title)) != NULL; hNext = hFind)
{
if (hFind != hWndInstance)
{
::EnumWindows(EnumSingleInstanceCallback, GetWindowProcessId(hFind));
return S_FALSE;
}
}
CCleanupHandlerList Handlers;
CEmptyVolumeCacheCallBack CacheCallBack;
@ -271,16 +300,19 @@ public:
psh.dwFlags = PSH_NOAPPLYNOW | PSH_USEICONID | PSH_NOCONTEXTHELP;
psh.hInstance = _AtlBaseModule.GetResourceInstance();
psh.pszIcon = MAKEINTRESOURCEW(IDI_CLEANMGR);
CStringW Title;
Title.Format(IDS_PROPERTIES_MAIN_TITLE, m_Drive);
psh.pszCaption = Title;
psh.nPages = _countof(hpsp);
psh.phpage = hpsp;
if (PropertySheetW(&psh) >= 1)
{
::DestroyWindow(hWndInstance); // Allow new "cleanmgr /D" without waiting for these handlers
Handlers.ExecuteCleanup(&CacheCallBack);
}
else
{
::DestroyWindow(hWndInstance);
}
return S_OK;
}
};