From 5da85487b8547e3cef8d7fccea4d1f4f3d47c7ff Mon Sep 17 00:00:00 2001 From: KaiN Date: Sat, 27 Aug 2022 10:57:14 +0200 Subject: [PATCH] Use GetUserNameExW to query user name on windows --- libs/common/CMakeLists.txt | 2 +- libs/common/src/System_Win32.cpp | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/libs/common/CMakeLists.txt b/libs/common/CMakeLists.txt index 9ca7285..bcdfef1 100644 --- a/libs/common/CMakeLists.txt +++ b/libs/common/CMakeLists.txt @@ -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) diff --git a/libs/common/src/System_Win32.cpp b/libs/common/src/System_Win32.cpp index a5f7eac..1a19d59 100644 --- a/libs/common/src/System_Win32.cpp +++ b/libs/common/src/System_Win32.cpp @@ -6,6 +6,10 @@ #include #include #include +#ifndef SECURITY_WIN32 +# define SECURITY_WIN32 +#endif +#include #ifdef _MSC_VER # pragma warning(push) # pragma warning(disable : 4091) // "typedef ": Ignoriert auf der linken Seite von "tagGPFIDL_FLAGS" @@ -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 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())); }