mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-11-24 04:12:32 +08:00
[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:
parent
00c86fef4f
commit
369a147a80
@ -38,18 +38,6 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto mutex = CreateMutex(nullptr, true, instanceMutexName.c_str());
|
|
||||||
if (mutex == nullptr)
|
|
||||||
{
|
|
||||||
Logger::error(L"Failed to create mutex. {}", get_last_error_or_default(GetLastError()));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (GetLastError() == ERROR_ALREADY_EXISTS)
|
|
||||||
{
|
|
||||||
Logger::warn(L"WorkspacesLauncher instance is already running");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::wstring cmdLineStr{ GetCommandLineW() };
|
std::wstring cmdLineStr{ GetCommandLineW() };
|
||||||
auto cmdArgs = split(cmdLineStr, L" ");
|
auto cmdArgs = split(cmdLineStr, L" ");
|
||||||
if (cmdArgs.workspaceId.empty())
|
if (cmdArgs.workspaceId.empty())
|
||||||
@ -59,6 +47,9 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
|
|||||||
return 1;
|
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())
|
if (is_process_elevated())
|
||||||
{
|
{
|
||||||
Logger::warn("Workspaces Launcher is elevated, restart");
|
Logger::warn("Workspaces Launcher is elevated, restart");
|
||||||
@ -72,11 +63,24 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
|
|||||||
std::string cmdLineStr(cmdline);
|
std::string cmdLineStr(cmdline);
|
||||||
std::wstring cmdLineWStr(cmdLineStr.begin(), cmdLineStr.end());
|
std::wstring cmdLineWStr(cmdLineStr.begin(), cmdLineStr.end());
|
||||||
|
|
||||||
std::wstring cmd = cmdArgs.workspaceId + L" " + std::to_wstring(cmdArgs.invokePoint);
|
std::wstring cmd = cmdArgs.workspaceId + L" " + std::to_wstring(cmdArgs.invokePoint) + L" " + NonLocalizable::restartedString;
|
||||||
|
|
||||||
RunNonElevatedEx(exe_path.get(), cmd, modulePath);
|
RunNonElevatedEx(exe_path.get(), cmd, modulePath);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto mutex = CreateMutex(nullptr, true, instanceMutexName.c_str());
|
||||||
|
if (mutex == nullptr)
|
||||||
|
{
|
||||||
|
Logger::error(L"Failed to create mutex. {}", get_last_error_or_default(GetLastError()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (GetLastError() == ERROR_ALREADY_EXISTS)
|
||||||
|
{
|
||||||
|
Logger::warn(L"WorkspacesLauncher instance is already running");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// COM should be initialized before ShellExecuteEx is called.
|
// COM should be initialized before ShellExecuteEx is called.
|
||||||
if (FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
|
if (FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
|
||||||
|
@ -6,15 +6,22 @@
|
|||||||
#include <workspaces-common/GuidUtils.h>
|
#include <workspaces-common/GuidUtils.h>
|
||||||
#include <workspaces-common/InvokePoint.h>
|
#include <workspaces-common/InvokePoint.h>
|
||||||
|
|
||||||
|
namespace NonLocalizable
|
||||||
|
{
|
||||||
|
const wchar_t restartedString[] = L"restarted";
|
||||||
|
}
|
||||||
|
|
||||||
struct CommandLineArgs
|
struct CommandLineArgs
|
||||||
{
|
{
|
||||||
std::wstring workspaceId;
|
std::wstring workspaceId;
|
||||||
InvokePoint invokePoint;
|
InvokePoint invokePoint;
|
||||||
|
bool isRestarted;
|
||||||
};
|
};
|
||||||
|
|
||||||
CommandLineArgs split(std::wstring s, const std::wstring& delimiter)
|
CommandLineArgs split(std::wstring s, const std::wstring& delimiter)
|
||||||
{
|
{
|
||||||
CommandLineArgs cmdArgs{};
|
CommandLineArgs cmdArgs{};
|
||||||
|
cmdArgs.isRestarted = false;
|
||||||
|
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
std::wstring token;
|
std::wstring token;
|
||||||
@ -29,7 +36,11 @@ CommandLineArgs split(std::wstring s, const std::wstring& delimiter)
|
|||||||
|
|
||||||
for (const auto& token : tokens)
|
for (const auto& token : tokens)
|
||||||
{
|
{
|
||||||
if (!cmdArgs.workspaceId.empty())
|
if (token == NonLocalizable::restartedString)
|
||||||
|
{
|
||||||
|
cmdArgs.isRestarted = true;
|
||||||
|
}
|
||||||
|
else if (!cmdArgs.workspaceId.empty())
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user