Files
PowerShell/impl/getusername.cpp
T

33 lines
641 B
C++
Raw Normal View History

2015-06-16 14:02:19 -07:00
#include "getusername.h"
2015-07-01 16:24:36 -07:00
#include <errno.h>
#include <langinfo.h>
#include <locale.h>
2015-06-16 14:02:19 -07:00
#include <unistd.h>
2015-07-01 16:24:36 -07:00
#include <string>
2015-06-16 14:02:19 -07:00
2015-07-01 16:24:36 -07:00
using namespace std;
const string utf8 = "UTF-8";
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724432(v=vs.85).aspx
// Sets errno to:
// ERROR_BAD_ENVIRONMENT - locale is not UTF-8
//
// Returns:
// 1 - succeeded
// 0 - failed
2015-07-01 16:21:52 -07:00
BOOL GetUserName(WCHAR_T* lpBuffer, LPDWORD lpnSize)
2015-06-16 14:02:19 -07:00
{
2015-07-01 16:24:36 -07:00
errno = 0;
// Select locale from environment
setlocale(LC_ALL, "");
// Check that locale is UTF-8
if (nl_langinfo(CODESET) != utf8) {
errno = ERROR_BAD_ENVIRONMENT;
return 0;
}
return 1;
2015-06-16 14:02:19 -07:00
}