Get current username in C++ on Windows
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);
On windows use USERNAME enviroment variable or GetUserName function
It works:
#include <iostream>
using namespace std;
#include <windows.h>
#include <Lmcons.h>
int main()
{
TCHAR name [ UNLEN + 1 ];
DWORD size = UNLEN + 1;
if (GetUserName( (TCHAR*)name, &size ))
wcout << L"Hello, " << name << L"!\n";
else
cout << "Hello, unnamed person!\n";
}
Corrected code that worked for me:
TCHAR username[UNLEN + 1];
DWORD size = UNLEN + 1;
GetUserName((TCHAR*)username, &size);
I'm using Visual Studio Express 2012 (on Windows 7), maybe it works the same way with Dev-Cpp