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.
This commit is contained in:
Sam Lantinga 2016-10-14 08:27:44 -07:00
parent 3f38bd9142
commit 8aab39cb7d

View File

@ -126,12 +126,49 @@ main_utf8(int argc, char *argv[])
return SDL_main(argc, 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] */ /* This is where execution begins [console apps, ansi] */
int int
console_ansi_main(int argc, char *argv[]) console_ansi_main(int argc, char *argv[])
{ {
/* !!! FIXME: are these in the system codepage? We need to convert to UTF-8. */ return main_getcmdline();
return main_utf8(argc, argv);
} }
@ -162,39 +199,7 @@ console_wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
int WINAPI int WINAPI
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
{ {
char **argv; return main_getcmdline();
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;
} }
#endif /* __WIN32__ */ #endif /* __WIN32__ */