Skip to content

Commit

Permalink
Fixed warning of incorrect wide to narrow string conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
nefarius committed Aug 13, 2023
1 parent f41b738 commit fad8d37
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Injector/Injector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "Injector.h"
#include "EnsureCleanup.h"
#include "StringUtil.h"
#include "UniUtil.h"

// Static data
Injector* Injector::m_pSingleton = 0;
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Injector/Injector.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@
<ClInclude Include="Seh.h" />
<ClInclude Include="StringUtil.h" />
<ClInclude Include="StringWrap.h" />
<ClInclude Include="UniUtil.h" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Injector.rc" />
Expand Down
3 changes: 3 additions & 0 deletions Injector/Injector.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<ClInclude Include="StringUtil.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="UniUtil.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Injector.rc">
Expand Down
19 changes: 19 additions & 0 deletions Injector/UniUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING 1
#include <locale> // 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;
}

0 comments on commit fad8d37

Please sign in to comment.