From c17238ce5e7e810a0a9182981d185c61e1af5096 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Thu, 29 Jun 2017 14:56:53 +0100 Subject: [PATCH] deviceinfo: Fix character encoding for GetComputerName. Should be UTF8. Fixes: #651 --- core/os/device/deviceinfo/cc/windows/query.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/core/os/device/deviceinfo/cc/windows/query.cpp b/core/os/device/deviceinfo/cc/windows/query.cpp index fe39459c10..2c36ad5e78 100644 --- a/core/os/device/deviceinfo/cc/windows/query.cpp +++ b/core/os/device/deviceinfo/cc/windows/query.cpp @@ -47,7 +47,7 @@ struct Context { HDC mHDC; HGLRC mCtx; int mNumCores; - char mHostName[MAX_COMPUTERNAME_LENGTH+1]; + char mHostName[MAX_COMPUTERNAME_LENGTH*4+1]; // Stored as UTF-8 OSVERSIONINFOEX mOsVersion; const char* mOsName; }; @@ -146,7 +146,21 @@ bool createContext(void* platform_data) { gContext.mNumCores = sysInfo.dwNumberOfProcessors; DWORD size = MAX_COMPUTERNAME_LENGTH + 1; - GetComputerNameA(gContext.mHostName, &size); + WCHAR host_wide[MAX_COMPUTERNAME_LENGTH + 1]; + if (!GetComputerNameW(host_wide, &size)) { + snprintf(gContext.mError, sizeof(gContext.mError), + "Couldn't get host name: %d", GetLastError()); + } + WideCharToMultiByte( + CP_UTF8, // CodePage + 0, // dwFlags + host_wide, // lpWideCharStr + -1, // cchWideChar + gContext.mHostName, // lpMultiByteStr + sizeof(gContext.mHostName), // cbMultiByte + nullptr, // lpDefaultChar + nullptr // lpUsedDefaultChar + ); return true; }