Skip to content

Commit

Permalink
Fixed bug 3452 - Getting unicode arguments for the main entry point o…
Browse files Browse the repository at this point in the history
…n 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.
  • Loading branch information
slouken committed Oct 14, 2016
1 parent 3f38bd9 commit 8aab39c
Showing 1 changed file with 40 additions and 35 deletions.
75 changes: 40 additions & 35 deletions src/main/windows/SDL_windows_main.c
Expand Up @@ -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();
}


Expand Down Expand Up @@ -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__ */
Expand Down

0 comments on commit 8aab39c

Please sign in to comment.