One way would be to use CommandLineToArgvW()
to parse the result of GetCommandLineW()
into an argv
-style array of UTF-16 encoded strings, then use WideCharToMultiByte()
to convert them to ANSI strings so you can then pass them to main()
(assuming you can't use wmain()
instead).
For example:
int w_argc = 0;
LPWSTR* w_argv = CommandLineToArgvW(GetCommandLineW(), &w_argc);
if (w_argv)
{
char** my_argv = new char*[w_argc];
int my_argc = 0;
for (int i = 0; i < w_argc; ++i)
{
int w_len = lstrlenW(w_argv[i]);
int len = WideCharToMultiByte(CP_ACP, 0, w_argv[i], w_len, NULL, 0, NULL, NULL);
my_argv[my_argc] = new char[len+1];
WideCharToMultiByte(CP_ACP, 0, wargv[i], w_len, my_argv[my_argc], len+1, NULL, NULL);
++my_argc;
}
main(my_argc, my_argv);
for (int i = 0; i < my_argc; ++i)
delete[] my_argv[i];
delete[] my_argv;
LocalFree(w_argv);
}
Alternatively:
int w_argc = 0;
LPWSTR* w_argv = CommandLineToArgvW(GetCommandLineW(), &w_argc);
if (w_argv)
{
std vector<std::string> my_argv_buf;
my_argv.reserve(w_argc);
for (int i = 0; i < w_argc; ++i)
{
int w_len = lstrlenW(w_argv[i]);
int len = WideCharToMultiByte(CP_ACP, 0, w_argv[i], w_len, NULL, 0, NULL, NULL);
std::string s;
s.resize(len);
WideCharToMultiByte(CP_ACP, 0, wargv[i], w_len, &s[0], len, NULL, NULL);
my_argv_buf.push_back(s);
}
std vector<char*> my_argv;
my_argv.reserve(my_argv_buf.size());
for (std vector<std::string>::iterator i = my_argv_buf.begin(); i != my_argv_buf.end(); ++i)
my_argv.push_back(i->c_str());
main(my_argv.size(), &my_argv[0]);
LocalFree(w_argv);
}