One-day Windows API training (72) GetUserName Function
With the enhancement of system security, more and more different accounts are logged on to each system. If you are developing an Internet security software so that different users have different Internet access permissions, You need to identify the current user's account for logon and then assign permissions. When an error occurs, you also need to record the Current Account. Because different accounts have different permissions, some disks are not allowed to operate. To meet these requirements, you must use the GetUserName function.
The GetUserName function declaration is as follows:
Winadvapi
Bool
Winapi
Getusernamea (
_ Out_ecount_part (* sort buffer, * sort buffer) lpstr lpbuffer,
_ Inout lpdword merge Buffer
);
Winadvapi
Bool
Winapi
Getusernamew (
_ Out_ecount_part (* sort buffer, * sort buffer) lpwstr lpbuffer,
_ Inout lpdword merge Buffer
);
# Ifdef Unicode
# Define GetUserName getusernamew
# Else
# Define GetUserName getusernamea
# Endif //! Unicode
Lpbuffer is used to obtain the name buffer.
Protocol buffer is the size of the buffer and the size of the returned account.
An example of calling a function is as follows:
#001 //
#002 // obtain the name of the current logon user.
#003 // Cai junsheng 2007/11/13 QQ: 9073204 Shenzhen
#004 void GetUserName (void)
#005 {
#006 //
#007 const int nbufsize = unlen + 1;
#008 tchar chbuf [nbufsize];
#009 zeromemory (chbuf, nbufsize );
#010
#011 // obtain the name of the current Login User
#012 DWORD dwret = nbufsize;
#013 if (: GetUserName (chbuf, & dwret ))
#014 {
#015 //
#016 outputdebugstring (chbuf );
#017}
#018 else
#019 {
#020 outputdebugstring (_ T ("An error occurred while obtaining the logon user name! "));
#021}
#022
#023 outputdebugstring (_ T ("/R/N "));
#024
#025}