mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
[Workspaces] Fix restart launcher when elevated (#35064)
This commit is contained in:
@@ -1,12 +1,5 @@
|
|||||||
#include "pch.h"
|
#include "pch.h"
|
||||||
|
|
||||||
#include <WorkspacesLib/JsonUtils.h>
|
|
||||||
#include <WorkspacesLib/utils.h>
|
|
||||||
|
|
||||||
#include <Launcher.h>
|
|
||||||
|
|
||||||
#include <Generated Files/resource.h>
|
|
||||||
|
|
||||||
#include <common/utils/elevation.h>
|
#include <common/utils/elevation.h>
|
||||||
#include <common/utils/gpo.h>
|
#include <common/utils/gpo.h>
|
||||||
#include <common/utils/logger_helper.h>
|
#include <common/utils/logger_helper.h>
|
||||||
@@ -14,6 +7,13 @@
|
|||||||
#include <common/utils/UnhandledExceptionHandler.h>
|
#include <common/utils/UnhandledExceptionHandler.h>
|
||||||
#include <common/utils/resources.h>
|
#include <common/utils/resources.h>
|
||||||
|
|
||||||
|
#include <WorkspacesLib/JsonUtils.h>
|
||||||
|
#include <WorkspacesLib/utils.h>
|
||||||
|
|
||||||
|
#include <Launcher.h>
|
||||||
|
|
||||||
|
#include <Generated Files/resource.h>
|
||||||
|
|
||||||
const std::wstring moduleName = L"Workspaces\\WorkspacesLauncher";
|
const std::wstring moduleName = L"Workspaces\\WorkspacesLauncher";
|
||||||
const std::wstring internalPath = L"";
|
const std::wstring internalPath = L"";
|
||||||
|
|
||||||
@@ -28,6 +28,15 @@ 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())
|
if (is_process_elevated())
|
||||||
{
|
{
|
||||||
Logger::warn("Workspaces Launcher is elevated, restart");
|
Logger::warn("Workspaces Launcher is elevated, restart");
|
||||||
@@ -41,7 +50,9 @@ 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());
|
||||||
|
|
||||||
run_non_elevated(exe_path.get(), cmdLineWStr, nullptr, modulePath.c_str());
|
std::wstring cmd = cmdArgs.workspaceId + L" " + std::to_wstring(cmdArgs.invokePoint);
|
||||||
|
|
||||||
|
RunNonElevatedEx(exe_path.get(), cmd, modulePath);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,50 +65,21 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
|
|||||||
|
|
||||||
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
|
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
|
||||||
|
|
||||||
std::wstring cmdLineStr{ GetCommandLineW() };
|
Logger::trace(L"Invoke point: {}", cmdArgs.invokePoint);
|
||||||
auto cmdArgs = split(cmdLineStr, L" ");
|
|
||||||
if (cmdArgs.size() < 2)
|
|
||||||
{
|
|
||||||
Logger::warn("Incorrect command line arguments");
|
|
||||||
MessageBox(NULL, GET_RESOURCE_STRING(IDS_INCORRECT_ARGS).c_str(), GET_RESOURCE_STRING(IDS_WORKSPACES).c_str(), MB_ICONERROR | MB_OK);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::wstring id(cmdArgs[1].begin(), cmdArgs[1].end());
|
|
||||||
if (id.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;
|
|
||||||
}
|
|
||||||
|
|
||||||
InvokePoint invokePoint = InvokePoint::EditorButton;
|
|
||||||
if (cmdArgs.size() > 2)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
invokePoint = static_cast<InvokePoint>(std::stoi(cmdArgs[2]));
|
|
||||||
}
|
|
||||||
catch (std::exception)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger::trace(L"Invoke point: {}", invokePoint);
|
|
||||||
|
|
||||||
// read workspaces
|
// read workspaces
|
||||||
std::vector<WorkspacesData::WorkspacesProject> workspaces;
|
std::vector<WorkspacesData::WorkspacesProject> workspaces;
|
||||||
WorkspacesData::WorkspacesProject projectToLaunch{};
|
WorkspacesData::WorkspacesProject projectToLaunch{};
|
||||||
if (invokePoint == InvokePoint::LaunchAndEdit)
|
if (cmdArgs.invokePoint == InvokePoint::LaunchAndEdit)
|
||||||
{
|
{
|
||||||
// check the temp file in case the project is just created and not saved to the workspaces.json yet
|
// check the temp file in case the project is just created and not saved to the workspaces.json yet
|
||||||
auto file = WorkspacesData::TempWorkspacesFile();
|
auto file = WorkspacesData::TempWorkspacesFile();
|
||||||
auto res = JsonUtils::ReadSingleWorkspace(file);
|
auto res = JsonUtils::ReadSingleWorkspace(file);
|
||||||
if (res.isOk() && projectToLaunch.id == id)
|
if (res.isOk() && projectToLaunch.id == cmdArgs.workspaceId)
|
||||||
{
|
{
|
||||||
projectToLaunch = res.getValue();
|
projectToLaunch = res.getValue();
|
||||||
}
|
}
|
||||||
else
|
else if (res.isError())
|
||||||
{
|
{
|
||||||
std::wstring formattedMessage{};
|
std::wstring formattedMessage{};
|
||||||
switch (res.error())
|
switch (res.error())
|
||||||
@@ -150,7 +132,7 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
|
|||||||
|
|
||||||
for (const auto& proj : workspaces)
|
for (const auto& proj : workspaces)
|
||||||
{
|
{
|
||||||
if (proj.id == id)
|
if (proj.id == cmdArgs.workspaceId)
|
||||||
{
|
{
|
||||||
projectToLaunch = proj;
|
projectToLaunch = proj;
|
||||||
break;
|
break;
|
||||||
@@ -160,13 +142,13 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
|
|||||||
|
|
||||||
if (projectToLaunch.id.empty())
|
if (projectToLaunch.id.empty())
|
||||||
{
|
{
|
||||||
Logger::critical(L"Workspace {} not found", id);
|
Logger::critical(L"Workspace {} not found", cmdArgs.workspaceId);
|
||||||
std::wstring formattedMessage = fmt::format(GET_RESOURCE_STRING(IDS_PROJECT_NOT_FOUND), id);
|
std::wstring formattedMessage = fmt::format(GET_RESOURCE_STRING(IDS_PROJECT_NOT_FOUND), cmdArgs.workspaceId);
|
||||||
MessageBox(NULL, formattedMessage.c_str(), GET_RESOURCE_STRING(IDS_WORKSPACES).c_str(), MB_ICONERROR | MB_OK);
|
MessageBox(NULL, formattedMessage.c_str(), GET_RESOURCE_STRING(IDS_WORKSPACES).c_str(), MB_ICONERROR | MB_OK);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Launcher launcher(projectToLaunch, workspaces, invokePoint);
|
Launcher launcher(projectToLaunch, workspaces, cmdArgs.invokePoint);
|
||||||
|
|
||||||
Logger::trace("Finished");
|
Logger::trace("Finished");
|
||||||
CoUninitialize();
|
CoUninitialize();
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ bool LaunchingStatus::AllLaunchedAndMoved() noexcept
|
|||||||
{
|
{
|
||||||
if (data.state != LaunchingState::Failed && data.state != LaunchingState::LaunchedAndMoved)
|
if (data.state != LaunchingState::Failed && data.state != LaunchingState::LaunchedAndMoved)
|
||||||
{
|
{
|
||||||
Logger::debug(data.state);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,22 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
std::vector<std::wstring> split(std::wstring s, const std::wstring& delimiter)
|
#include <workspaces-common/GuidUtils.h>
|
||||||
|
#include <workspaces-common/InvokePoint.h>
|
||||||
|
|
||||||
|
struct CommandLineArgs
|
||||||
{
|
{
|
||||||
std::vector<std::wstring> tokens;
|
std::wstring workspaceId;
|
||||||
|
InvokePoint invokePoint;
|
||||||
|
};
|
||||||
|
|
||||||
|
CommandLineArgs split(std::wstring s, const std::wstring& delimiter)
|
||||||
|
{
|
||||||
|
CommandLineArgs cmdArgs{};
|
||||||
|
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
std::wstring token;
|
std::wstring token;
|
||||||
|
std::vector<std::wstring> tokens;
|
||||||
while ((pos = s.find(delimiter)) != std::wstring::npos)
|
while ((pos = s.find(delimiter)) != std::wstring::npos)
|
||||||
{
|
{
|
||||||
token = s.substr(0, pos);
|
token = s.substr(0, pos);
|
||||||
@@ -16,5 +27,28 @@ std::vector<std::wstring> split(std::wstring s, const std::wstring& delimiter)
|
|||||||
}
|
}
|
||||||
tokens.push_back(s);
|
tokens.push_back(s);
|
||||||
|
|
||||||
return tokens;
|
for (const auto& token : tokens)
|
||||||
|
{
|
||||||
|
if (!cmdArgs.workspaceId.empty())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
auto invokePoint = static_cast<InvokePoint>(std::stoi(token));
|
||||||
|
cmdArgs.invokePoint = invokePoint;
|
||||||
|
}
|
||||||
|
catch (std::exception)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
auto guid = GuidFromString(token);
|
||||||
|
if (guid.has_value())
|
||||||
|
{
|
||||||
|
cmdArgs.workspaceId = token;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmdArgs;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,17 +35,7 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto args = split(commandLine, L" ");
|
auto args = split(commandLine, L" ");
|
||||||
std::wstring id{};
|
if (args.workspaceId.empty())
|
||||||
if (args.size() == 1)
|
|
||||||
{
|
|
||||||
id = args[0];
|
|
||||||
}
|
|
||||||
else if (args.size() == 2)
|
|
||||||
{
|
|
||||||
id = args[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (id.empty())
|
|
||||||
{
|
{
|
||||||
Logger::warn("Incorrect command line arguments: no workspace id");
|
Logger::warn("Incorrect command line arguments: no workspace id");
|
||||||
return 1;
|
return 1;
|
||||||
@@ -60,11 +50,11 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
|
|||||||
{
|
{
|
||||||
auto file = WorkspacesData::TempWorkspacesFile();
|
auto file = WorkspacesData::TempWorkspacesFile();
|
||||||
auto res = JsonUtils::ReadSingleWorkspace(file);
|
auto res = JsonUtils::ReadSingleWorkspace(file);
|
||||||
if (res.isOk() && res.value().id == id)
|
if (res.isOk() && res.value().id == args.workspaceId)
|
||||||
{
|
{
|
||||||
projectToLaunch = res.getValue();
|
projectToLaunch = res.getValue();
|
||||||
}
|
}
|
||||||
else
|
else if (res.isError())
|
||||||
{
|
{
|
||||||
Logger::error(L"Error reading temp file");
|
Logger::error(L"Error reading temp file");
|
||||||
return 1;
|
return 1;
|
||||||
@@ -86,7 +76,7 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
|
|||||||
|
|
||||||
for (const auto& proj : workspaces)
|
for (const auto& proj : workspaces)
|
||||||
{
|
{
|
||||||
if (proj.id == id)
|
if (proj.id == args.workspaceId)
|
||||||
{
|
{
|
||||||
projectToLaunch = proj;
|
projectToLaunch = proj;
|
||||||
break;
|
break;
|
||||||
@@ -96,7 +86,7 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
|
|||||||
|
|
||||||
if (projectToLaunch.id.empty())
|
if (projectToLaunch.id.empty())
|
||||||
{
|
{
|
||||||
Logger::critical(L"Workspace {} not found", id);
|
Logger::critical(L"Workspace {} not found", args.workspaceId);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user