大家可以写一个通用类,把它放到成员函数中,要用的时候直接调用就行了,下面是我自己写的代码
void CommonWnd::GethostIP()
{
//得到本机名称
char hostName[256];
gethostname(hostName,256);
//得到本机IP
char hostAddress[256];
HOSTENT *hostEntry;
hostEntry=gethostbyname(hostName);
wsprintf(hostAddress , "%d.%d.%d.%d" ,
(hostEntry->h_addr_list[0][0]&0x00ff) ,
(hostEntry->h_addr_list[0][1]&0x00ff) ,
(hostEntry->h_addr_list[0][2]&0x00ff) ,
(hostEntry->h_addr_list[0][3]&0x00ff)
);
m_localIP.m_strIP.Format(_T("%s"),CString(hostAddress));
m_localIP.Hasset = TRUE;
}