1.2.3 test tool-programming to obtain the IP address and name of a computer (1)
Instance function to obtain the IP address and computer name of the current computer
Source code path CD \ Yuanma \ 1 \ IP
The purpose of this example is to use visual c ++ 6.0 to develop an application for obtaining the IP address and computer name of the current machine.
1. design the MFC form
After creating an MFC project using Visual C ++ 6.0, design two forms based on the needs of this instance, namely idd_aboutbox form (see Figure 1-12) and idd_ipaddress_dialog form (see Figure 1-13 ).
|
Figure 1-12 idd_aboutbox form |
|
Figure 1-13 idd_ipaddress_dialog form |
2. Specific Encoding
After designing the form, we will start to explain the specific encoding process.
(1) Implement the initialization dialog box in the file ipaddressdlg. cpp, and display the obtained IP address and computer name in the dialog box. The Code is as follows:
- Bool cipaddressdlg: oninitdialog ()
- {
- Cdialog: oninitdialog ();
- Assert (idm_aboutbox & 0xfff0) = idm_aboutbox );
- Assert (idm_aboutbox <0xf000 );
- Cmenu * psysmenu = getsystemmenu (false );
- If (psysmenu! = NULL)
- {
- Cstring straboutmenu;
- Straboutmenu. loadstring (ids_aboutbox );
- If (! Straboutmenu. isempty ())
- {
- Psysmenu-> appendmenu (mf_separator );
- Psysmenu-> appendmenu (mf_string, idm_aboutbox, straboutmenu );
- }
- }
- // Set the dialog box icon
- Seticon (m_hicon, true); // you can specify a large icon.
- Seticon (m_hicon, false); // you can specify a small icon.
- Int nretcode;
-
- Nretcode = startup ();
- Trace1 ("Startup retcode: % d \ n", nretcode );
- Nretcode = getlocalhostname (m_shostname );
- Trace1 ("getlocalhostname retcode: % d \ n", nretcode );
- Nretcode = getipaddress (m_shostname, m_sipaddress );
- Trace1 ("getipaddress retcode: % d \ n", nretcode );
- Nretcode = cleanup ();
- Trace1 ("cleanup retcode: % d \ n", nretcode );
- Updatedata (false );
- Return true;
- }
-
- Void cipaddressdlg: onsyscommand (uint NID, lparam)
- {
- If (NID & 0xfff0) = idm_aboutbox)
- {
- Caboutdlg dlgabout;
- Dlgabout. domodal ();
- }
- Else
- {
- Cdialog: onsyscommand (NID, lparam );
- }
- }
- Void cipaddressdlg: onpaint ()
- {
- If (isiconic ())
- {
- Cpaintdc DC (this); // device context for painting
-
- Sendmessage (wm_iconerasebkgnd, (wparam) DC. getsafehdc (), 0 );
- Int cxicon = getsystemmetrics (sm_cxicon );
- Int cyicon = getsystemmetrics (sm_cyicon );
- Crect rect;
- Getclientrect (& rect );
- Int x = (rect. Width ()-cxicon + 1)/2;
- Int y = (rect. Height ()-cyicon + 1)/2;
- DC. drawicon (X, Y, m_hicon );
- }
- Else
- {
- Cdialog: onpaint ();
- }
- }
(2) In the file ipaddressdlg. cpp, write the function getlocalhostname () to get the machine name and call the function getipaddress () to get the IP address of the machine. The Code is as follows:
- Int cipaddressdlg: getlocalhostname (cstring & shostname)
- {
- Char szhostname [256];
- Int nretcode;
- Nretcode = gethostname (szhostname, sizeof (szhostname ));
- If (nretcode! = 0 ){
- Shostname = _ T ("not available ");;
- Return wsagetlasterror ();
- }
- Shostname = szhostname;
- Return 0;
- }
-
- Int cipaddressdlg: getipaddress (const cstring & shostname,
- Cstring & sipaddress)
- {
- Struct hostent far * lphostent = gethostbyname (shostname );
- If (lphostent = NULL ){
- Sipaddress = _ T ("");
- Return wsagetlasterror ();
- }
- Lpstr lpaddr = lphostent-> h_addr_list [0];
- If (lpaddr ){
- Struct in_addr inaddr;
- Memmove (& inaddr, lpaddr, 4 );
- Sipaddress = inet_ntoa (inaddr );
- If (sipaddress. isempty ())
- Sipaddress = _ T ("not available ");
- }
-
- Return 0;
- }
(3) load the Winsock library in the file ipaddressdlg. cpp and release the control. The specific code is as follows:
- Int cipaddressdlg: startup ()
- {
- Word wversionrequested;
- Wsadata;
- Int err;
- Wversionrequested = makeword (2, 0 );
- Err = wsastartup (wversionrequested, & wsadata );
- If (Err! = 0 ){
- Return err;
- }
- If (lobyte (wsadata. wversion )! = 2
- | Hibyte (wsadata. wversion )! = 0 ){
- Wsacleanup ();
- Return wsavernotsupported;
- }
- Return 0;
- }
At this point, the main modules of the entire instance are introduced. After execution, the machine name and IP address are obtained, as shown in figure 1-14.
|
Figure 1-14 execution results |
Test Tool-programming to obtain the IP address and computer name of a computer