I am developing an application. One of the methods needs to capture the computer name and user logged on the machine, then display both to the user. I need it to run on both Win
in Linux you can also use the following using Posix library to retrieve the real user that owns the process: getuid() returns the real user ID of the calling process. see getuid man page
#include
string userName = "unknownUser";
// Structure to store user info
struct passwd p;
// Get user ID of the application
uid_t uid = getuid();
// Buffer that contains password additional information
char pwdBuffer[ourPwdBufferSize];
// Temporary structure for reentrant function
struct passwd* tempPwdPtr;
if ((getpwuid_r(uid, &p, pwdBuffer, sizeof(pwdBuffer),
&tempPwdPtr)) == 0) {
userName = p.pw_name;
}