if (gethostname(szHostname, sizeof(szHostname))) { TRACE(_T("Failed in call to gethostname, WSAGetLastError returns %d\n"), WSAGetLastError()); return FALSE; }
//get host information from the host name HOSTENT* pHostEnt = gethostbyname(szHostname); if (pHostEnt == NULL) { TRACE(_T("Failed in call to gethostbyname, WSAGetLastError returns %d\n"), WSAGetLastError()); return FALSE; }
//check the length of the IP adress if (pHostEnt->h_length != 4) { TRACE(_T("IP address returned is not 32 bits !!\n")); return FALSE; }
//call the virtual callback function in a loop int nAdapter = 0; in_addr address; CopyMemory(&address.S_un.S_addr, pHostEnt->h_addr_list[nAdapter], pHostEnt->h_length); //bContinue = EnumCallbackFunction(nAdapter, address);
CString str;
if(gethostname (hostname,128)==0) //得到主机名 返回0表示成功
{
strHostName=hostname; //保存主机名
m_HostName.SetWindowText (strHostName); //显示主机名 //获得主机IP
struct hostent *hostIP;
int occurred;
hostIP=gethostbyname(hostname);
occurred=0;
int j,h_length=4;
for(j=0;j<h_length;j++)
{
CString string; if(j>0)
str+="."; string.Format ("%u",(unsigned int)((unsigned char *)hostIP->h_addr_list [occurred])[j]);
str+=string;
}
}
m_IP.SetWindowText (str); //显示主机IP
{
char szHostname[256];
if (gethostname(szHostname, sizeof(szHostname)))
{
TRACE(_T("Failed in call to gethostname, WSAGetLastError returns %d\n"), WSAGetLastError());
return FALSE;
}
//get host information from the host name HOSTENT* pHostEnt = gethostbyname(szHostname);
if (pHostEnt == NULL)
{
TRACE(_T("Failed in call to gethostbyname, WSAGetLastError returns %d\n"), WSAGetLastError());
return FALSE;
}
//check the length of the IP adress
if (pHostEnt->h_length != 4)
{
TRACE(_T("IP address returned is not 32 bits !!\n"));
return FALSE;
}
//call the virtual callback function in a loop
int nAdapter = 0;
in_addr address;
CopyMemory(&address.S_un.S_addr, pHostEnt->h_addr_list[nAdapter], pHostEnt->h_length);
//bContinue = EnumCallbackFunction(nAdapter, address);
sIP.Format(_T("%d.%d.%d.%d"), address.S_un.S_un_b.s_b1, address.S_un.S_un_b.s_b2, address.S_un.S_un_b.s_b3, address.S_un.S_un_b.s_b4);
// AfxMessageBox(sIP);
return TRUE;
}