Fixed bug 3452 - Getting unicode arguments for the main entry point on Windows
authorSam Lantinga <slouken@libsdl.org>
Fri, 14 Oct 2016 08:27:44 -0700
changeset 10538e093aacd58a7
parent 10537 dd84646065e6
child 10539 bf0989bde198
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.
src/main/windows/SDL_windows_main.c
     1.1 --- a/src/main/windows/SDL_windows_main.c	Fri Oct 14 08:22:48 2016 -0700
     1.2 +++ b/src/main/windows/SDL_windows_main.c	Fri Oct 14 08:27:44 2016 -0700
     1.3 @@ -126,12 +126,49 @@
     1.4      return SDL_main(argc, argv);
     1.5  }
     1.6  
     1.7 +/* Gets the arguments with GetCommandLine, converts them to argc and argv
     1.8 +   and calls main_utf8 */
     1.9 +static int
    1.10 +main_getcmdline()
    1.11 +{
    1.12 +    char **argv;
    1.13 +    int argc;
    1.14 +    char *cmdline;
    1.15 +    int retval = 0;
    1.16 +
    1.17 +    /* Grab the command line */
    1.18 +    TCHAR *text = GetCommandLine();
    1.19 +#if UNICODE
    1.20 +    cmdline = WIN_StringToUTF8(text);
    1.21 +#else
    1.22 +    /* !!! FIXME: are these in the system codepage? We need to convert to UTF-8. */
    1.23 +    cmdline = SDL_strdup(text);
    1.24 +#endif
    1.25 +    if (cmdline == NULL) {
    1.26 +        return OutOfMemory();
    1.27 +    }
    1.28 +
    1.29 +    /* Parse it into argv and argc */
    1.30 +    argc = ParseCommandLine(cmdline, NULL);
    1.31 +    argv = SDL_stack_alloc(char *, argc + 1);
    1.32 +    if (argv == NULL) {
    1.33 +        return OutOfMemory();
    1.34 +    }
    1.35 +    ParseCommandLine(cmdline, argv);
    1.36 +
    1.37 +    retval = main_utf8(argc, argv);
    1.38 +
    1.39 +    SDL_stack_free(argv);
    1.40 +    SDL_free(cmdline);
    1.41 +
    1.42 +    return retval;
    1.43 +}
    1.44 +
    1.45  /* This is where execution begins [console apps, ansi] */
    1.46  int
    1.47  console_ansi_main(int argc, char *argv[])
    1.48  {
    1.49 -    /* !!! FIXME: are these in the system codepage? We need to convert to UTF-8. */
    1.50 -    return main_utf8(argc, argv);
    1.51 +    return main_getcmdline();
    1.52  }
    1.53  
    1.54  
    1.55 @@ -162,39 +199,7 @@
    1.56  int WINAPI
    1.57  WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
    1.58  {
    1.59 -    char **argv;
    1.60 -    int argc;
    1.61 -    char *cmdline;
    1.62 -
    1.63 -    /* Grab the command line */
    1.64 -    TCHAR *text = GetCommandLine();
    1.65 -#if UNICODE
    1.66 -    cmdline = WIN_StringToUTF8(text);
    1.67 -#else
    1.68 -    /* !!! FIXME: are these in the system codepage? We need to convert to UTF-8. */
    1.69 -    cmdline = SDL_strdup(text);
    1.70 -#endif
    1.71 -    if (cmdline == NULL) {
    1.72 -        return OutOfMemory();
    1.73 -    }
    1.74 -
    1.75 -    /* Parse it into argv and argc */
    1.76 -    argc = ParseCommandLine(cmdline, NULL);
    1.77 -    argv = SDL_stack_alloc(char *, argc + 1);
    1.78 -    if (argv == NULL) {
    1.79 -        return OutOfMemory();
    1.80 -    }
    1.81 -    ParseCommandLine(cmdline, argv);
    1.82 -
    1.83 -    /* Run the main program */
    1.84 -    main_utf8(argc, argv);
    1.85 -
    1.86 -    SDL_stack_free(argv);
    1.87 -
    1.88 -    SDL_free(cmdline);
    1.89 -
    1.90 -    /* Hush little compiler, don't you cry... */
    1.91 -    return 0;
    1.92 +    return main_getcmdline();
    1.93  }
    1.94  
    1.95  #endif /* __WIN32__ */