How can I get the username of the process owner (the user who is executing my program) in C++?
On Mac OSX:
getenv("USER");
On Linux:
getenv("USERNAME");
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 */
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With