📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "iphlpapi.h"
#include "winsock.h"
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TIP *IP;
//---------------------------------------------------------------------------
__fastcall TIP::TIP(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void TIP::GetComputerAdapterList(void) //取本机网卡列表
{
AdapterList->Clear();
PIP_ADAPTER_INFO pAdapterInfo=NULL;
ULONG ulSize=0;
GetAdaptersInfo(pAdapterInfo,&ulSize);
pAdapterInfo=(PIP_ADAPTER_INFO)new char[ulSize];
GetAdaptersInfo(pAdapterInfo,&ulSize);
if(AdapterListIndex==NULL)
AdapterListIndex=new TStringList;
else AdapterListIndex->Clear();
while(pAdapterInfo)
{
AdapterListIndex->Add(IntToStr(pAdapterInfo->Index));
AdapterList->Items->Add(Trim((AnsiString)pAdapterInfo->Description));
// Caption=pAdapterInfo->AdapterName;
pAdapterInfo=pAdapterInfo->Next;
}
delete pAdapterInfo;
if(AdapterList->Items->Count>0)
{
AdapterList->ItemIndex=0;
GetIpAddressWithIndex(AdapterList->ItemIndex);
}
else Application->MessageBoxA("当前系统没有任何网络设置","系统信息",MB_OK);
}
//---------------------------------------------------------------------------
void __fastcall TIP::FormClose(TObject *Sender, TCloseAction &Action)
{
if(AdapterListIndex==NULL)
delete AdapterListIndex;
}
//---------------------------------------------------------------------------
void TIP::GetIpAddressWithIndex(int index) //取不同的网卡的IP地址及共它
{
AnsiString Mac;
DWORD Index=StrToInt(AdapterListIndex->Strings[index]);
CurrentAdapterIPAddress->Clear();
PIP_ADAPTER_INFO pAdapterInfo=NULL;
ULONG ulSize=0;
GetAdaptersInfo(pAdapterInfo,&ulSize);
pAdapterInfo=(PIP_ADAPTER_INFO)new char[ulSize];
GetAdaptersInfo(pAdapterInfo,&ulSize);
while(pAdapterInfo)
{
if(pAdapterInfo->Index ==Index)
{
PIP_ADDR_STRING IpAddressList=&(pAdapterInfo->IpAddressList);
while(IpAddressList) //取IP地址表
{
CurrentAdapterIPAddress->Items->Add(IpAddressList->IpAddress.String);
//Mac=GetMacAdd(IpAddressList->IpAddress.String);
Mac.sprintf("%02X-%02X-%02X-%02X-%02X-%02X",pAdapterInfo->Address[0],pAdapterInfo->Address[1],pAdapterInfo->Address[2], \
pAdapterInfo->Address[3],pAdapterInfo->Address[4],pAdapterInfo->Address[5]);
if(Mac.Length()>0) MacAddress->Caption =Mac;
IpAddressList=IpAddressList->Next ;
}
Mac=GetMacAdd(CurrentAdapterGetway->Items->Strings[0]);//取网关的物理地址
IpAddressList=&(pAdapterInfo->GatewayList);
CurrentAdapterGetway->Items->Clear();
while(IpAddressList) //取网关表
{
CurrentAdapterGetway->Items->Add(IpAddressList->IpAddress.String );
IpAddressList=IpAddressList->Next ;
}
if(CurrentAdapterGetway->Items->Count>0) CurrentAdapterGetway->ItemIndex =0;
CurrentAdapterDHCP->Items->Clear();
if(pAdapterInfo->DhcpEnabled) //是否有DHCP,如有则取DHCP表
{
IpAddressList=&(pAdapterInfo->DhcpServer);
while(IpAddressList)
{
CurrentAdapterDHCP->Items->Add(IpAddressList->IpAddress.String );
IpAddressList=IpAddressList->Next ;
}
}
else CurrentAdapterDHCP->Items->Add("无 DHCP 服务器");
if(CurrentAdapterDHCP->Items->Count>0) CurrentAdapterDHCP->ItemIndex =0;
if(pAdapterInfo->HaveWins) //是否有WINS服务器
{
FirstWins->Caption=pAdapterInfo->PrimaryWinsServer.IpAddress.String;
SecondWins->Caption=pAdapterInfo->SecondaryWinsServer.IpAddress.String;
}
else{
FirstWins->Caption="无";
SecondWins->Caption="无";
}
delete IpAddressList; //删除临时地址表缓冲
break;
}
pAdapterInfo=pAdapterInfo->Next;
}
struct tm *newtime;
newtime = gmtime(&pAdapterInfo->LeaseObtained);
LeaseObtained->Caption=AnsiString(asctime(newtime));
newtime = gmtime(&pAdapterInfo->LeaseExpires);
LeaseExpires->Caption=AnsiString(asctime(newtime));
delete pAdapterInfo;
if(CurrentAdapterIPAddress->Items->Count >0)
{
CurrentAdapterIPAddress->ItemIndex =0;
GetEveryIpInfo(CurrentAdapterIPAddress->Items->Strings[0]);
}
}
//---------------------------------------------------------------------------
void __fastcall TIP::FormCreate(TObject *Sender)
{
HMENU SysMenu=GetSystemMenu(Handle,False);
AppendMenu(SysMenu,0,WM_ABOUTME,"关于本程序(&A)");
this->Caption=Application->Title;
GetDNS();
GetComputerAdapterList();
}
//---------------------------------------------------------------------------
void __fastcall TIP::AdapterListChange(TObject *Sender)
{
GetIpAddressWithIndex(AdapterList->ItemIndex);
}
//---------------------------------------------------------------------------
void TIP::GetEveryIpInfo(AnsiString IP) //取不同的IP地址的子网掩码
{
AnsiString Tmp;
PMIB_IPADDRTABLE pIPTable=NULL;
DWORD ulSize=0;
GetIpAddrTable(pIPTable,&ulSize,TRUE);//获得缓冲区大小
pIPTable=(PMIB_IPADDRTABLE)new(char[ulSize]);
GetIpAddrTable(pIPTable,&ulSize,TRUE);
for(int i=0;i<(int)pIPTable->dwNumEntries;i++)
{
//取出每一个字段,显示IP
Tmp.sprintf("%d.%d.%d.%d",(unsigned int)((LOWORD(pIPTable->table[i].dwAddr)&0x00FF)),
(unsigned int)((LOWORD(pIPTable->table[i].dwAddr)>>8)),
(unsigned int)((HIWORD(pIPTable->table[i].dwAddr)&0x00FF)),
(unsigned int)((HIWORD(pIPTable->table[i].dwAddr))>>8));
if(Tmp==IP)
{
//显示子网掩码
Tmp.sprintf("%d.%d.%d.%d",(unsigned int)((LOWORD(pIPTable->table[i].dwMask)&0x00FF)),
(unsigned int)((LOWORD(pIPTable->table[i].dwMask)>>8)),
(unsigned int)((HIWORD(pIPTable->table[i].dwMask)&0x00FF)),
(unsigned int)((HIWORD(pIPTable->table[i].dwMask))>>16));
CurrentAdapterMask->Caption=Tmp;
break;
}
}
delete pIPTable;
}
void __fastcall TIP::Button1Click(TObject *Sender)
{
//AnsiString GetWayMac=GetMacAdd(CurrentAdapterGetway->Items->Strings[0]);
//if(GetWayMac.Length()>0)
// ShowMessage("网关的网卡物理地址为: "+GetWayMac);
Close();
}
//---------------------------------------------------------------------------
void __fastcall TIP::CurrentAdapterIPAddressChange(TObject *Sender)
{
GetEveryIpInfo(CurrentAdapterIPAddress->Items->Strings[CurrentAdapterIPAddress->ItemIndex]);
}
//---------------------------------------------------------------------------
AnsiString TIP::GetMacAdd(AnsiString strIP) //取不同的IP地址对应的网卡的物理地址
{
AnsiString Mac="";
unsigned char ulMacAddr[6];
unsigned long ulMacAddrLen = 6;
DWORD dwIPAddr = inet_addr(strIP.c_str());
DWORD dwret = SendARP(dwIPAddr, 0,(PULONG)ulMacAddr, &ulMacAddrLen);
if(dwret == 0)
Mac.sprintf("%02X-%02X-%02X-%02X-%02X-%02X", ulMacAddr[0],ulMacAddr[1],ulMacAddr[2],ulMacAddr[3],ulMacAddr[4],ulMacAddr[5]);
return(Mac);
}
//---------------------------------------------------------------------------
void TIP::GetDNS(void) //取DNS列表
{
FIXED_INFO *FixedInfo;
ULONG ulOutBufLen;
ulOutBufLen =0;
FixedInfo=(FIXED_INFO *) GlobalAlloc( GPTR, sizeof( FIXED_INFO ));
GetNetworkParams( FixedInfo, &ulOutBufLen );
GlobalFree(FixedInfo);
FixedInfo=(FIXED_INFO *) GlobalAlloc( GPTR, ulOutBufLen);
if(!GetNetworkParams( FixedInfo, &ulOutBufLen ))
{
MyHostName->Caption=AnsiString(FixedInfo->HostName)+"."+AnsiString(FixedInfo->DomainName);
DNSList->Clear();
// if(!FixedInfo->EnableDns)
// {
PIP_ADDR_STRING DNS=&(FixedInfo->DnsServerList);
while(DNS)
{
DNSList->Items->Add(DNS->IpAddress.String );
DNS=DNS->Next;
}
delete DNS;
if(DNSList->Items->Count==0) DNSList->Items->Add("无 DNS 设置");
DNSList->ItemIndex =0;
switch(FixedInfo->NodeType)
{
case BROADCAST_NODETYPE:
NodeType->Caption="广播网络";break;
case PEER_TO_PEER_NODETYPE:
NodeType->Caption="点对点网络";break;
case MIXED_NODETYPE:
NodeType->Caption="混合网络";break;
case HYBRID_NODETYPE:
NodeType->Caption ="其它混合网络";break;
default:
NodeType->Caption="未知类型";break;
}
EnabledIpRoute->Caption =FixedInfo->EnableRouting?"已启用":"未启用";
NetBiosScope->Caption =AnsiString(FixedInfo->ScopeId);
if(NetBiosScope->Caption.Length()<1) NetBiosScope->Caption ="未知";
NetBiosDNS->Caption =FixedInfo->EnableDns?"已启用":"未启用";
}
GlobalFree(FixedInfo);
}
//---------------------------------------------------------------------------
void TIP::ReleaseIpWithIndex(int index)
{
if(index>=AdapterListIndex->Count) return;
IP_ADAPTER_INDEX_MAP *AdapterInfo=new IP_ADAPTER_INDEX_MAP;
AdapterInfo->Index=(DWORD)StrToInt(AdapterListIndex->Strings[index]);
IpReleaseAddress(AdapterInfo);
delete AdapterInfo;
}
//---------------------------------------------------------------------------
void TIP::ReNewIpWithIndex(int index)
{
if(index>=AdapterListIndex->Count) return;
IP_ADAPTER_INDEX_MAP *AdapterInfo=new IP_ADAPTER_INDEX_MAP;
AdapterInfo->Index=(DWORD)StrToInt(AdapterListIndex->Strings[index]);
IpRenewAddress(AdapterInfo);
delete AdapterInfo;
}
//---------------------------------------------------------------------------
void __fastcall TIP::Button2Click(TObject *Sender)
{
ReleaseIpWithIndex(AdapterList->ItemIndex);
GetIpAddressWithIndex(AdapterList->ItemIndex);
}
//---------------------------------------------------------------------------
void __fastcall TIP::Button3Click(TObject *Sender)
{
ReNewIpWithIndex(AdapterList->ItemIndex);
GetIpAddressWithIndex(AdapterList->ItemIndex);
}
//---------------------------------------------------------------------------
void __fastcall TIP::Button4Click(TObject *Sender)
{
for(int i=0;i<AdapterList->Items->Count;i++)
{
ReleaseIpWithIndex(i);
GetIpAddressWithIndex(i);
}
}
//---------------------------------------------------------------------------
void __fastcall TIP::Button5Click(TObject *Sender)
{
for(int i=0;i<AdapterList->Items->Count;i++)
{
ReNewIpWithIndex(i);
GetIpAddressWithIndex(i);
}
}
//---------------------------------------------------------------------------
void TIP::MySySCommand(TMessage &Msg)
{
if(Msg.WParam==WM_ABOUTME)
{
ShellAbout(Handle,Application->Title.c_str()," 本软件由 SkyBuilder 编写,任何人未经作者同意不得擅自修改!",Application->Icon->Handle );
}
else
Msg.Result = DefWindowProc(Handle, Msg.Msg, Msg.WParam, Msg.LParam);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -