#include "stdafx.h "
#include <stdio.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;
//函数指针
PGAINFO pGAInfo;
//加载IP Helper API 所需的库文件
HINSTANCE hInst;//实例句柄
hInst=LoadLibrary( "iphlpapi.dll ");
if(!hInst)
cout < < "iphlpapi.dll not supported in this platform!/n ";
cout < < "net adapters information: " < <endl < <endl;
//------------------------------------》获得网卡数据
pGAInfo=(PGAINFO)GetProcAddress(hInst, "GetAdaptersInfo ");
PIP_ADAPTER_INFO pInfo=NULL,pInfoTemp=NULL;
ULONG ulSize=0;
pGAInfo(pInfo,&ulSize);//第一次调用,获取缓冲区大小
pInfoTemp=pInfo=(PIP_ADAPTER_INFO)new(char[ulSize]);
pGAInfo(pInfo,&ulSize);
//遍历每一张网卡
while(pInfo)
{
//网卡名
cout < < "adapter name: " < <pInfo-> AdapterName < <endl;
//网卡描述信息
cout < < "description: " < <pInfo-> Description < <endl;
//记录下它的索引值以便后面使用。
index=pInfo-> Index;
//物理地址的长度
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);
//显示绑定于这张网卡之上的IP地址
cout < < "ip address bound to this chapter: " < <endl;
PIP_ADDR_STRING pAddTemp=&(pInfo-> IpAddressList);
while(pAddTemp)/*遍历IP列表中的每一个元素*/
{
cout < <pAddTemp-> IpAddress.String < <endl;
pAddTemp=pAddTemp-> Next;
}
//显示当前使用的IP地址
//因为在离线状态下pInfo-> CurrentIpAddress被置空,所以有必要做检查
if(pInfo-> CurrentIpAddress)
{
cout < < "current ip using: " < <endl;
pAddTemp=pInfo-> CurrentIpAddress;
while(pAddTemp)/*遍历IP列表中的每一个元素*/
{
cout < <pAddTemp-> IpAddress.String < <endl;
pAddTemp=pAddTemp-> Next;
}
}
else
cout < < "network malfunctioning,no ip is in use!/n " < <endl;
//显示DHCP 服务器数据
cout < < "DHCP in use: " < <endl;
pAddTemp=&(pInfo-> DhcpServer);
while(pAddTemp)/*遍历IP列表中的每一个元素*/
{
cout < <pAddTemp-> IpAddress.String < <endl;
pAddTemp=pAddTemp-> Next;
}
//将当前指针移向下一个
pInfo=pInfo-> Next;
}
delete pInfoTemp;//回收无用内存
//------------------------------------》获得网卡数据部分结束
cout < <endl < <endl;
//函数指针
PGNOINTERFACE pGNOInterface;
PGIINFO pGIInfo;
cout < < "network interfaces information: " < <endl < <endl;
//------------------------------------》网络接口信息部分
//显示网络接口的个数
DWORD ulNumOfInterfaces=0;
pGNOInterface=(PGNOINTERFACE)GetProcAddress(hInst, "GetNumberOfInterfaces ");
pGNOInterface(&ulNumOfInterfaces);
cout < < "U have " < <ulNumOfInterfaces < < " network interfaces/n ";
//获取网络接口信息
pGIInfo=(PGIINFO)GetProcAddress(hInst, "GetInterfaceInfo ");
PIP_INTERFACE_INFO pIInfo=NULL;
ulSize=0;
pGIInfo(pIInfo,&ulSize);//第一次调用,获取缓冲区大小
pIInfo=(PIP_INTERFACE_INFO)new(char[ulSize]);
pGIInfo(pIInfo,&ulSize);
//显示网络接口信息
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;
//------------------------------------》网络接口信息部分结束
cout < <endl < <endl;
//函数指针
PGIAT pGIAT;
cout < < "ip information: " < <endl < <endl;
//------------------------------------》IP绑定信息
pGIAT=(PGIAT)GetProcAddress(hInst, "GetIpAddrTable ");
PMIB_IPADDRTABLE pIPTable=NULL;
ulSize=0;
pGIAT(pIPTable,&ulSize,TRUE);//获得缓冲区大小
pIPTable=(PMIB_IPADDRTABLE)new(char[ulSize]);
pGIAT(pIPTable,&ulSize,TRUE);
for(i=0;i <pIPTable-> dwNumEntries;i++)
{
//取出每一个字段,显示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;
//显示绑定网络接口的索引
cout < < "it is bound to interface: " < <pIPTable-> table[i].dwIndex < <endl;
//显示子网掩码
/* 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;*/
char szMask[200];
sprintf( "%s ",inet_ntoa(pIPTable-> table[0].dwMask));
cout < <szMask < <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;
//显示最大报文大小
cout < < "it 's reassembly size: " < <pIPTable-> table[i].dwReasmSize < <endl;
}
//------------------------------------》IP绑定信息结束
PAIA pAIA;
/*
//------------------------------------》IP设置部分
if(!index)
{
cout < < "no adapters available, cannot set ip address! " < <endl;
return 0;//没有网络接口可以设置
}
pAIA=(PAIA)GetProcAddress(hInst, "AddIPAddress ");
IPAddr addr=0x184BC5CA;
IPMask mask=0x00FFFFFF;
ULONG context;
ULONG Inst;
pAIA(addr,mask,index,&context,&Inst);
//------------------------------------》IP设置部分结束
cout < <endl < <endl;
*/
return 0;
}
vc 控制IP的有关信息
最新推荐文章于 2024-06-03 11:15:05 发布