From bac5d6c713ff061e74873ac969840f04a03cc485 Mon Sep 17 00:00:00 2001
From: James Turner <james@flightgear.org>
Date: Sat, 27 Apr 2019 18:50:24 +0100
Subject: [PATCH] Windows: read Unicode command line

One more step in full Unicode support.
---
 src/Main/bootstrap.cxx | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/Main/bootstrap.cxx b/src/Main/bootstrap.cxx
index 3800498cb..f538d2d89 100644
--- a/src/Main/bootstrap.cxx
+++ b/src/Main/bootstrap.cxx
@@ -134,9 +134,22 @@ static void initFPE(bool)
 #if defined(SG_WINDOWS)
 int main ( int argc, char **argv );
 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
-                             LPSTR lpCmdLine, int nCmdShow) {
+                             LPSTR lpCmdLine, int nCmdShow) 
+{
+	// convert wchar_t args to UTF-8 which is what we expect cross-platform
+    int     numArgs  = 0;
+	LPWSTR* wideArgs = CommandLineToArgvW(GetCommandLineW(), &numArgs);
 
-  main( __argc, __argv );
+	std::vector<char*> utf8Args;
+    utf8Args.reserve(numArgs);
+
+	for (int a = 0; a < numArgs; ++a) {
+		const auto s = simgear::strutils::convertWStringToUtf8(wideArgs[a]);
+		// note we leak these (strudp calls malloc) but not a big concern
+        utf8Args[a] = strdup(s.c_str());
+	}
+
+	main(numArgs, utf8Args.data());
 }
 #endif