C + + access to Linux and Windows system user name

Source: Internet
Author: User

(1) Obtain the user name of Linux system:
Through struct passwd *getpwuid (uid_t uid) function; This function returns the passwd structure, which contains the User name field
struct passwd {char *pw_name;  /*user name */char *pw_passwd;  /*user Password * * uid_t pw_uid;  /*user ID */gid_t pw_gid;  /*group ID */char *pw_gecos;  /*user real name */char *pw_dir;  /*home Directory * * Char *pw_shell; /*shell program */};
(2) Obtain the user name of the Windows system:
Through the GetUserName (buffer, &len) function

(3) Complete instance (cross platform):
[CPP]  View plain  copy #include  <iostream>   #include  <string>      #ifdef  linux        #include  <unistd.h>        #include  <pwd.h>   #endif       #ifdef  _ win32        #include <Windows.h>   #endif       Std::string getusername ()    {   #if  defined linux   //linux  system       uid_t userid;       struct  passwd* pwd;       userid=getuid ();        pwd=getpwuid (userid);       return pwd->pw_name;       #elif  defined _WIN32  //windows system       const  int max_len = 100;       char szBuffer[MAX_LEN];       DWORD len = MAX_LEN;       if (  GetUserName (Szbuffer, &len)  )      //username is saved in Szbuffer, Len is the length of user name             return szBuffer;      #else    //outher system       return  "";   #endif   }       int main ()    {       std::string ss  = getusername ();       std::cout << ss <<  std::endl;       return 0;  }   (4) GCC compile run screenshot on Linux system
(5) Linux under the four simple function introduction: Getpid, Getppid, Getuid, Getgid

#include <stdio.h>  
  
int main ()  
{  
    printf ("pid:%d, ppid:%d, uid:%d, gid:%d\n", Getpid (), Getppid (), Getuid (), Getgid ());  
    return 0;  
}  
The result is: [Taoge@localhost learn_c]$ Echo $$
2774
[Taoge@localhost learn_c]$ ID
uid=501 (Taoge) gid=502 (taoge) groups=502 (Taoge), 501 (embed)
[Taoge@localhost learn_c]$./a.out
pid:2898, ppid:2774, uid:501, gid:502
[Taoge@localhost learn_c]$ su Root
Password:
[Root@localhost learn_c]# Echo &&
>
[Root@localhost learn_c]# ID
Uid=0 (Root) gid=0 (root) groups=0 (root), 1 (BIN), 2 (daemon), 3 (SYS), 4 (ADM), 6 (disk), (wheel)
[Root@localhost learn_c]#./a.out
pid:2922, ppid:2905, uid:0, gid:0
[Root@localhost learn_c]# su Taoge
[Taoge@localhost learn_c]$ Echo $$
2928
[Taoge@localhost learn_c]$ ID
uid=501 (Taoge) gid=502 (taoge) groups=502 (Taoge), 501 (embed)
[Taoge@localhost learn_c]$./a.out
pid:2946, ppid:2928, uid:501, gid:502
[Taoge@localhost learn_c]$
 #include <unistd.h> #include <pwd.h> void Getuserandpid () {uid_t userid;
  struct passwd* pwd;
  Userid=getuid ();
  Pwd=getpwuid (userid);
  FILE *FP = fopen ("/home/stamp_temp/ao.log", "ab+");
  fprintf (FP, "user:%s, pid:%d, ppid:%d, uid:%d, gid:%d\n", Pwd->pw_name, Getpid (), Getppid (), Getuid (), Getgid ());
Fclose (FP); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.