From 577044163e53448f2bee154fd72d6e6e1e296cd0 Mon Sep 17 00:00:00 2001 From: Seraphima Zykova Date: Thu, 3 Oct 2024 17:09:13 +0300 Subject: [PATCH] [Workspaces]Fix launching incorrect workspace via shortcut (#35233) ensure one launcher instance is running --- src/modules/Workspaces/WorkspacesLauncher/main.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/modules/Workspaces/WorkspacesLauncher/main.cpp b/src/modules/Workspaces/WorkspacesLauncher/main.cpp index 38e88acd48..9f3d151097 100644 --- a/src/modules/Workspaces/WorkspacesLauncher/main.cpp +++ b/src/modules/Workspaces/WorkspacesLauncher/main.cpp @@ -16,6 +16,7 @@ const std::wstring moduleName = L"Workspaces\\WorkspacesLauncher"; const std::wstring internalPath = L""; +const std::wstring instanceMutexName = L"Local\\PowerToys_WorkspacesLauncher_InstanceMutex"; int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cmdShow) { @@ -28,6 +29,18 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm 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() }; auto cmdArgs = split(cmdLineStr, L" "); if (cmdArgs.workspaceId.empty())