From 484481f886ca40b133689126c5cda999824d8b0e Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 7 Oct 2022 10:14:24 +0200 Subject: [PATCH] fix: Opening files with unicode names through the command line --- lib/libimhex/include/hex/api/imhex_api.hpp | 1 + lib/libimhex/source/api/imhex_api.cpp | 21 +++++++++++++++++++++ main/source/main.cpp | 6 ++++-- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/libimhex/include/hex/api/imhex_api.hpp b/lib/libimhex/include/hex/api/imhex_api.hpp index 485624618..5b0c2e44e 100644 --- a/lib/libimhex/include/hex/api/imhex_api.hpp +++ b/lib/libimhex/include/hex/api/imhex_api.hpp @@ -188,6 +188,7 @@ namespace hex { }; const ProgramArguments &getProgramArguments(); + std::optional getProgramArgument(int index); float getTargetFPS(); void setTargetFPS(float fps); diff --git a/lib/libimhex/source/api/imhex_api.cpp b/lib/libimhex/source/api/imhex_api.cpp index 0515cb644..40346a153 100644 --- a/lib/libimhex/source/api/imhex_api.cpp +++ b/lib/libimhex/source/api/imhex_api.cpp @@ -11,6 +11,12 @@ #include +#if defined(OS_WINDOWS) + #define WIN32_LEAN_AND_MEAN + #include + #include +#endif + namespace hex { namespace ImHexApi::Common { @@ -399,6 +405,21 @@ namespace hex { return impl::s_programArguments; } + std::optional getProgramArgument(int index) { + if (index >= impl::s_programArguments.argc) { + return std::nullopt; + } + + #if defined(OS_WINDOWS) + std::wstring wideArg = ::CommandLineToArgvW(::GetCommandLineW(), &impl::s_programArguments.argc)[index]; + std::string byteArg = std::wstring_convert, wchar_t>().to_bytes(wideArg); + + return std::u8string(byteArg.begin(), byteArg.end()); + #else + return std::u8string(reinterpret_cast(impl::s_programArguments.argv[index])); + #endif + } + static float s_targetFPS = 60.0F; diff --git a/main/source/main.cpp b/main/source/main.cpp index ba4bde941..22ea9e817 100644 --- a/main/source/main.cpp +++ b/main/source/main.cpp @@ -54,8 +54,10 @@ int main(int argc, char **argv, char **envp) { if (argc == 1) ; // No arguments provided else if (argc >= 2) { - for (auto i = 1; i < argc; i++) - EventManager::post(argv[i]); + for (auto i = 1; i < argc; i++) { + if (auto argument = ImHexApi::System::getProgramArgument(i); argument.has_value()) + EventManager::post(argument.value()); + } } try {