前一章節提到的,在VC下動態擷取主機名稱與外網IP地址,有興趣的可以看看!
/************************************************************************/<br />/* 函數說明:擷取當前外網IP地址<br />/* 參 數:無<br />/* 返 回 值:返回外網IP地址、CString類型字串<br />/* By:Koma 2009.08.21 14:12<br />/************************************************************************/<br />CString CQDlg::GetIPAddress()<br />{<br /> char buf[MAX_PATH] = {0};<br /> char chTempIp[128];<br /> char chIP[64];<br /> CString strPath;<br /> strPath = GetExePath() + "//netip.ini";<br /> URLDownloadToFile(0,"http://www.ip138.com/ip2city.asp",strPath,0,NULL); </p><p> FILE *fp=fopen( strPath, "r" );<br /> if ( fp != NULL )<br /> {<br /> fseek(fp, 0, SEEK_SET);<br /> fread(buf,1,256,fp);<br /> fclose(fp);<br /> char* iIndex = strstr(buf,"[");<br /> if (iIndex)<br /> {<br /> sprintf( chTempIp,"%s",iIndex);<br /> int nBuflen = strlen(chTempIp);<br /> for(int i =0; i<nBuflen; i++)<br /> {<br /> chIP[i] = chTempIp[i+1]; </p><p> if(chTempIp[i] == ']')<br /> {<br /> chIP[i-1] = '/0';<br /> DeleteFile(strPath);<br /> continue;<br /> }<br /> }<br /> }<br /> }<br /> CString strTemp="";<br /> if(chIP != "")<br /> {<br /> strTemp.Format("%s",chIP);<br /> }<br /> return strTemp;<br />} </p><p>/************************************************************************/<br />/* 函數說明:擷取當前主機名稱<br />/* 參 數:無<br />/* 返 回 值:返回主機名稱、CString類型字串<br />/* By:Koma 2009.08.21 14:23<br />/************************************************************************/<br />CString CQDlg::GetHostName()<br />{<br /> WORD wVersionRequested;<br /> WSADATA wsaData;<br /> int err;<br /> wVersionRequested = MAKEWORD(2,0);<br /> err = WSAStartup(wVersionRequested,&wsaData);<br /> if(err != 0)<br /> {<br /> return err;<br /> }<br /> if(LOBYTE(wsaData.wVersion != 2) || HIBYTE(wsaData.wVersion) != 0)<br /> {<br /> WSACleanup();<br /> return (char)WSAVERNOTSUPPORTED;<br /> } </p><p> char szHostName[256];<br /> int nRetCode;<br /> CString str=""; </p><p> //調用API來獲得主機名稱<br /> nRetCode = gethostname(szHostName,sizeof(szHostName));<br /> if(nRetCode != 0)<br /> {<br /> return WSAGetLastError();<br /> }<br /> if(szHostName != "")<br /> {<br /> str.Format("主機名稱:%s",szHostName);<br /> }<br /> return str;<br />}