From 55f3e6bc342ba52304febbc1159c566daa894993 Mon Sep 17 00:00:00 2001 From: Maurice Kalinowski Date: Mon, 25 May 2015 23:31:05 +0300 Subject: [PATCH] WinRT: Fix argument handling on Windows 10 The Windows 10 CRT does not export __getmainargs(), so we need to move to GetCommandLine like on WinCE and desktop Windows. As CommandLineToArgv is not available, the command line is split according by spaces, allowing for quotes (and quote escaping) around arguments. Change-Id: Ibc969df94ca5423a3a71d8190bbacd201189ea19 Reviewed-by: Andrew Knight Reviewed-by: Maurice Kalinowski --- src/winmain/qtmain_winrt.cpp | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/winmain/qtmain_winrt.cpp b/src/winmain/qtmain_winrt.cpp index 5a44df622a..e68da520e7 100644 --- a/src/winmain/qtmain_winrt.cpp +++ b/src/winmain/qtmain_winrt.cpp @@ -222,11 +222,49 @@ private: // Main entry point for Appx containers int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { +#if _MSC_VER < 1900 int argc = 0; char **argv, **env; _startupinfo info = { _query_new_mode() }; if (int init = __getmainargs(&argc, &argv, &env, false, &info)) return init; +#else + QByteArray commandLine = QString::fromWCharArray(GetCommandLine()).toUtf8(); + QVarLengthArray args; + args.append(commandLine.data()); + bool quote = false; + bool escape = false; + for (int i = 0; i < commandLine.size(); ++i) { + switch (commandLine.at(i)) { + case '\\': + escape = true; + break; + case '"': + if (escape) { + escape = false; + break; + } + quote = !quote; + commandLine[i] = '\0'; + break; + case ' ': + if (quote) + break; + commandLine[i] = '\0'; + if (args.last()[0] != '\0') + args.append(commandLine.data() + i + 1); + // fall through + default: + if (args.last()[0] == '\0') + args.last() = commandLine.data() + i; + escape = false; // only quotes are escaped + break; + } + } + int argc = args.size(); + char **argv = args.data(); + char **env = Q_NULLPTR; +#endif // _MSC_VER >= 1900 for (int i = 0; env && env[i]; ++i) { QByteArray var(env[i]);