windows: wrap all main funcs with utf8 conversions

fixes 12858
This commit is contained in:
Shawn Hoffman 2022-03-07 23:22:35 -08:00
parent 9af9e791f6
commit a546c451b7
3 changed files with 59 additions and 16 deletions

View File

@ -149,6 +149,10 @@ static std::unique_ptr<Platform> GetPlatform(const optparse::Values& options)
return nullptr;
}
#ifdef _WIN32
#define main app_main
#endif
int main(int argc, char* argv[])
{
auto parser = CommandLineParse::CreateParser(CommandLineParse::ParserOptions::OmitGUIOptions);
@ -275,3 +279,18 @@ int main(int argc, char* argv[])
return 0;
}
#ifdef _WIN32
int wmain(int, wchar_t*[], wchar_t*[])
{
std::vector<std::string> args = CommandLineToUtf8Argv(GetCommandLineW());
const int argc = static_cast<int>(args.size());
std::vector<char*> argv(args.size());
for (size_t i = 0; i < args.size(); ++i)
argv[i] = args[i].data();
return main(argc, argv.data());
}
#undef main
#endif

View File

@ -98,18 +98,13 @@ static bool QtMsgAlertHandler(const char* caption, const char* text, bool yes_no
return false;
}
#ifndef _WIN32
#ifdef _WIN32
#define main app_main
#endif
int main(int argc, char* argv[])
{
#else
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
std::vector<std::string> utf8_args = CommandLineToUtf8Argv(GetCommandLineW());
const int utf8_argc = static_cast<int>(utf8_args.size());
std::vector<char*> utf8_argv(utf8_args.size());
for (size_t i = 0; i < utf8_args.size(); ++i)
utf8_argv[i] = utf8_args[i].data();
#ifdef _WIN32
const bool console_attached = AttachConsole(ATTACH_PARENT_PROCESS) != FALSE;
HANDLE stdout_handle = ::GetStdHandle(STD_OUTPUT_HANDLE);
if (console_attached && stdout_handle)
@ -134,12 +129,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
#endif
auto parser = CommandLineParse::CreateParser(CommandLineParse::ParserOptions::IncludeGUIOptions);
const optparse::Values& options =
#ifdef _WIN32
CommandLineParse::ParseArguments(parser.get(), utf8_argc, utf8_argv.data());
#else
CommandLineParse::ParseArguments(parser.get(), argc, argv);
#endif
const optparse::Values& options = CommandLineParse::ParseArguments(parser.get(), argc, argv);
const std::vector<std::string> args = parser->args();
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
@ -300,3 +290,18 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
return retval;
}
#ifdef _WIN32
int WINAPI wWinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPWSTR, _In_ int)
{
std::vector<std::string> args = CommandLineToUtf8Argv(GetCommandLineW());
const int argc = static_cast<int>(args.size());
std::vector<char*> argv(args.size());
for (size_t i = 0; i < args.size(); ++i)
argv[i] = args[i].data();
return main(argc, argv.data());
}
#undef main
#endif

View File

@ -20,6 +20,10 @@ static int PrintUsage(int code)
return code;
}
#ifdef _WIN32
#define main app_main
#endif
int main(int argc, char* argv[])
{
if (argc < 2)
@ -43,3 +47,18 @@ int main(int argc, char* argv[])
return command->Main(args);
}
#ifdef _WIN32
int wmain(int, wchar_t*[], wchar_t*[])
{
std::vector<std::string> args = CommandLineToUtf8Argv(GetCommandLineW());
const int argc = static_cast<int>(args.size());
std::vector<char*> argv(args.size());
for (size_t i = 0; i < args.size(); ++i)
argv[i] = args[i].data();
return main(argc, argv.data());
}
#undef main
#endif