From bbe336ee97c86c424f8f94e0f947f8e3b20e7123 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Mon, 9 May 2022 16:37:27 +0200 Subject: [PATCH] Use GetCommandLineW() to get a UTF16-encoded command line on Windows (#2248). --- src/lib/cross.h | 23 +++++++++++++++++++++++ src/lib/cross_unix.cc | 7 +++++++ src/lib/cross_windows.cc | 21 ++++++++++++++++++--- src/tools/dcpomatic_kdm_cli.cc | 5 ++++- 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/lib/cross.h b/src/lib/cross.h index b79f7ebdf..4be121d2b 100644 --- a/src/lib/cross.h +++ b/src/lib/cross.h @@ -159,4 +159,27 @@ boost::optional analyse_osx_media_path (std::string path); std::vector osx_disks_to_drives (std::vector disks); +class ArgFixer +{ +public: + ArgFixer(int argc, char** argv); + + int argc() const { + return _argc; + } + + char** argv() const { + return _argv; + } + +private: + int _argc; + char** _argv; +#ifdef DCPOMATIC_WINDOWS + std::vector _argv_strings; +#endif + +}; + + #endif diff --git a/src/lib/cross_unix.cc b/src/lib/cross_unix.cc index 7501a4c08..743c11980 100644 --- a/src/lib/cross_unix.cc +++ b/src/lib/cross_unix.cc @@ -84,3 +84,10 @@ dcpomatic::get_process_id () } +ArgFixer::ArgFixer(int argc, char** argv) + : _argc(argc) + , _argv(argv) +{ + +} + diff --git a/src/lib/cross_windows.cc b/src/lib/cross_windows.cc index 054961578..200b72485 100644 --- a/src/lib/cross_windows.cc +++ b/src/lib/cross_windows.cc @@ -44,10 +44,11 @@ extern "C" { #include #include #undef DATADIR -#include -#include -#include #include +#include +#include +#include +#include #include #include #include @@ -657,3 +658,17 @@ show_in_file_manager (boost::filesystem::path, boost::filesystem::path select) return (reinterpret_cast(r) <= 32); } + +ArgFixer::ArgFixer(int, char**) +{ + auto cmd_line = GetCommandLineW(); + auto wide_argv = CommandLineToArgvW(cmd_line, &_argc); + + _argv_strings.resize(_argc); + _argv = new char*[_argc]; + for (int i = 0; i < _argc; ++i) { + _argv_strings[i] = wchar_to_utf8(wide_argv[i]); + _argv[i] = const_cast(_argv_strings[i].c_str()); + } +} + diff --git a/src/tools/dcpomatic_kdm_cli.cc b/src/tools/dcpomatic_kdm_cli.cc index 46ab47a9a..1db5ce7f9 100644 --- a/src/tools/dcpomatic_kdm_cli.cc +++ b/src/tools/dcpomatic_kdm_cli.cc @@ -24,6 +24,7 @@ */ +#include "lib/cross.h" #include "lib/kdm_cli.h" #include "lib/util.h" #include @@ -32,10 +33,12 @@ int main (int argc, char* argv[]) { + ArgFixer fixer(argc, argv); + dcpomatic_setup_path_encoding (); dcpomatic_setup (); - auto error = kdm_cli (argc, argv, [](std::string s) { std::cout << s << "\n"; }); + auto error = kdm_cli (fixer.argc(), fixer.argv(), [](std::string s) { std::cout << s << "\n"; }); if (error) { std::cerr << *error << "\n"; exit (EXIT_FAILURE); -- 2.30.2