#include <Winsock2.h>
char* GetLocalIP(void)
{
WSADATA wsaDataqq;
WSAStartup(MAKEWORD(1,1),&wsaDataqq);
PHOSTENT hostinfo;
char name[512] = {0};
in_addr dest;
char *ipAddr=NULL;
if(gethostname (name,sizeof(name)) != 0 || (hostinfo = gethostbyname(name)) == NULL)
return NULL;
for(int a=0; hostinfo->h_addr_list[a] != NULL ;a++)
{
memcpy(&dest, hostinfo->h_addr_list[a], hostinfo->h_length);
if (a==0){
ipAddr=inet_ntoa(dest);
}
}
WSACleanup();
return ipAddr;
}
CString GetInternetIP(){
#if(1)
CString checkWeb="http://only-997227-218-13-34-109.nstool.netease.com/";
CString tagStr="您的IP地址信息: ";
CString endChar=" ";
#else
CString checkWeb="http://www.ip138.com/ips1388.asp";
CString tagStr="您的IP地址是:[";
CString endChar="]";
#endif
HRESULT hr=URLDownloadToFileA(0, _T(checkWeb),_T("ipFile.txt"), 0,NULL);
if (hr!=S_OK){
return "";
}
CFile file;
file.Open(_T("ipFile.txt"),CFile::modeRead);
if (!file)
{
return "";
}
ULONGLONG len=file.GetLength();
char* buf=new char[len+1];
ZeroMemory(buf,len+1);
file.Read(buf,len);
buf[len]=0;
CString ans=buf;
CString ip="";
int ix=ans.Find(tagStr);
int endn=-1;
if (ix>-1)
{
len=tagStr.GetLength();
endn=ans.Find(endChar,ix+len);
if (endn>-1)
{
ip=ans.Mid(ix+len,endn-ix-len);
}
}
delete buf;
file.Close();
return ip;
}