VC获取主机名与外网IP(CString类型)

  • 发表于
  • VC

在VC下动态获取主机名与外网IP地址的C源码,有兴趣的可以看看!

VC动态获取主机名与外网IP

/************************************************************************/
/* 函数说明:获取当前外网IP地址 
/* 参数:无 
/* 返 回 值:返回外网IP地址、CString类型字符串
/* By:Koma 2009.08.21 14:12
/************************************************************************/
CString CQDlg::GetIPAddress() 
{ 
char buf[MAX_PATH] = {0}; 
char chTempIp[128]; 
char chIP[64]; 
CString strPath; 
strPath = GetExePath() + "//netip.ini"; 
URLDownloadToFile(0,"http://www.ip138.com/ip2city.asp",strPath,0,NULL); 
 
FILE *fp=fopen( strPath, "r" ); 
if ( fp != NULL )
{ 
fseek(fp, 0, SEEK_SET); 
fread(buf,1,256,fp); 
fclose(fp); 
char* iIndex = strstr(buf,"["); 
if (iIndex)
{ 
sprintf( chTempIp,"%s",iIndex); 
int nBuflen = strlen(chTempIp); 
for(int i =0; i