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}