Obtain the local IP address and display it in the IP Control.

Source: Internet
Author: User

Char shostname [100];
Gethostname (shostname, sizeof (shostname ));
Struct hostent far * lphostent = gethostbyname (shostname );
If (lphostent = NULL)
{
MessageBox (_ T ("error "));
}
// Obtain the IP address
Lpstr lpaddr = lphostent-> h_addr_list [0];
If (lpaddr)
{
Struct in_addr inaddr;
Memmove (& inaddr, lpaddr, 4 );

Cstring STR;
STR = inet_ntoa (inaddr );
M_edit = STR;
Updatedata (0 );

DWORD dw1 = inet_addr (inet_ntoa (inaddr ));
Word wd1 = loword (dw1); // obtain the 16-digit height.
Word WD2 = hiword (dw1); // retrieve 16 lower bits
Byte bte1 = lobyte (wd1 );
Byte bte2 = hibyte (wd1 );
Byte bte3 = lobyte (WD2 );
Byte bte4 = hibyte (WD2 );

M_ip.setaddress (bte1, bte2, bte3, bte4 );

// M_ip.setaddress (inet_addr (inet_ntoa (inaddr )));

}

 

The obtained IP address is first converted to cstring and then to DWORD. If DWORD is directly displayed in IP control at this time, it will be an incorrect order of IP addresses. The order of low and high levels is the opposite. The local IP address can be correctly displayed only when the data is taken out and put in byte, and then displayed in IP Control in ascending order.

This program is successfully debugged in the vs2008.net SP1 environment.

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.