From 28de475e511e7552766a33e51dbffe866224a114 Mon Sep 17 00:00:00 2001 From: Valentin Radu Date: Fri, 10 Dec 2021 02:35:26 +0200 Subject: [PATCH] Improved Explorer restart --- ExplorerPatcher/GUI.c | 53 +++++++++++++++++++---------------- ep_setup/ep_setup.c | 64 ++++++++++++++++++++++++------------------- 2 files changed, 65 insertions(+), 52 deletions(-) diff --git a/ExplorerPatcher/GUI.c b/ExplorerPatcher/GUI.c index d5512a5..7254128 100644 --- a/ExplorerPatcher/GUI.c +++ b/ExplorerPatcher/GUI.c @@ -771,33 +771,38 @@ static BOOL GUI_Build(HDC hDC, HWND hwnd, POINT pt) if (p) *p = 0; if (!strncmp(line + 1, "restart", 7)) { - if (FindWindowW(L"Shell_TrayWnd", NULL)) + HWND hShellTrayWnd = FindWindowW(L"Shell_TrayWnd", NULL); + if (hShellTrayWnd) { - HANDLE hExplorerRestartThread = CreateThread(NULL, 0, BeginExplorerRestart, NULL, 0, NULL); - if (hExplorerRestartThread) - { - WaitForSingleObject(hExplorerRestartThread, 2000); - CloseHandle(hExplorerRestartThread); - hExplorerRestartThread = NULL; - } - else - { - BeginExplorerRestart(); - } - Sleep(100); - //ZZRestartExplorer(0, 0, 0, 0); WCHAR wszPath[MAX_PATH]; ZeroMemory(wszPath, MAX_PATH * sizeof(WCHAR)); - GetSystemDirectoryW(wszPath, MAX_PATH); - wcscat_s(wszPath, MAX_PATH, L"\\taskkill.exe"); - ShellExecuteW( - NULL, - L"open", - wszPath, - L"/f /im explorer.exe", - NULL, - SW_SHOWMINIMIZED - ); + INT64 res = -1; + if (!SendMessageTimeoutW(hShellTrayWnd, 1460, 0, 0, SMTO_ABORTIFHUNG, 2000, &res) && res) + { + HANDLE hExplorerRestartThread = CreateThread(NULL, 0, BeginExplorerRestart, NULL, 0, NULL); + if (hExplorerRestartThread) + { + WaitForSingleObject(hExplorerRestartThread, 2000); + CloseHandle(hExplorerRestartThread); + hExplorerRestartThread = NULL; + } + else + { + BeginExplorerRestart(); + } + Sleep(100); + //ZZRestartExplorer(0, 0, 0, 0); + GetSystemDirectoryW(wszPath, MAX_PATH); + wcscat_s(wszPath, MAX_PATH, L"\\taskkill.exe"); + ShellExecuteW( + NULL, + L"open", + wszPath, + L"/f /im explorer.exe", + NULL, + SW_SHOWMINIMIZED + ); + } GetWindowsDirectoryW(wszPath, MAX_PATH); wcscat_s(wszPath, MAX_PATH, L"\\explorer.exe"); Sleep(1000); diff --git a/ep_setup/ep_setup.c b/ep_setup/ep_setup.c index de393ac..f7cfb61 100644 --- a/ep_setup/ep_setup.c +++ b/ep_setup/ep_setup.c @@ -438,36 +438,44 @@ int WINAPI wWinMain( CoUninitialize(); } - HANDLE hExplorerRestartThread = CreateThread(NULL, 0, BeginExplorerRestart, NULL, 0, NULL); - if (hExplorerRestartThread) + HWND hShellTrayWnd = FindWindowW(L"Shell_TrayWnd", NULL); + if (hShellTrayWnd) { - WaitForSingleObject(hExplorerRestartThread, 2000); - CloseHandle(hExplorerRestartThread); - hExplorerRestartThread = NULL; - } - else - { - BeginExplorerRestart(); - } - Sleep(100); + INT res = -1; + if (!SendMessageTimeoutW(hShellTrayWnd, 1460, 0, 0, SMTO_ABORTIFHUNG, 2000, &res) && res) + { + HANDLE hExplorerRestartThread = CreateThread(NULL, 0, BeginExplorerRestart, NULL, 0, NULL); + if (hExplorerRestartThread) + { + WaitForSingleObject(hExplorerRestartThread, 2000); + CloseHandle(hExplorerRestartThread); + hExplorerRestartThread = NULL; + } + else + { + BeginExplorerRestart(); + } + Sleep(100); - GetSystemDirectoryW(wszPath, MAX_PATH); - wcscat_s(wszPath, MAX_PATH, L"\\taskkill.exe"); - SHELLEXECUTEINFOW sei; - ZeroMemory(&sei, sizeof(SHELLEXECUTEINFOW)); - sei.cbSize = sizeof(sei); - sei.fMask = SEE_MASK_NOCLOSEPROCESS; - sei.hwnd = NULL; - sei.hInstApp = NULL; - sei.lpVerb = NULL; - sei.lpFile = wszPath; - sei.lpParameters = L"/f /im explorer.exe"; - sei.hwnd = NULL; - sei.nShow = SW_SHOWMINIMIZED; - if (ShellExecuteExW(&sei) && sei.hProcess) - { - WaitForSingleObject(sei.hProcess, INFINITE); - CloseHandle(sei.hProcess); + GetSystemDirectoryW(wszPath, MAX_PATH); + wcscat_s(wszPath, MAX_PATH, L"\\taskkill.exe"); + SHELLEXECUTEINFOW sei; + ZeroMemory(&sei, sizeof(SHELLEXECUTEINFOW)); + sei.cbSize = sizeof(sei); + sei.fMask = SEE_MASK_NOCLOSEPROCESS; + sei.hwnd = NULL; + sei.hInstApp = NULL; + sei.lpVerb = NULL; + sei.lpFile = wszPath; + sei.lpParameters = L"/f /im explorer.exe"; + sei.hwnd = NULL; + sei.nShow = SW_SHOWMINIMIZED; + if (ShellExecuteExW(&sei) && sei.hProcess) + { + WaitForSingleObject(sei.hProcess, INFINITE); + CloseHandle(sei.hProcess); + } + } } HWND hWnd = FindWindowW(L"ExplorerPatcher_GUI_" _T(EP_CLSID), NULL);