One-day Windows API training (72) GetUserName Function

Source: Internet
Author: User
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}

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.