Commit 8aab39cb7d58b09fc5f7b1337a3651fc1f9851e7

Sam Lantinga 2016-10-14T08:27:44

Fixed bug 3452 - Getting unicode arguments for the main entry point on Windows Simon Hug There are currently three entry points in the SDL2_main code for windows: main, wmain and WinMain. Only the latter two properly convert the arguments to UTF-8. Console applications linked with MSVC will always link with the main entry point (wmain has to be selected by manually setting the entry point). This makes it likely that such programs will not have proper unicode arguments.

diff --git a/src/main/windows/SDL_windows_main.c b/src/main/windows/SDL_windows_main.c
index 67ef98e..b502ed5 100644
--- a/src/main/windows/SDL_windows_main.c
+++ b/src/main/windows/SDL_windows_main.c
@@ -126,12 +126,49 @@ main_utf8(int argc, char *argv[])
     return SDL_main(argc, argv);
 }
 
+/* Gets the arguments with GetCommandLine, converts them to argc and argv
+   and calls main_utf8 */
+static int
+main_getcmdline()
+{
+    char **argv;
+    int argc;
+    char *cmdline;
+    int retval = 0;
+
+    /* Grab the command line */
+    TCHAR *text = GetCommandLine();
+#if UNICODE
+    cmdline = WIN_StringToUTF8(text);
+#else
+    /* !!! FIXME: are these in the system codepage? We need to convert to UTF-8. */
+    cmdline = SDL_strdup(text);
+#endif
+    if (cmdline == NULL) {
+        return OutOfMemory();
+    }
+
+    /* Parse it into argv and argc */
+    argc = ParseCommandLine(cmdline, NULL);
+    argv = SDL_stack_alloc(char *, argc + 1);
+    if (argv == NULL) {
+        return OutOfMemory();
+    }
+    ParseCommandLine(cmdline, argv);
+
+    retval = main_utf8(argc, argv);
+
+    SDL_stack_free(argv);
+    SDL_free(cmdline);
+
+    return retval;
+}
+
 /* This is where execution begins [console apps, ansi] */
 int
 console_ansi_main(int argc, char *argv[])
 {
-    /* !!! FIXME: are these in the system codepage? We need to convert to UTF-8. */
-    return main_utf8(argc, argv);
+    return main_getcmdline();
 }
 
 
@@ -162,39 +199,7 @@ console_wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
 int WINAPI
 WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
 {
-    char **argv;
-    int argc;
-    char *cmdline;
-
-    /* Grab the command line */
-    TCHAR *text = GetCommandLine();
-#if UNICODE
-    cmdline = WIN_StringToUTF8(text);
-#else
-    /* !!! FIXME: are these in the system codepage? We need to convert to UTF-8. */
-    cmdline = SDL_strdup(text);
-#endif
-    if (cmdline == NULL) {
-        return OutOfMemory();
-    }
-
-    /* Parse it into argv and argc */
-    argc = ParseCommandLine(cmdline, NULL);
-    argv = SDL_stack_alloc(char *, argc + 1);
-    if (argv == NULL) {
-        return OutOfMemory();
-    }
-    ParseCommandLine(cmdline, argv);
-
-    /* Run the main program */
-    main_utf8(argc, argv);
-
-    SDL_stack_free(argv);
-
-    SDL_free(cmdline);
-
-    /* Hush little compiler, don't you cry... */
-    return 0;
+    return main_getcmdline();
 }
 
 #endif /* __WIN32__ */