该函数通过http://city.ip138.com/city0.asp获取外网IP,获取成功则返回外网IP,失败则返回"fail",在VC6.0MFC框架
CString GetIPAddress()
{
char buf[MAX_PATH] = {0};
int first = 0;
int last = 0;
CString strPath = ".\\netip.ini";
URLDownloadToFile(0,"http://city.ip138.com/city0.asp",strPath,0,NULL);
FILE *fp=fopen( strPath, "r" );
if( fp != NULL )
{
fseek(fp, 0, SEEK_SET);
fread(buf,1,MAX_PATH,fp);
fclose(fp);
CString strAsp(buf);
first = strAsp.Find('[');
last = strAsp.Find(']');
CString sSubStr = strAsp.Mid(first+1,last-first-1);
return sSubStr;
}
else
{
fclose(fp);
fp = NULL;
return "fail";
}
}
下测试成功。