如何设置本机IP地址
想做这样一个东西,首次运行记录下本机的IP地址
发现IP更改后需要还原成原来的IP,该这么弄呢?
另:如何读取子网掩码,网关等信息。
[解决办法]
好久以前写的一个程序,里面有相关的功能代码,楼主参考一下
void __fastcall TIpChangerMainForm::FormCreate(TObject *Sender){//清空控件ComboBox1->Items->Clear();Edit1->Text="";Edit2->Text="";Edit3->Text="";Edit4->Text="";Edit5->Text="";Edit6->Text="";Label26->Caption="";Label13->Caption="";pStatus=(MIB_IPSTATS*)new char[sizeof(MIB_IPSTATS)];OldReceiveNumber=0;//读取数据,填充结构体int iAdapterNumber=0;//计算有几块网卡,准备初始化结构体ULONG ulAdapterInfoSize = sizeof(IP_ADAPTER_INFO);IP_ADAPTER_INFO *pAdapterInfo=(IP_ADAPTER_INFO*)new char[ulAdapterInfoSize];IP_ADAPTER_INFO *pAdapterInfoBkp;if(GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_BUFFER_OVERFLOW ) // 缓冲区不够大{ delete pAdapterInfo; pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize];}if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_SUCCESS ){ pAdapterInfoBkp=pAdapterInfo; while(pAdapterInfo){ // 遍历所有适配器,取得网络适配器数量 if(pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET) { iAdapterNumber++; } pAdapterInfo = pAdapterInfo->Next; }}ArrayAdapterInfo.Length=iAdapterNumber;pAdapterInfo=pAdapterInfoBkp;int i=0;//读取网卡名字到ComboBox中while(pAdapterInfo){ // 遍历所有适配器 if(pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET) { ComboBox1->Items->Add(pAdapterInfo->Description); ArrayAdapterInfo[i]=*pAdapterInfo; i++; } pAdapterInfo = pAdapterInfo->Next; }ComboBox1->ItemIndex=0;SetDisplay(0);//取得选中的网卡的indexOldAdapterIndex=ArrayAdapterInfo[0].Index;NewAdapterIndex=OldAdapterIndex;delete pAdapterInfo;//读取TCP连接列表Timer2->Enabled=true;Timer2Timer(Sender);}//---------------------------------------void TIpChangerMainForm::SetDisplay(int i){//显示ip地址,mask,gatewayIP_ADDR_STRING *pIpList=&ArrayAdapterInfo[i].IpAddressList;IP_ADDR_STRING *pGateWayList=&ArrayAdapterInfo[i].GatewayList;while(pIpList) { Edit2->Text=pIpList->IpAddress.String; Edit3->Text=pIpList->IpMask.String; pIpList=pIpList->Next; }while(pGateWayList) { Edit4->Text=pGateWayList->IpAddress.String; pGateWayList=pGateWayList->Next; }//显示DNS服务器ULONG ulAdapterInfoSizePer = sizeof(IP_PER_ADAPTER_INFO);IP_PER_ADAPTER_INFO *pAdapterInfoPer=(IP_PER_ADAPTER_INFO*)new char[ulAdapterInfoSizePer];if(GetPerAdapterInfo(ArrayAdapterInfo[i].Index, pAdapterInfoPer, &ulAdapterInfoSizePer) == ERROR_BUFFER_OVERFLOW ) // 缓冲区不够大{ delete pAdapterInfoPer; pAdapterInfoPer = (IP_PER_ADAPTER_INFO*)new char[ulAdapterInfoSizePer];}if( GetPerAdapterInfo(ArrayAdapterInfo[i].Index,pAdapterInfoPer, &ulAdapterInfoSizePer) == ERROR_SUCCESS ){//取DNSIP_ADDR_STRING *pDnsList=&pAdapterInfoPer->DnsServerList;while(pDnsList) { Edit5->Text=pDnsList->IpAddress.String; pDnsList=pDnsList->Next; if(!pDnsList) break; Edit6->Text=pDnsList->IpAddress.String; pDnsList=pDnsList->Next; }delete pDnsList;}//显示物理地址AnsiString StrMacAddress="";DynamicArray<BYTE> MacArray;MacArray.Length=ArrayAdapterInfo[i].AddressLength;for(int j=0;j<int(ArrayAdapterInfo[i].AddressLength);j++){ MacArray[j]=ArrayAdapterInfo[i].Address[j];}for(int j=0;j<int(ArrayAdapterInfo[i].AddressLength)-1;j++) { StrMacAddress=StrMacAddress+IntToHex(MacArray[j],2)+"-"; }StrMacAddress=StrMacAddress+IntToHex(MacArray[int(ArrayAdapterInfo[i].AddressLength)-1],2);Edit1->Text=StrMacAddress;//显示折线图Timer1->Enabled=true;if(GetIpStatistics(pStatus)==NO_ERROR) { OldReceiveNumber=pStatus->dwInReceives; OldOutRequest=pStatus->dwOutRequests; }delete pAdapterInfoPer;delete pGateWayList;delete pIpList;MacArray.Length=0;}