Convert get_login_r to UTF-16 and return

This commit is contained in:
Andrew Schwartzmeyer 2015-07-22 10:20:50 -07:00
parent e14934d06d
commit 00390847e6

View file

@ -4,8 +4,13 @@
#include <locale.h>
#include <unistd.h>
#include <string>
#include <vector>
#include <scxcorelib/scxstrencodingconv.h>
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<unsigned char> output;
Utf8ToUtf16le(input, output);
// Unfortunately codecvt is not yet supported by G++, othwerise:
// wstring_convert<std::codecvt_utf8_utf16<char16_t>,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;
}