Skip to content

Commit fad8d37

Browse files
committed
Fixed warning of incorrect wide to narrow string conversion
1 parent f41b738 commit fad8d37

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

Injector/Injector.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "Injector.h"
1212
#include "EnsureCleanup.h"
1313
#include "StringUtil.h"
14+
#include "UniUtil.h"
1415

1516
// Static data
1617
Injector* Injector::m_pSingleton = 0;
@@ -231,8 +232,12 @@ std::tstring Injector::GetPath( const std::tstring& ModuleName )
231232
// Check path/file is valid
232233
if (GetFileAttributes(ModulePath.c_str()) == INVALID_FILE_ATTRIBUTES)
233234
{
234-
std::string NewModulePath(ModulePath.begin(),ModulePath.end());
235-
throw std::runtime_error("Could not find module. Path: '" + NewModulePath + "'.");
235+
#ifdef _UNICODE
236+
std::string NarrowModulePath(ConvertWideToANSI(ModulePath));
237+
#else
238+
std::string NarrowModulePath(ModulePath.begin(), ModulePath.end());
239+
#endif
240+
throw std::runtime_error("Could not find module. Path: '" + NarrowModulePath + "'.");
236241
}
237242

238243
// Return module path

Injector/Injector.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@
335335
<ClInclude Include="Seh.h" />
336336
<ClInclude Include="StringUtil.h" />
337337
<ClInclude Include="StringWrap.h" />
338+
<ClInclude Include="UniUtil.h" />
338339
</ItemGroup>
339340
<ItemGroup>
340341
<ResourceCompile Include="Injector.rc" />

Injector/Injector.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
<ClInclude Include="StringUtil.h">
4545
<Filter>Header Files</Filter>
4646
</ClInclude>
47+
<ClInclude Include="UniUtil.h">
48+
<Filter>Header Files</Filter>
49+
</ClInclude>
4750
</ItemGroup>
4851
<ItemGroup>
4952
<ResourceCompile Include="Injector.rc">

Injector/UniUtil.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING 1
3+
#include <locale> // wstring_convert
4+
5+
std::string ConvertWideToANSI(const std::wstring& wstr)
6+
{
7+
int count = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), (int)wstr.length(), NULL, 0, NULL, NULL);
8+
std::string str(count, 0);
9+
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, &str[0], count, NULL, NULL);
10+
return str;
11+
}
12+
13+
std::wstring ConvertAnsiToWide(const std::string& str)
14+
{
15+
int count = MultiByteToWideChar(CP_ACP, 0, str.c_str(), (int)str.length(), NULL, 0);
16+
std::wstring wstr(count, 0);
17+
MultiByteToWideChar(CP_ACP, 0, str.c_str(), (int)str.length(), &wstr[0], count);
18+
return wstr;
19+
}

0 commit comments

Comments
 (0)