diff --git a/src/modules/Workspaces/WorkspacesLauncher/main.cpp b/src/modules/Workspaces/WorkspacesLauncher/main.cpp index 09f63785b1..d23a5b05a1 100644 --- a/src/modules/Workspaces/WorkspacesLauncher/main.cpp +++ b/src/modules/Workspaces/WorkspacesLauncher/main.cpp @@ -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(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(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))) { diff --git a/src/modules/Workspaces/WorkspacesLib/utils.h b/src/modules/Workspaces/WorkspacesLib/utils.h index 5aebd197b3..971900d8ec 100644 --- a/src/modules/Workspaces/WorkspacesLib/utils.h +++ b/src/modules/Workspaces/WorkspacesLib/utils.h @@ -6,15 +6,22 @@ #include #include +namespace NonLocalizable +{ + const wchar_t restartedString[] = L"restarted"; +} + struct CommandLineArgs { std::wstring workspaceId; InvokePoint invokePoint; + bool isRestarted; }; CommandLineArgs split(std::wstring s, const std::wstring& delimiter) { CommandLineArgs cmdArgs{}; + cmdArgs.isRestarted = false; size_t pos = 0; std::wstring token; @@ -29,7 +36,11 @@ CommandLineArgs split(std::wstring s, const std::wstring& delimiter) for (const auto& token : tokens) { - if (!cmdArgs.workspaceId.empty()) + if (token == NonLocalizable::restartedString) + { + cmdArgs.isRestarted = true; + } + else if (!cmdArgs.workspaceId.empty()) { try { @@ -47,7 +58,7 @@ CommandLineArgs split(std::wstring s, const std::wstring& delimiter) { cmdArgs.workspaceId = token; } - } + } } return cmdArgs;