代码如下:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "urlmon.h" //syradd
#include "stdio.h" //syradd
#include "winsock2.h" //syradd
#include <wchar.h>
#pragma link "D:\\Program Files\\Borland\\CBuilder6\\Lib\\Psdk\\urlmon.lib" //syradd
#pragma link "D:\\Program Files\\Borland\\CBuilder6\\Lib\\Psdk\\ws2_32.lib" //syradd
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{//获取公网IP
char Buf_File_Read[MAX_PATH] = {0};
char IP_Temp[128];
char IP_Host[64];
int Result_URLDownloadToFile = 1;
Result_URLDownloadToFile = URLDownloadToFile(0,"http://www.ip138.com/ip2city.asp","IPinTheWorld.ini",0,NULL);
if(Result_URLDownloadToFile != 0)//若URLDownloadToFile()成功,返回0
{
MessageBox(NULL, "网络故障,无法接入Internet,请联系网络管理员","提示",MB_ICONASTERISK);
return ;
}
FILE *fp_Download_URL_File=NULL;
if ((fp_Download_URL_File=fopen( "IPinTheWorld.ini","r")) == NULL)
{
MessageBox(NULL, "系统故障,无法打开IP文件,请检查防火墙设置或联系管理人员","提示",MB_ICONASTERISK);
return ;
}
if ( fp_Download_URL_File != NULL )
{
fseek(fp_Download_URL_File, 0, SEEK_SET);
fread(Buf_File_Read,1,256,fp_Download_URL_File);
fclose(fp_Download_URL_File);
char* Start_IP = strstr(Buf_File_Read,"[");
if (Start_IP)
{
sprintf( IP_Temp,"%s",Start_IP);
int Buf_Length = strlen(IP_Temp);
for (int i =0; i<Buf_Length; i++)
{
IP_Host[i] = IP_Temp[i+1];
if (IP_Temp[i] == ']')
{
IP_Host[i-1] = '\0';
DeleteFile("IPinTheWorld.ini");
continue;
}
}
}
}
AnsiString IP_World = IP_Host;
Memo1->Text = Memo1->Text + "\r\n公网IP:" + IP_World;
//MessageBox(NULL, chIP,chIP,0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{//获取本机IP
WSADATA Data_WSA;
int Result_WSAStartup = 1;
hostent *P_Host = NULL;
char Computer_Name[128] = "";
Result_WSAStartup = WSAStartup(MAKEWORD(2, 2), &Data_WSA);
if (Result_WSAStartup != 0)//无错返回0
{
MessageBox(NULL, "本机故障,无法启动网络服务,请联系网络管理员","提示",MB_ICONASTERISK);
return ;
}
gethostname(Computer_Name, 128);//获取指定计算机的名字
if(Computer_Name[0] == '\0')
{
MessageBox(NULL, "本机网络故障,无法接入局域网,请联系网络管理员","提示",MB_ICONASTERISK);
return ;
}
//Memo1->Text = s;
P_Host = gethostbyname(Computer_Name);
if(P_Host == NULL)
{
//Memo1->Text = "p NULL ";
MessageBox(NULL, "本机网络故障,无法获取IP,请联系管理员","提示",MB_ICONASTERISK);
return ;
}
AnsiString IP_Host = inet_ntoa(*((in_addr *)P_Host->h_addr));//获取指定计算机的IP地址
if(P_Host != NULL)
{delete P_Host;}
P_Host = NULL;
WSACleanup();//完成对WSAStartup()的调用
Memo1->Text = Memo1->Text + "\r\n局域网IP:" + IP_Host;
//MessageBox(NULL, HostIP,HostIP,0);
}
//---------------------------------------------------------------------------