// 控制臺程序獲取本機外網IP 並更新文件
// Code By PE_luck
// 百度:/question/74236874.html
/*********************************************/
#include <windows.h>
#include <Wininet.h>
#include <stdio.h>
#pragma comment(lib,"Wininet.lib")
void update()
{
printf("讀取IP數據...\n");
HINTERNET hnet= InternetOpen("PE_luck",PRE_CONFIG_INTERNET_ACCESS,NULL,INTERNET_INVALID_PORT_NUMBER,0);
HINTERNET hopen= InternetOpenUrl(hnet,"/ip2city.asp",NULL,0,INTERNET_FLAG_RELOAD,0);
CHAR buff[1024*100]={0};
DWORD dwRead=0;
InternetReadFile(hopen,buff,sizeof(buff),&dwRead);
CHAR *str=new CHAR [dwRead];
memset(str,0,dwRead);
buff[dwRead]=0;
strcpy(str,buff);
int len=strlen(str);
for (int k=0;k<len;k++)
{
if (str[k]=='[')
{
break;
}
}
k++;
for (int j=0;j<len;j++)
{
if (str[j]==']')
{
break;
}
}
j++;
char ip[30]={0};
for (int q=0;q<j-k-1;q++)
{
ip[q]=str[k+q];
}
FILE *pfile=NULL;
printf("寫入IP數據...\n");
pfile=fopen("D:\\CS.txt","w");
char strfile[100]={0};
sprintf(strfile,"%s %s %s","127.0.0.1",ip,"27015");
fwrite(strfile,sizeof(char),strlen(strfile),pfile);
fclose(pfile);
printf("IP:%s...\n",ip);
CloseHandle(hnet);
CloseHandle(hopen);
}
void CALLBACK TimerProc(
HWND hwnd,
UINT uMsg,
UINT idEvent,
DWORD dwTime )
{
update();
}
void main()
{
printf("已經啟動IP更新...每分鐘更新壹次...\n");
SetTimer(NULL,1,1000*60,(TIMERPROC)TimerProc);
MSG msg;
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}