diff --git a/ExplorerPatcher/utility.h b/ExplorerPatcher/utility.h index 1b6c7e9..7d1e286 100644 --- a/ExplorerPatcher/utility.h +++ b/ExplorerPatcher/utility.h @@ -410,13 +410,45 @@ inline BOOL ExitExplorer() return PostMessageW(hWndTray, 0x5B4, 0, 0); } -inline void StartExplorerWithDelay(int delay) +inline void StartExplorerWithDelay(int delay, HANDLE userToken) { WCHAR wszPath[MAX_PATH]; ZeroMemory(wszPath, MAX_PATH * sizeof(WCHAR)); GetWindowsDirectoryW(wszPath, MAX_PATH); wcscat_s(wszPath, MAX_PATH, L"\\explorer.exe"); Sleep(delay); + if (userToken != INVALID_HANDLE_VALUE) + { + HANDLE primaryUserToken = INVALID_HANDLE_VALUE; + if (ImpersonateLoggedOnUser(userToken)) + { + DuplicateTokenEx(userToken, MAXIMUM_ALLOWED, NULL, SecurityImpersonation, TokenPrimary, &primaryUserToken); + RevertToSelf(); + } + if (primaryUserToken != INVALID_HANDLE_VALUE) + { + PROCESS_INFORMATION processInfo; + ZeroMemory(&processInfo, sizeof(processInfo)); + STARTUPINFOW startupInfo; + ZeroMemory(&startupInfo, sizeof(startupInfo)); + startupInfo.cb = sizeof(startupInfo); + BOOL processCreated = CreateProcessWithTokenW( + primaryUserToken, LOGON_WITH_PROFILE, wszPath, NULL, 0, NULL, NULL, &startupInfo, &processInfo) != 0; + CloseHandle(primaryUserToken); + if (processInfo.hProcess != INVALID_HANDLE_VALUE) + { + CloseHandle(processInfo.hProcess); + } + if (processInfo.hThread != INVALID_HANDLE_VALUE) + { + CloseHandle(processInfo.hThread); + } + if (processCreated) + { + return; + } + } + } ShellExecuteW( NULL, L"open", diff --git a/ep_setup/ep_setup.c b/ep_setup/ep_setup.c index 8728ecf..5428470 100644 --- a/ep_setup/ep_setup.c +++ b/ep_setup/ep_setup.c @@ -605,10 +605,62 @@ int WINAPI wWinMain( if (bOk || (!bOk && GetLastError() == ERROR_ALREADY_EXISTS)) { bOk = TRUE; + HANDLE userToken = INVALID_HANDLE_VALUE; HWND hShellTrayWnd = FindWindowW(L"Shell_TrayWnd", NULL); if (hShellTrayWnd) { + DWORD explorerProcessId = 0; + GetWindowThreadProcessId(hShellTrayWnd, &explorerProcessId); + if (explorerProcessId != 0) + { + HANDLE explorerProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, explorerProcessId); + if (explorerProcess != NULL) + { + OpenProcessToken(explorerProcess, TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY, &userToken); + CloseHandle(explorerProcess); + } + if (userToken) + { + HANDLE myToken = INVALID_HANDLE_VALUE; + OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY, &myToken); + if (myToken != INVALID_HANDLE_VALUE) + { + DWORD cbSizeNeeded = 0; + SetLastError(0); + if (!GetTokenInformation(userToken, TokenUser, NULL, 0, &cbSizeNeeded) && GetLastError() == ERROR_INSUFFICIENT_BUFFER) + { + TOKEN_USER* userTokenInfo = malloc(cbSizeNeeded); + if (userTokenInfo) + { + if (GetTokenInformation(userToken, TokenUser, userTokenInfo, cbSizeNeeded, &cbSizeNeeded)) + { + cbSizeNeeded = 0; + SetLastError(0); + if (!GetTokenInformation(myToken, TokenUser, NULL, 0, &cbSizeNeeded) && GetLastError() == ERROR_INSUFFICIENT_BUFFER) + { + TOKEN_USER* myTokenInfo = malloc(cbSizeNeeded); + if (myTokenInfo) + { + if (GetTokenInformation(myToken, TokenUser, myTokenInfo, cbSizeNeeded, &cbSizeNeeded)) + { + if (EqualSid(userTokenInfo->User.Sid, myTokenInfo->User.Sid)) + { + CloseHandle(userToken); + userToken = INVALID_HANDLE_VALUE; + } + } + free(myTokenInfo); + } + } + } + free(userTokenInfo); + } + } + CloseHandle(myToken); + } + } + } PDWORD_PTR res = -1; if (!SendMessageTimeoutW(hShellTrayWnd, 1460, 0, 0, SMTO_ABORTIFHUNG, 2000, &res) && res) { @@ -1187,7 +1239,8 @@ int WINAPI wWinMain( exit(0); } - StartExplorerWithDelay(1000); + StartExplorerWithDelay(1000, userToken); + if (userToken != INVALID_HANDLE_VALUE) CloseHandle(userToken); } return GetLastError();