From 7ae4b994f74f3b91f21ca1c907e141e687db50fe Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Thu, 13 Sep 2018 16:23:12 -0700 Subject: [PATCH] Dev16 removed the static_cast<> in a std::basic_string constructor so need to explicit convert string to wstring to avoid warning. (dotnet/coreclr#19947) Commit migrated from https://github.com/dotnet/coreclr/commit/30e66e40b712fb11fcbbc60ecc041e2f02484796 --- .../src/coreclr/hosts/coreshim/CoreShim.cpp | 37 ++++++++++++++-------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/coreclr/src/coreclr/hosts/coreshim/CoreShim.cpp b/src/coreclr/src/coreclr/hosts/coreshim/CoreShim.cpp index 497c10e..f055b04 100644 --- a/src/coreclr/src/coreclr/hosts/coreshim/CoreShim.cpp +++ b/src/coreclr/src/coreclr/hosts/coreshim/CoreShim.cpp @@ -11,6 +11,7 @@ namespace { + template struct PathBuffer { PathBuffer() @@ -38,21 +39,21 @@ namespace return Len; } - operator WCHAR *() + operator CT *() { return Buf; } - WCHAR DefBuffer[MAX_PATH]; - std::vector BigBuffer; + CT DefBuffer[MAX_PATH]; + std::vector BigBuffer; - WCHAR *Buf; + CT *Buf; DWORD Len; }; - std::string GetExePath() + std::wstring GetExePath() { - PathBuffer buffer; + PathBuffer buffer; DWORD len = ::GetModuleFileNameW(nullptr, buffer, buffer); while (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) { @@ -60,7 +61,7 @@ namespace len = ::GetModuleFileNameW(nullptr, buffer, buffer); } - return std::string{ buffer.Buf, buffer.Buf + len }; + return std::wstring{ buffer.Buf, buffer.Buf + len }; } std::wstring GetEnvVar(_In_z_ const WCHAR *env) @@ -69,13 +70,23 @@ namespace if (len == 0) throw __HRESULT_FROM_WIN32(ERROR_ENVVAR_NOT_FOUND); - PathBuffer buffer; + PathBuffer buffer; buffer.SetLength(len); (void)::GetEnvironmentVariableW(env, buffer, buffer); return static_cast(buffer.Buf); } + std::string ConvertWideToUtf8(_In_ const std::wstring &wide) + { + // [TODO] Properly convert to UTF-8 + std::string narrow; + for (WCHAR p : wide) + narrow.push_back(static_cast(p)); + + return narrow; + } + coreclr *s_CoreClrInstance; } @@ -86,7 +97,7 @@ namespace Utility try { std::wstring envVarLocal = GetEnvVar(env); - envVar = { std::begin(envVarLocal), std::end(envVarLocal) }; + envVar = ConvertWideToUtf8(envVarLocal); } catch (HRESULT hr) { @@ -163,7 +174,7 @@ HRESULT coreclr::CreateTpaList(_Inout_ std::string &tpaList, _In_opt_z_ const WC w_dirLocal = { dir }; } - std::string dirLocal{ std::begin(w_dirLocal), std::end(w_dirLocal) }; + std::string dirLocal = ConvertWideToUtf8(w_dirLocal); w_dirLocal.append(W("\\*")); std::set addedAssemblies; @@ -204,8 +215,7 @@ HRESULT coreclr::CreateTpaList(_Inout_ std::string &tpaList, _In_opt_z_ const WC { addedAssemblies.insert(std::move(filenameWithoutExt)); - // [TODO] Properly convert to UTF-8 - std::string filename_utf8{ std::begin(filename), std::end(filename) }; + std::string filename_utf8 = ConvertWideToUtf8(filename); tpaStream << dirLocal << "\\" << filename_utf8 << ";"; } } @@ -266,7 +276,8 @@ HRESULT coreclr::Initialize( HRESULT hr; try { - const std::string exePath = GetExePath(); + const std::wstring exePathW = GetExePath(); + const std::string exePath = ConvertWideToUtf8(exePathW); RETURN_IF_FAILED(_initialize(exePath.c_str(), appDomainName, propertyCount, keys, values, &_clrInst, &_appDomainId)); } catch (const std::bad_alloc&) -- 2.7.4