From fad8d37a100cde96d1e4f66cb450d27372537d8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=B6glinger-Stelzer?= Date: Sun, 13 Aug 2023 15:04:46 +0200 Subject: [PATCH] Fixed warning of incorrect wide to narrow string conversion --- Injector/Injector.cpp | 9 +++++++-- Injector/Injector.vcxproj | 1 + Injector/Injector.vcxproj.filters | 3 +++ Injector/UniUtil.h | 19 +++++++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 Injector/UniUtil.h diff --git a/Injector/Injector.cpp b/Injector/Injector.cpp index 124c577..204cba4 100644 --- a/Injector/Injector.cpp +++ b/Injector/Injector.cpp @@ -11,6 +11,7 @@ #include "Injector.h" #include "EnsureCleanup.h" #include "StringUtil.h" +#include "UniUtil.h" // Static data Injector* Injector::m_pSingleton = 0; @@ -231,8 +232,12 @@ std::tstring Injector::GetPath( const std::tstring& ModuleName ) // Check path/file is valid if (GetFileAttributes(ModulePath.c_str()) == INVALID_FILE_ATTRIBUTES) { - std::string NewModulePath(ModulePath.begin(),ModulePath.end()); - throw std::runtime_error("Could not find module. Path: '" + NewModulePath + "'."); +#ifdef _UNICODE + std::string NarrowModulePath(ConvertWideToANSI(ModulePath)); +#else + std::string NarrowModulePath(ModulePath.begin(), ModulePath.end()); +#endif + throw std::runtime_error("Could not find module. Path: '" + NarrowModulePath + "'."); } // Return module path diff --git a/Injector/Injector.vcxproj b/Injector/Injector.vcxproj index ed6f01c..16a439a 100644 --- a/Injector/Injector.vcxproj +++ b/Injector/Injector.vcxproj @@ -335,6 +335,7 @@ + diff --git a/Injector/Injector.vcxproj.filters b/Injector/Injector.vcxproj.filters index af18f84..1f5d5a4 100644 --- a/Injector/Injector.vcxproj.filters +++ b/Injector/Injector.vcxproj.filters @@ -44,6 +44,9 @@ Header Files + + Header Files + diff --git a/Injector/UniUtil.h b/Injector/UniUtil.h new file mode 100644 index 0000000..c48215d --- /dev/null +++ b/Injector/UniUtil.h @@ -0,0 +1,19 @@ +#pragma once +#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING 1 +#include // wstring_convert + +std::string ConvertWideToANSI(const std::wstring& wstr) +{ + int count = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), (int)wstr.length(), NULL, 0, NULL, NULL); + std::string str(count, 0); + WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, &str[0], count, NULL, NULL); + return str; +} + +std::wstring ConvertAnsiToWide(const std::string& str) +{ + int count = MultiByteToWideChar(CP_ACP, 0, str.c_str(), (int)str.length(), NULL, 0); + std::wstring wstr(count, 0); + MultiByteToWideChar(CP_ACP, 0, str.c_str(), (int)str.length(), &wstr[0], count); + return wstr; +} \ No newline at end of file