[Workspaces]Fix launcher restart for admin users (#35652)

* [Workspaces] fix endless restarting of the launcher

* Fixing restart, creating mutex after restart.
This commit is contained in:
Laszlo Nemeth
2024-10-30 15:52:15 +01:00
committed by GitHub
parent 00c86fef4f
commit 369a147a80
2 changed files with 45 additions and 30 deletions

View File

@@ -38,6 +38,38 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
return 0;
}
std::wstring cmdLineStr{ GetCommandLineW() };
auto cmdArgs = split(cmdLineStr, L" ");
if (cmdArgs.workspaceId.empty())
{
Logger::warn("Incorrect command line arguments: no workspace id");
MessageBox(NULL, GET_RESOURCE_STRING(IDS_INCORRECT_ARGS).c_str(), GET_RESOURCE_STRING(IDS_WORKSPACES).c_str(), MB_ICONERROR | MB_OK);
return 1;
}
if (!cmdArgs.isRestarted)
{
// check if restart is needed. Only check it if not yet restarted to avoid endless restarting. Restart is needed if the process is elevated.
if (is_process_elevated())
{
Logger::warn("Workspaces Launcher is elevated, restart");
constexpr DWORD exe_path_size = 0xFFFF;
auto exe_path = std::make_unique<wchar_t[]>(exe_path_size);
GetModuleFileNameW(nullptr, exe_path.get(), exe_path_size);
const auto modulePath = get_module_folderpath();
std::string cmdLineStr(cmdline);
std::wstring cmdLineWStr(cmdLineStr.begin(), cmdLineStr.end());
std::wstring cmd = cmdArgs.workspaceId + L" " + std::to_wstring(cmdArgs.invokePoint) + L" " + NonLocalizable::restartedString;
RunNonElevatedEx(exe_path.get(), cmd, modulePath);
return 1;
}
}
auto mutex = CreateMutex(nullptr, true, instanceMutexName.c_str());
if (mutex == nullptr)
{
@@ -50,34 +82,6 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
return 0;
}
std::wstring cmdLineStr{ GetCommandLineW() };
auto cmdArgs = split(cmdLineStr, L" ");
if (cmdArgs.workspaceId.empty())
{
Logger::warn("Incorrect command line arguments: no workspace id");
MessageBox(NULL, GET_RESOURCE_STRING(IDS_INCORRECT_ARGS).c_str(), GET_RESOURCE_STRING(IDS_WORKSPACES).c_str(), MB_ICONERROR | MB_OK);
return 1;
}
if (is_process_elevated())
{
Logger::warn("Workspaces Launcher is elevated, restart");
constexpr DWORD exe_path_size = 0xFFFF;
auto exe_path = std::make_unique<wchar_t[]>(exe_path_size);
GetModuleFileNameW(nullptr, exe_path.get(), exe_path_size);
const auto modulePath = get_module_folderpath();
std::string cmdLineStr(cmdline);
std::wstring cmdLineWStr(cmdLineStr.begin(), cmdLineStr.end());
std::wstring cmd = cmdArgs.workspaceId + L" " + std::to_wstring(cmdArgs.invokePoint);
RunNonElevatedEx(exe_path.get(), cmd, modulePath);
return 1;
}
// COM should be initialized before ShellExecuteEx is called.
if (FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
{