24

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?

Andrew
  • 479
  • 1
  • 5
  • 13
  • 3
    You should not use environment variables to retrieve the username. It is not guaranteed to exist. In general, outside of a BAT file you should not use environment variables. – user7116 Jul 20 '12 at 22:07
  • If you must use an environment variable to retrieve the username, the correct one is `USERNAME` not `LOGNAME`. You may also want `USERDOMAIN`. – Ferruccio Dec 31 '13 at 13:23
  • 1
    Also, this might help you: http://www.cplusplus.com/forum/beginner/12076/ – Chefire Jul 20 '12 at 21:58

8 Answers8

60

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);
orlp
  • 112,504
  • 36
  • 218
  • 315
  • 2
    +1, you can use [GetUserNameEx if you'd like to control the format of the username](http://msdn.microsoft.com/en-us/library/windows/desktop/ms724268(v=vs.85).aspx) instead of what was entered by the user. You should use `UNLEN+1` from `Lmcons.h` – user7116 Jul 20 '12 at 21:58
  • @sixlettervariables: thanks, I edited. Do you by any chance know whether or not `` includes ``? – orlp Jul 20 '12 at 22:03
  • I'm on my Mac, but if memory serves the answer is No. [LANMAN headers need to be brought in on separately, and I think this example agrees](http://msdn.microsoft.com/en-us/library/windows/desktop/ms676907%28v=vs.85%29.aspx). – user7116 Jul 20 '12 at 22:06
  • I am trying to use this for a file path. Is there any shortcut or are functions always necessary? – Andrew Jul 20 '12 at 22:13
  • @Andrew: I'm sorry, I don't see what the problem is with the code I posted. Exactly what file path are you trying to construct? – orlp Jul 20 '12 at 22:16
  • Putting the current username into a file path is almost always the wrong thing to do. What are you actually trying to achieve? – Harry Johnston Jul 20 '12 at 22:17
  • I am using `ifstream` and `ofstream` to copy a file. @nightcracker what is the variable `UNLEN`? – Andrew Jul 20 '12 at 22:20
  • @Andrew: `UNLEN` is a macro defined by one of the standard Windows headers (specifically ``) that gets replaced with the maximum username length. This is more accurate and portable than simply coming up with a random number. – orlp Jul 20 '12 at 22:21
  • @nightcracker, I checked my windows.h, and it doesn't include it. – chris Jul 20 '12 at 22:22
  • Where is the file you're trying to copy? Why do you think you need the username? – Harry Johnston Jul 20 '12 at 22:54
  • @HarryJohnston A simple file on the desktop. For the path. – Andrew Jul 21 '12 at 00:13
  • 3
    The user's desktop isn't always in c:\Users\\Desktop. You should use the SHGetFolderPath or SHGetKnownFolderPath function, depending on which version(s) of Windows you're writing for. – Harry Johnston Jul 21 '12 at 05:05
  • 1
    This will not work as written. The second parameter needs to be a pointer to the size of the `username` buffer. When `GetUserName` returns the size will be replaced with the number of characters actually written into the `username` buffer. – Ferruccio Dec 31 '13 at 13:21
  • I used this `GetUserName` function but what I get was the old username. I changed my user name (control panel -> user accounts) to a new one but the old one was retrieved instead. – ReignBough Apr 15 '15 at 06:53
9

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

jyz
  • 6,011
  • 3
  • 29
  • 37
6

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";
}
Anna Eurich
  • 61
  • 1
  • 1
5

On windows use USERNAME enviroment variable or GetUserName function

parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85
3

In case GetUserName(username, &username_len); doesn't work when using a char[] as storage, you should know that GetUserName could be a macro solved to GetUserNameW. If u worked with Windows API probably u know the difference between W and A. Using GetUserNameA would solve the problem if you're using char[] as buf

uIM7AI9S
  • 353
  • 4
  • 13
1

By using getenv() function:

char * user = getenv("username");
cout << string(user) << endl;

If you want to convert into string then use:

string userStr = string(user);
Skulaurun Mrusal
  • 2,792
  • 1
  • 15
  • 29
0
void funtions::_GetUserNameA()

char username[1024];
DWORD username_len = 1024;
GetUserNameA(username,&username_len);
std::cout <<"username:"<< username <<"\n";
-2

You should use the env variable USERNAME.

Van Zuzu
  • 31
  • 2
  • 9
    The %username% env variable is not safe. If you use that the user can open a cmd line and do `set username=SomeOneElse launchYourApp` and he'll effectively be running your app as SomeOneElse. This can be a security vulnerability. – MathKid Jan 21 '15 at 17:21