Windows API one-day training (71) getcomputername Function

Source: Internet
Author: User
Windows API one-day training (71) getcomputername Function

How do you differentiate a large company from a network administrator who finds that you need to manage thousands of computers? It must be the name of the computer. The network administrator needs you to develop a set of software that can automatically report the names of all computers to him. To meet this requirement, you can use the following function getcomputername to obtain the name of the computer and transmit it to the Administrator over the network.

The getcomputername function declaration is as follows:

Winbaseapi
Bool
Winapi
Getcomputernamea (
_ Out_ecount_part (* nsize, * nsize + 1) lpstr lpbuffer,
_ Inout lpdword nsize
);
Winbaseapi
Bool
Winapi
Getcomputernamew (
_ Out_ecount_part (* nsize, * nsize + 1) lpwstr lpbuffer,
_ Inout lpdword nsize
);
# Ifdef Unicode
# Define getcomputername getcomputernamew
# Else
# Define getcomputername getcomputernamea
# Endif //! Unicode

Lpbuffer is the buffer for obtaining computer names.
Nsize is the size of the input buffer and the size of the output computer name.

An example of calling a function is as follows:
#001 //
#002 // obtain the name of the current computer.
#003 // Cai junsheng 2007/11/12 QQ: 9073204 Shenzhen
#004 void getpcname (void)
#005 {
#006 //
#007 const int nbufsize = max_computername_length + 1;
#008 tchar chbuf [nbufsize];
#009 zeromemory (chbuf, nbufsize );
#010
#011 // obtain the name of the current computer
#012 DWORD dwret = nbufsize;
#013 if (getcomputername (chbuf, & dwret ))
#014 {
#015 //
#016 outputdebugstring (chbuf );
#017}
#018 else
#019 {
#020 outputdebugstring (_ T ("An error occurred while obtaining the computing name! "));
#021}
#022
#023 outputdebugstring (_ T ("/R/N "));
#024
#025}

Related Article

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.