views:

307

answers:

5

How can I get the username of the process owner (the user who is executing my program) in C++?

+2  A: 

IDK, but in a batch file you can use %username%. if its windows, and you only need to echo it, then

system("echo %username%");
i believe

hintss
I could use that but I was looking for more of an non-batch anwser, lol +1 for good anwser though!
H4cKL0rD
also works for %computername%
hintss
Jesus Christ, how does this have +3? All it does is echo the username, not return it, and this code wouldn't even compile!
Charlie Somerville
You can also modify environment variables. I can do `set USERNAME=foo` whenever I want, but it doesn't change who I'm logged in as.
Graeme Perrow
add quotes? did i miss those?i used C++ a while back, but before that, I wrote many, many, overcomplicated batch files. Who Am I, an installer, and a copy protection scheme were just the beginingleme edit
hintss
+1  A: 

This is specific to the operating system. On Windows, use GetUserName. On unix, use getuid.

Graeme Perrow
The question is about the local user name, not the computer name.
Matthew Flaschen
This is the machine name not local user/logged on user.
BobbyShaftoe
The original question was not clear. I've fixed my answer.
Graeme Perrow
+17  A: 

Windows

GetUserName()

Example:

 char user_name[UNLEN+1];
 DWORD user_name_size = sizeof(user_name);
 if (GetUserName(user_name, &user_name_size))
     cout << "Your user name is: " << user_name << endl;
 else
     /* Handle error */

Linux

Look at getpwuid:

The getpwuid() function shall search the user database for an entry with a matching uid.

The getpwuid() function shall return a pointer to a struct passwd

The struct passwd will contain char *pw_name.

Use getuid to get the user id.

Andreas Bonini
Woah, 7 votes in 2 minutes.. Must be the new stuffed owl avatar :)
Andreas Bonini
+1 for being helpful in a detail-sparse situation!
just_wes
Got what I was saying and got one for the linux Users +1 and tyvm.
H4cKL0rD
If you want to hard-code a buffer size, use 257 (UNLEN+1) this is the documented max size for a username on windows
Anders
@Anders: thanks for pointing it out. I'll edit.
Andreas Bonini
A: 

It is not a C++ related question. You can find information (not 100% sure) in the environment variables when using UNIX like systems. You can also use the 'id' program on these systems.

In general, the fastest way is to make a platform-dependent kernel / API call.

On windows under cmd.exe the USERNAME environment variable holds the username (which is also informational not factual). Search in WINAPI documentation for precise.

Notinlist
@Notinlist: what do you mean it's not a C++ related question? He's using C++. This makes the question C++ related. Maybe you come from places where "C++ related" means only standard C++ (such as #[email protected]), but here on SO this is considered very in topic :)
Andreas Bonini
A: 

On windows, a thread can be impersonated, a process can not. To get the process owner you should call GetTokenInformation with the TokenUser infoclass on your process token, this will give you a SID, this SID can be converted to a username with LookupAccountSid. If you don't care about thread vs process, GetUserName() is fine.

Anders