誰知道怎么改本地靜態(tài) IP 地址? - C++ Builder / Windows SDK...
//加載IP Helper API 所需的庫(kù)文件
E:\Borland\CBuilder5\Lib\PSDK\iphlpapi.lib
// IpHelper.cpp : Defines the entry point for the console application.
#include "stdafx.h "
#include "windows.h "
#include "iostream.h "
#include "iphlpapi.h "
typedef DWORD(CALLBACK * PGNOINTERFACE)(PDWORD);//GetNumberOfInterfaces
typedef DWORD(CALLBACK * PGAINFO)(PIP_ADAPTER_INFO,PULONG);//GetAdaptersInfo
typedef DWORD(CALLBACK * PGIINFO)(PIP_INTERFACE_INFO,PULONG );//GetInterfaceInfo
typedef DWORD(CALLBACK * PGIAT)(PMIB_IPADDRTABLE,PULONG,BOOL);//GetIpAddrTable
typedef DWORD(CALLBACK * PAIA)(IPAddr,IPMask,DWORD,PULONG,PULONG);//AddIPAddress
int main(int argc, char* argv[])
{
DWORD index=0;
//函數(shù)指針
PGAINFO pGAInfo;
//加載IP Helper API 所需的庫(kù)文件
HINSTANCE hInst;//實(shí)例句柄
hInst=LoadLibrary( "iphlpapi.dll ");
if(!hInst)
cout < < "iphlpapi.dll not supported in this platform!\n ";
cout < < "net adapters information: " < <endl < <endl;
//------------------------------------》獲得網(wǎng)卡數(shù)據(jù)
pGAInfo=(PGAINFO)GetProcAddress(hInst, "GetAdaptersInfo ");
PIP_ADAPTER_INFO pInfo=NULL,pInfoTemp=NULL;
ULONG ulSize=0;
pGAInfo(pInfo,&ulSize);//第一次調(diào)用,獲取緩沖區(qū)大小
pInfoTemp=pInfo=(PIP_ADAPTER_INFO)new(char[ulSize]);
pGAInfo(pInfo,&ulSize);
//遍歷每一張網(wǎng)卡
while(pInfo)
{
//網(wǎng)卡名
cout < < "adapter name: " < <pInfo-> AdapterName < <endl;
//網(wǎng)卡描述信息
cout < < "description: " < <pInfo-> Description < <endl;
//記錄下它的索引值以便后面使用。
index=pInfo-> Index;
//物理地址的長(zhǎng)度
cout < < "hardware address length: " < <pInfo-> AddressLength < <endl;
//顯示物理地址
cout < < "hardware address: ";
cout.setf(ios::hex);
cout.unsetf(ios::dec);
for(int i=0;i <(int)pInfo-> AddressLength;i++)
cout < <(unsigned int)pInfo-> Address[i];
cout < <endl;
cout.unsetf(ios::hex);
cout.setf(ios::dec);
//顯示綁定于這張網(wǎng)卡之上的IP地址
cout < < "ip address bound to this chapter: " < <endl;
PIP_ADDR_STRING pAddTemp=&(pInfo-> IpAddressList);
while(pAddTemp)/*遍歷IP列表中的每一個(gè)元素*/
{
cout < <pAddTemp-> IpAddress.String < <endl;
pAddTemp=pAddTemp-> Next;
}
//顯示當(dāng)前使用的IP地址
//因?yàn)樵陔x線狀態(tài)下pInfo-> CurrentIpAddress被置空,所以有必要做檢查
if(pInfo-> CurrentIpAddress)
{
cout < < "current ip using: " < <endl;
pAddTemp=pInfo-> CurrentIpAddress;
while(pAddTemp)/*遍歷IP列表中的每一個(gè)元素*/
{
cout < <pAddTemp-> IpAddress.String < <endl;
pAddTemp=pAddTemp-> Next;
}
}
else
cout < < "network malfunctioning,no ip is in use!\n " < <endl;
//顯示DHCP 服務(wù)器數(shù)據(jù)
cout < < "DHCP in use: " < <endl;
pAddTemp=&(pInfo-> DhcpServer);
while(pAddTemp)/*遍歷IP列表中的每一個(gè)元素*/
{
cout < <pAddTemp-> IpAddress.String < <endl;
pAddTemp=pAddTemp-> Next;
}
//將當(dāng)前指針移向下一個(gè)
pInfo=pInfo-> Next;
}
delete pInfoTemp;//回收無用內(nèi)存
//------------------------------------》獲得網(wǎng)卡數(shù)據(jù)部分結(jié)束
cout < <endl < <endl;
//函數(shù)指針
PGNOINTERFACE pGNOInterface;
PGIINFO pGIInfo;
cout < < "network interfaces information: " < <endl < <endl;
//------------------------------------》網(wǎng)絡(luò)接口信息部分
//顯示網(wǎng)絡(luò)接口的個(gè)數(shù)
DWORD ulNumOfInterfaces=0;
pGNOInterface=(PGNOINTERFACE)GetProcAddress(hInst, "GetNumberOfInterfaces ");
pGNOInterface(&ulNumOfInterfaces);
cout < < "U have " < <ulNumOfInterfaces < < " network interfaces\n ";
//獲取網(wǎng)絡(luò)接口信息
pGIInfo=(PGIINFO)GetProcAddress(hInst, "GetInterfaceInfo ");
PIP_INTERFACE_INFO pIInfo=NULL;
ulSize=0;
pGIInfo(pIInfo,&ulSize);//第一次調(diào)用,獲取緩沖區(qū)大小
pIInfo=(PIP_INTERFACE_INFO)new(char[ulSize]);
pGIInfo(pIInfo,&ulSize);
//顯示網(wǎng)絡(luò)接口信息
for(int i=0;i <pIInfo-> NumAdapters;i++)
{
cout < < "Adapter index: " < <pIInfo-> Adapter[i].Index < <endl;
cout < < "and name: " < <pIInfo-> Adapter[i].Name < <endl;
}
delete pIInfo;
//------------------------------------》網(wǎng)絡(luò)接口信息部分結(jié)束
cout < <endl < <endl;
//函數(shù)指針
PGIAT pGIAT;
cout < < "ip information: " < <endl < <endl;
//------------------------------------》IP綁定信息
pGIAT=(PGIAT)GetProcAddress(hInst, "GetIpAddrTable ");
PMIB_IPADDRTABLE pIPTable=NULL;
ulSize=0;
pGIAT(pIPTable,&ulSize,TRUE);//獲得緩沖區(qū)大小
pIPTable=(PMIB_IPADDRTABLE)new(char[ulSize]);
pGIAT(pIPTable,&ulSize,TRUE);
for(i=0;i <pIPTable-> dwNumEntries;i++)
{
//取出每一個(gè)字段,顯示IP
cout < < "ip address: " < <(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) < <endl;
//顯示綁定網(wǎng)絡(luò)接口的索引
cout < < "it is bound to interface: " < <pIPTable-> table[i].dwIndex < <endl;
//顯示子網(wǎng)掩碼
cout < < "it 's net mask: " < <(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) < <endl;
//顯示廣播地址
cout < < "and broadcast addres: " < <(unsigned int)((LOWORD(pIPTable-> table[i].dwBCastAddr)&0x00FF)) < < ". "\
< <(unsigned int)((LOWORD(pIPTable-> table[i].dwBCastAddr)> > 8)) < < ". "\
< <(unsigned int)((HIWORD(pIPTable-> table[i].dwBCastAddr)&0x00FF)) < < ". "\
< <(unsigned int)((HIWORD(pIPTable-> table[i].dwBCastAddr))> > 16) < <endl;
//顯示最大報(bào)文大小
cout < < "it 's reassembly size: " < <pIPTable-> table[i].dwReasmSize < <endl;
}
//------------------------------------》IP綁定信息結(jié)束
PAIA pAIA;
//------------------------------------》IP設(shè)置部分
if(!index)
{
cout < < "no adapters available, cannot set ip address! " < <endl;
return 0;//沒有網(wǎng)絡(luò)接口可以設(shè)置
}
pAIA=(PAIA)GetProcAddress(hInst, "AddIPAddress ");
IPAddr addr=0x184BC5CA;
IPMask mask=0x00FFFFFF;
ULONG context;
ULONG Inst;
pAIA(addr,mask,index,&context,&Inst);
//------------------------------------》IP設(shè)置部分結(jié)束
cout < <endl < <endl;
return 0;
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。