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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
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__ */