BSTR camutilsctrl::get_term_ip (void)
{
Afx_manage_state (AfxGetStaticModuleState ());
CString strresult;
TODO: Add the Dispatch handler code here
Wsadata Wsadata;
WORD sockversion = Makeword (2,2);
Initializing the socket environment
if (:: WSAStartup (sockversion,&wsadata)!=0)
{
Tracelog3 (Log_info, "WSAStartup failed. Errorcode=%d ", GetLastError ());
Exit (0);
}
Get host Name
Char szhost[256]={0};
:: GetHostName (szhost,256);
Tracelog3 (Log_info, "host name =%s", Szhost);
Get the network information under the host name
Hostent *phost =:: gethostbyname (Szhost);//Returns a pointer to the hostent structure that contains host name and address information for the specified host name
/*struct hostent
{
Char *h_name;//official host name
char * * h_aliases;//host Alias
Short h_addrtype;//host IP address type: ipv4-af_inet
Short h_length;//host I ticket address byte length, IPV4 is 4 bytes, or 32 bits
char * * h_addr_list;//host's IP address list
};*/
Processing the acquired network information
IN_ADDR addr;
CString Strtmp,strbuf;
for (int i=0;; i++)
{
Char *p = phost->h_addr_list[i];
if (p==null)
Break
memcpy (&addr. S_un. S_addr,p,phost->h_length);
Char *SLZP =:: Inet_ntoa (addr);//convert sin_addr stored IP (value) to string form (127.0.0.1)
Strbuf = SLZP;
Strtmp.append ((strbuf+ "|"));
}
Shutting down the socket environment
:: WSACleanup ();
Strtmp.trimright ("|");
Evtfsnmessage (strtmp);
strresult = strtmp;
return strresult.allocsysstring ();
}
Get the native IP address in C + + under Windows