Get Computer Name and logged user name

前端 未结 6 1443
走了就别回头了
走了就别回头了 2021-02-07 01:59

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

6条回答
  •  滥情空心
    2021-02-07 02:30

    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;
    }
    

提交回复
热议问题