Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use GetUserNameExW to query user name on windows #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ IF (WIN32)
target_compile_definitions(s25util_common PRIVATE WIN32_LEAN_AND_MEAN _WIN32_WINNT=0x0600) # Win Vista
# Exclude some windows defines
target_compile_definitions(s25util_common PRIVATE NOMINMAX NODRAWTEXT NOSOUND NOTEXTMETRIC NOCOMM NOMCX)
target_link_libraries(s25util_common PUBLIC Boost::locale Boost::disable_autolinking iphlpapi)
target_link_libraries(s25util_common PUBLIC Boost::locale Boost::disable_autolinking iphlpapi secur32)
ENDIF ()

include(EnableWarnings)
Expand Down
10 changes: 7 additions & 3 deletions libs/common/src/System_Win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include <boost/nowide/convert.hpp>
#include <windows.h>
#include <shellapi.h>
#ifndef SECURITY_WIN32
# define SECURITY_WIN32
#endif
#include <security.h>
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable : 4091) // "typedef ": Ignoriert auf der linken Seite von "tagGPFIDL_FLAGS"
Expand Down Expand Up @@ -112,14 +116,14 @@ std::string System::getUserName()
{
DWORD nameLen = 0;
// Query required size
GetUserNameW(nullptr, &nameLen);
GetUserNameExW(EXTENDED_NAME_FORMAT::NameDisplay, nullptr, &nameLen); // nameLen contains terminating 0 here
if(nameLen == 0)
throw std::runtime_error("Could not query username length");
std::vector<wchar_t> userName(nameLen);
if(GetUserNameW(userName.data(), &nameLen) == 0)
if(GetUserNameExW(EXTENDED_NAME_FORMAT::NameDisplay, userName.data(), &nameLen) == 0)
throw std::runtime_error("Could not get username");

userName.resize(nameLen - 1); // nameLen already contains terminating 0
userName.resize(nameLen); // nameLen doesn't contain terminating 0 here

return boost::nowide::narrow(std::wstring(userName.begin(), userName.end()));
}