Convert command line arguments on Windows from UTF-16 to UTF-8
[libs/core.git] / source / core / main.cpp
1 #ifdef WIN32
2 #include <windows.h>
3 #include <msp/stringcodec/utf16.h>
4 #include <msp/stringcodec/utf8.h>
5 #endif
6 #include "application.h"
7
8 #ifdef WIN32
9 using namespace std;
10 using namespace Msp;
11
12 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int /*nCmdShow*/)
13
14         int argc        = 0;
15         LPWSTR *argv_w = CommandLineToArgvW(GetCommandLineW(), &argc);
16
17         unsigned max_size = 0;
18         for(int i=0; i<argc; ++i)
19                 max_size += wcslen(argv_w[i])*4+1;
20
21         char *argv_buf = new char[max_size+(argc+1)*sizeof(char *)];
22         char **argv = reinterpret_cast<char **>(argv_buf);
23         char *argv_ptr = argv_buf+(argc+1)*sizeof(char *);
24         for(int i=0; i<argc; ++i)
25         {
26                 argv[i] = argv_ptr;
27                 string arg(reinterpret_cast<char *>(argv_w[i]), wcslen(argv_w[i])*2);
28                 arg = StringCodec::transcode<StringCodec::Utf16Le, StringCodec::Utf8>(arg);
29                 copy(arg.begin(), arg.end(), argv_ptr);
30                 argv_ptr += arg.size();
31                 *argv_ptr++ = 0;
32         }
33
34         LocalFree(argv_w);
35         int exit_code = Msp::Application::run(argc, argv, hInstance);
36         delete[] argv_buf;
37         return exit_code;
38 }
39 #endif
40
41 int main(int argc, char **argv)
42 { return Msp::Application::run(argc, argv); }