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.