-
-
[旧帖]
[原创]获取外网IP(C和mfc两种实现)
0.00雪花
-
发表于:
2012-3-28 11:21
3375
-
[旧帖] [原创]获取外网IP(C和mfc两种实现)
0.00雪花
c:
# include <windows.h>
# include <urlmon.h>
# pragma comment(lib,"URLMON.lib")
void main()
{
URLDownloadToFile(NULL,"cc3K9s2c8@1M7q4)9K6b7g2)9J5c8W2)9J5c8Y4N6%4N6#2)9J5k6h3W2H3x3e0x3^5i4K6u0W2j5$3!0E0i4K6u0r3K9i4l9J5j5$3W2@1P5g2)9J5k6h3q4K6M7l9`.`.","ip.txt",0,NULL);
system("notepad ip.txt");
}
#include<urlmon.h>
#pragma comment (lib,"Urlmon.lib")
char buf[MAX_PATH] = {0};
char chTempIp[128];
char chIP[64];
URLDownloadToFile(0,"d64K9s2c8@1M7q4)9K6b7g2)9J5c8W2)9J5c8Y4N6%4N6#2)9J5k6h3W2H3x3e0x3^5i4K6u0W2j5$3!0E0i4K6u0r3K9i4l9J5j5$3W2@1P5g2)9J5k6h3q4K6M7l9`.`.","c:\\1.ini",0,NULL);
FILE *fp=fopen( "c:\\1.ini", "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<nBuflen; i++)
{
chIP[i] = chTempIp[i+1];
if(chTempIp[i] == ']')
{
chIP[i-1] = '\0';
DeleteFile("c:\\1.ini");
continue;
}
}
}
}
MessageBox(chIP);
MFC:
#include <afxinet.h>
void CLanChatDlg::GetNetIP()
{
SetDlgItemText(IDC_NET_IP,"正在获取外网IP");
CString strsource;
CString Address;
CInternetSession mySession(NULL,0);
CHttpFile* myHttpFile=NULL;
Address="http://f94K9s2c8@1M7q4)9K6b7g2)9J5c8W2)9J5c8Y4N6%4N6#2)9J5k6h3W2H3x3e0x3^5i4K6u0W2j5$3!0E0i4K6u0r3K9i4l9J5j5$3W2@1P5g2)9J5k6h3q4K6M7l9`.`.";//ip138网页
myHttpFile=(CHttpFile*)mySession.OpenURL(Address);//读取网络地址
while(myHttpFile->ReadString(strsource))
{ //循环读取下载来的网页文本
// AddToLog(strsource);
int begin=0;
begin=strsource.Find("[",0);
if(begin!=-1)//如果找到"[", 则找"]" 中括号内的文本则是 你的外网ip
{ int end=strsource.Find("]");
m_internetip=strsource.Mid(begin+1,end-begin-1);//提取外网ip
SetDlgItemText(IDC_NET_IP,m_internetip);//在左下角显示外网ip
}
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课