From 00390847e6dacfde17cfa38634ce7895592089c6 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Wed, 22 Jul 2015 10:20:50 -0700 Subject: [PATCH] Convert get_login_r to UTF-16 and return --- src/impl/getusername.cpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/impl/getusername.cpp b/src/impl/getusername.cpp index 5e3aeb1c5f..ba58182502 100644 --- a/src/impl/getusername.cpp +++ b/src/impl/getusername.cpp @@ -4,8 +4,13 @@ #include #include #include +#include +#include -using namespace std; +using std::string; +using std::vector; + +using SCXCoreLib::Utf8ToUtf16le; const string utf8 = "UTF-8"; @@ -73,15 +78,22 @@ BOOL GetUserName(WCHAR_T *lpBuffer, LPDWORD lpnSize) return 0; } - // TODO: Convert to char* to wchar* (UTF-8 to UTF-16 LE w/o BOM) + // Convert to char * to WCHAR_T * (UTF-8 to UTF-16 LE w/o BOM) + string input = string(userName); + vector output; + Utf8ToUtf16le(input, output); - // Unfortunately codecvt is not yet supported by G++, othwerise: - // wstring_convert,char16_t> conversion; + if (output.size()/2 + 1 > *lpnSize) { + errno = ERROR_INSUFFICIENT_BUFFER; + return 0; + } - // TODO: Use PAL's Unicode converters + // Add two null bytes (because it's UTF-16) + output.push_back('\0'); + output.push_back('\0'); - // TODO: set the lpnSize to the userName length (see msdn) + memcpy(lpBuffer, &output[0], output.size()); + *lpnSize = output.size()/2; return 1; } -