Test Tool-programming to obtain the IP address and computer name of a computer

Source: Internet
Author: User

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:

  1. Bool cipaddressdlg: oninitdialog ()
  2. {
  3. Cdialog: oninitdialog ();
  4. Assert (idm_aboutbox & 0xfff0) = idm_aboutbox );
  5. Assert (idm_aboutbox <0xf000 );
  6. Cmenu * psysmenu = getsystemmenu (false );
  7. If (psysmenu! = NULL)
  8. {
  9. Cstring straboutmenu;
  10. Straboutmenu. loadstring (ids_aboutbox );
  11. If (! Straboutmenu. isempty ())
  12. {
  13. Psysmenu-> appendmenu (mf_separator );
  14. Psysmenu-> appendmenu (mf_string, idm_aboutbox, straboutmenu );
  15. }
  16. }
  17. // Set the dialog box icon
  18. Seticon (m_hicon, true); // you can specify a large icon.
  19. Seticon (m_hicon, false); // you can specify a small icon.
  20. Int nretcode;
  21.  
  22. Nretcode = startup ();
  23. Trace1 ("Startup retcode: % d \ n", nretcode );
  24. Nretcode = getlocalhostname (m_shostname );
  25. Trace1 ("getlocalhostname retcode: % d \ n", nretcode );
  26. Nretcode = getipaddress (m_shostname, m_sipaddress );
  27. Trace1 ("getipaddress retcode: % d \ n", nretcode );
  28. Nretcode = cleanup ();
  29. Trace1 ("cleanup retcode: % d \ n", nretcode );
  30. Updatedata (false );
  31. Return true;
  32. }
  33.  
  34. Void cipaddressdlg: onsyscommand (uint NID, lparam)
  35. {
  36. If (NID & 0xfff0) = idm_aboutbox)
  37. {
  38. Caboutdlg dlgabout;
  39. Dlgabout. domodal ();
  40. }
  41. Else
  42. {
  43. Cdialog: onsyscommand (NID, lparam );
  44. }
  45. }
  46. Void cipaddressdlg: onpaint ()
  47. {
  48. If (isiconic ())
  49. {
  50. Cpaintdc DC (this); // device context for painting
  51.  
  52. Sendmessage (wm_iconerasebkgnd, (wparam) DC. getsafehdc (), 0 );
  53. Int cxicon = getsystemmetrics (sm_cxicon );
  54. Int cyicon = getsystemmetrics (sm_cyicon );
  55. Crect rect;
  56. Getclientrect (& rect );
  57. Int x = (rect. Width ()-cxicon + 1)/2;
  58. Int y = (rect. Height ()-cyicon + 1)/2;
  59. DC. drawicon (X, Y, m_hicon );
  60. }
  61. Else
  62. {
  63. Cdialog: onpaint ();
  64. }
  65. }

(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:

  1. Int cipaddressdlg: getlocalhostname (cstring & shostname)
  2. {
  3. Char szhostname [256];
  4. Int nretcode;
  5. Nretcode = gethostname (szhostname, sizeof (szhostname ));
  6. If (nretcode! = 0 ){
  7. Shostname = _ T ("not available ");;
  8. Return wsagetlasterror ();
  9. }
  10. Shostname = szhostname;
  11. Return 0;
  12. }
  13.  
  14. Int cipaddressdlg: getipaddress (const cstring & shostname,
  15. Cstring & sipaddress)
  16. {
  17. Struct hostent far * lphostent = gethostbyname (shostname );
  18. If (lphostent = NULL ){
  19. Sipaddress = _ T ("");
  20. Return wsagetlasterror ();
  21. }
  22. Lpstr lpaddr = lphostent-> h_addr_list [0];
  23. If (lpaddr ){
  24. Struct in_addr inaddr;
  25. Memmove (& inaddr, lpaddr, 4 );
  26. Sipaddress = inet_ntoa (inaddr );
  27. If (sipaddress. isempty ())
  28. Sipaddress = _ T ("not available ");
  29. }
  30.  
  31. Return 0;
  32. }

 

(3) load the Winsock library in the file ipaddressdlg. cpp and release the control. The specific code is as follows:

  1. Int cipaddressdlg: startup ()
  2. {
  3. Word wversionrequested;
  4. Wsadata;
  5. Int err;
  6. Wversionrequested = makeword (2, 0 );
  7. Err = wsastartup (wversionrequested, & wsadata );
  8. If (Err! = 0 ){
  9. Return err;
  10. }
  11. If (lobyte (wsadata. wversion )! = 2
  12. | Hibyte (wsadata. wversion )! = 0 ){
  13. Wsacleanup ();
  14. Return wsavernotsupported;
  15. }
  16. Return 0;
  17. }

 

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

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.