From 35c7ef4ede83d27ecb90fd3d8a758cf4b54c4886 Mon Sep 17 00:00:00 2001 From: firai Date: Fri, 25 Apr 2025 20:22:10 +0800 Subject: [PATCH 1/5] Pass arguments in windows launcher template --- .../launcher_template_WINDOWS.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/portable/launchers_src/launcher_template_WINDOWS.cpp b/portable/launchers_src/launcher_template_WINDOWS.cpp index 9c878be6..e178f31e 100644 --- a/portable/launchers_src/launcher_template_WINDOWS.cpp +++ b/portable/launchers_src/launcher_template_WINDOWS.cpp @@ -19,6 +19,26 @@ int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR / std::wstring exeDir = exePath; exeDir = exeDir.substr(0, exeDir.find_last_of(L"\\/")); + // Get command line string + LPWSTR commandLine = GetCommandLineW(); + // Skip the current executable path and name + std::wstring args; + if (commandLine) { + // If path is double quoted, skip the entire double quote + if (commandLine[0] == L'"') { + LPWSTR closingQuote = wcschr(commandLine + 1, L'"'); + if (closingQuote) { + args = closingQuote + 1; // Skip closing quote and space + } + // Otherwise skip to first space + } else { + LPWSTR spacePos = wcschr(commandLine, L' '); + if (spacePos) { + args = spacePos + 1; // Skip space + } + } + } + // Define the path to the "scripts" directory std::wstring scriptsDir = exeDir + L"\\scripts"; @@ -40,6 +60,11 @@ int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR / // Define the command to run std::wstring target = L"cmd.exe /c \"" LAUNCH_TARGET L"\""; + // Append arguments if present + if (!args.empty()) { + target += L" " + args; + } + // Configure the process startup info STARTUPINFO si = { sizeof(si) }; si.dwFlags = STARTF_USESHOWWINDOW; // Prevent the window from appearing From 85d0e85c1024c10cdb429a4ec216d29b69ff6301 Mon Sep 17 00:00:00 2001 From: firai Date: Fri, 25 Apr 2025 22:43:00 +0800 Subject: [PATCH 2/5] Remove extra space from argument --- portable/launchers_src/launcher_template_WINDOWS.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/portable/launchers_src/launcher_template_WINDOWS.cpp b/portable/launchers_src/launcher_template_WINDOWS.cpp index e178f31e..9f03e097 100644 --- a/portable/launchers_src/launcher_template_WINDOWS.cpp +++ b/portable/launchers_src/launcher_template_WINDOWS.cpp @@ -28,13 +28,13 @@ int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR / if (commandLine[0] == L'"') { LPWSTR closingQuote = wcschr(commandLine + 1, L'"'); if (closingQuote) { - args = closingQuote + 1; // Skip closing quote and space + args = closingQuote + 2; // Skip closing quote and space } // Otherwise skip to first space } else { LPWSTR spacePos = wcschr(commandLine, L' '); if (spacePos) { - args = spacePos + 1; // Skip space + args = spacePos + 2; // GetCommandLineW puts 2 spaces when path isn't double quoted } } } From eeda305313d06f4c88124f909806f5d19bd9d286 Mon Sep 17 00:00:00 2001 From: firai Date: Sat, 26 Apr 2025 00:25:32 +0800 Subject: [PATCH 3/5] Remove space from concat rather than from pointer offset --- portable/launchers_src/launcher_template_WINDOWS.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/portable/launchers_src/launcher_template_WINDOWS.cpp b/portable/launchers_src/launcher_template_WINDOWS.cpp index 9f03e097..c02aea61 100644 --- a/portable/launchers_src/launcher_template_WINDOWS.cpp +++ b/portable/launchers_src/launcher_template_WINDOWS.cpp @@ -28,13 +28,13 @@ int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR / if (commandLine[0] == L'"') { LPWSTR closingQuote = wcschr(commandLine + 1, L'"'); if (closingQuote) { - args = closingQuote + 2; // Skip closing quote and space + args = closingQuote + 1; // Skip closing quote and space } // Otherwise skip to first space } else { LPWSTR spacePos = wcschr(commandLine, L' '); if (spacePos) { - args = spacePos + 2; // GetCommandLineW puts 2 spaces when path isn't double quoted + args = spacePos + 1; // GetCommandLineW puts 2 spaces when path isn't double quoted } } } @@ -62,7 +62,7 @@ int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR / // Append arguments if present if (!args.empty()) { - target += L" " + args; + target += args; } // Configure the process startup info From 164cf600c94064b2b7cc670ca4656bad0aef6f32 Mon Sep 17 00:00:00 2001 From: firai Date: Sat, 26 Apr 2025 01:28:04 +0800 Subject: [PATCH 4/5] Wrap everything in outer quotes if arguments are passed --- portable/launchers_src/launcher_template_WINDOWS.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/portable/launchers_src/launcher_template_WINDOWS.cpp b/portable/launchers_src/launcher_template_WINDOWS.cpp index c02aea61..04b25fef 100644 --- a/portable/launchers_src/launcher_template_WINDOWS.cpp +++ b/portable/launchers_src/launcher_template_WINDOWS.cpp @@ -57,12 +57,12 @@ int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR / return 1; } - // Define the command to run - std::wstring target = L"cmd.exe /c \"" LAUNCH_TARGET L"\""; - - // Append arguments if present + // Define the command to run and append arguments if present + std::wstring target; if (!args.empty()) { - target += args; + target = L"cmd.exe /c \"\"" LAUNCH_TARGET L"\"" + args + L"\""; + } else { + target = L"cmd.exe /c \"" LAUNCH_TARGET L"\""; } // Configure the process startup info From 4a624c67f912ad3b4bafa17cbe07342a82a2a9d0 Mon Sep 17 00:00:00 2001 From: firai Date: Sat, 26 Apr 2025 14:46:51 +0800 Subject: [PATCH 5/5] More reliable way to deal with whitespace --- .../launcher_template_WINDOWS.cpp | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/portable/launchers_src/launcher_template_WINDOWS.cpp b/portable/launchers_src/launcher_template_WINDOWS.cpp index 04b25fef..6b10a89a 100644 --- a/portable/launchers_src/launcher_template_WINDOWS.cpp +++ b/portable/launchers_src/launcher_template_WINDOWS.cpp @@ -19,25 +19,25 @@ int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR / std::wstring exeDir = exePath; exeDir = exeDir.substr(0, exeDir.find_last_of(L"\\/")); - // Get command line string + // Get command line string and extract arguments LPWSTR commandLine = GetCommandLineW(); - // Skip the current executable path and name std::wstring args; - if (commandLine) { - // If path is double quoted, skip the entire double quote - if (commandLine[0] == L'"') { - LPWSTR closingQuote = wcschr(commandLine + 1, L'"'); - if (closingQuote) { - args = closingQuote + 1; // Skip closing quote and space - } - // Otherwise skip to first space - } else { - LPWSTR spacePos = wcschr(commandLine, L' '); - if (spacePos) { - args = spacePos + 1; // GetCommandLineW puts 2 spaces when path isn't double quoted - } + // If executable path is double quoted, skip the entire quoted section + if (commandLine[0] == L'"') { + LPWSTR closingQuote = wcschr(commandLine + 1, L'"'); + if (closingQuote) { + args = closingQuote + 1; + } + // For non-quoted path, skip to character after first space if it exists + } else { + LPWSTR spacePos = wcschr(commandLine, L' '); + if (spacePos) { + args = spacePos + 1; } } + // Strip leading whitespace + size_t args_start = args.find_first_not_of(L" "); + args = (args_start != std::wstring::npos) ? args.substr(args_start) : L""; // Define the path to the "scripts" directory std::wstring scriptsDir = exeDir + L"\\scripts"; @@ -60,7 +60,7 @@ int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR / // Define the command to run and append arguments if present std::wstring target; if (!args.empty()) { - target = L"cmd.exe /c \"\"" LAUNCH_TARGET L"\"" + args + L"\""; + target = L"cmd.exe /c \"\"" LAUNCH_TARGET L"\" " + args + L"\""; } else { target = L"cmd.exe /c \"" LAUNCH_TARGET L"\""; }