I am attempting to create a program that retrieves the current user's username on Windows using C++.
I tried this:
char *userName = getenv("LOGNAME");
stringstream ss;
string userNameString;
ss << userName;
ss >> userNameString;
cout << "Username: " << userNameString << endl;
Nothing is outputted except "Username:".
What is the simplest, best way to get the current username?
Use the Win32API GetUserName
function. Example:
#include <windows.h>
#include <Lmcons.h>
char username[UNLEN+1];
DWORD username_len = UNLEN+1;
GetUserName(username, &username_len);