mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-11-23 19:49:17 +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,6 +38,38 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
|
|||||||
return 0;
|
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());
|
auto mutex = CreateMutex(nullptr, true, instanceMutexName.c_str());
|
||||||
if (mutex == nullptr)
|
if (mutex == nullptr)
|
||||||
{
|
{
|
||||||
@ -50,34 +82,6 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
|
|||||||
return 0;
|
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.
|
// 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
|
||||||
{
|
{
|
||||||
@ -47,7 +58,7 @@ CommandLineArgs split(std::wstring s, const std::wstring& delimiter)
|
|||||||
{
|
{
|
||||||
cmdArgs.workspaceId = token;
|
cmdArgs.workspaceId = token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmdArgs;
|
return cmdArgs;
|
||||||
|
Loading…
Reference in New Issue
Block a user