//使用IP Helper函数获取网络参数
//头文件
#include <stdio.h>
#include <windows.h>
#include <iphlpai.h>
//定义变量
ULONG ulOutBufLen;
DWORD dwRetVal;
HANDLE hHeap = GetProcessHeap();
//宏定义
#define MyAlloc(size) HeapAlloc(hHeap,0,size)
#define MyFree(ptr) HeapFree(hHeap,0,ptr)
//主函数
void main(int argc,char *argv[])
{
//获取网络参数
printf("----------------------------------\n");
printf("GetNetworkParams\n\n");
//定义变量
FIXED_INFO *pFixedInfo;
IP_ADDR_STRING *pIPAddr;
//分配内存
pFixedInfo = (FIXED_INFO*)MyAlloc(sizeof(FIXED_INFO));
ulOutBufLen = sizeof(FIXED_INFO);
if(GetNetworkParams(pFixedInfo,&ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
{
//空间不足,重新分配
MyFree(pFixedInfo);
pFixedInfo = (FIXED_INFO *)MyAlloc(ulOutBufLen);
}
//获取信息
if(dwRetVal = GetNetworkParams(pFixedInfo,&ulOutBufLen) != NO_ERROR)
{
printf("Call to GetNetworkParams failed.\n");
}
else
{
//打印输出
printf("\tHost Name: %s\n",pFixedInfo->HostName); //主机名
printf("\tDomain Name: %sn",pFixedInfo->DomainName); //DNS主机名
printf("\tDNS Servers:\n"); //DNS服务器
printf("\t\t%s\n",pFixedInfo->DnsServerList.IpAddress.String); //IP,字符串形式
pIPAddr = pFixedInfo->DnsServerList.Next; //备用DNS
while(pIPAddr)
{
printf("\t\t%s\n",pIPAddr->IpAddress.String);
pIPAddr = pIPAddr->Next;
}
if(pFixedInfo->EnableRouting)
{
printf("\tEnable Routing: Yes\n"); //是否路由
}
else
{
printf("\tEnable Routing: No\n");
}
if(pFixedInfo->EnableProxy)
{
printf("\tEnable Proxy: Yes\n"); //是否有代理
}
else
{
printf("\tEnable Proxy: No\n");
}
if(pFixedInfo->EnableDns)
{
printf("\tEnable DNS: Yes\n"); //是否有DNS
}
else
{
printf("\tEnable DNS: No\n");
}
}
//释放内存
MyFree(pFixedInfo);
printf("--------------------------------------\n");
printf("GetInterfaceInfo\n\n");
//定义变量,分配空间
PIP_INTERFACE_INFO pInfo;
ulOutBufLen = sizeof(IP_INTERFACE_INFO);
pInfo = (PIP_INTERFACE_INFO)MyAlloc(ulOutBufLen);
while(1)
{
dwRetVal = GetInterfaceInfo(pInfo,&ulOutBufLen);
//内存不足,重新分配
if(dwRetVal == ERROR_INSUFFICIENT_BUFFER)
{
MyFree(pInfo);
pInfo = (IP_INTERFACE_INFO *)MyAlloc(ulOutBufLen);
}
else if(dwRetVal == NO_ERROR)
{
//显示输出每个网卡的信息
int i;
printf("Num Adapters: %ld\n",pInfo->NumAdapters);
for(i = 0;i < pInfo->NumAdapters;i++)
{
printf("(%d)Adapter Name: %ws\n",i,pInfo->Adapter[i].Name);
printf("(%d)Adapter Index: %ld\n",i,pInfo->Adapter[i].Index);
}
break;
}
else
{
printf("GetInterfaceInfo: %u\n",GetLastError());
}
}
MyFree(pInfo); //释放内存
printf("----------------------------------------------\n");
printf("GetIpAddrTable\n\n");
//变量定义,内存分配
MIB_IPADDRTABLE *pIPAddrTable;
DWORD dwSize;
in_addr IPAddr;
char *strIpAddr;
pIPAddrTable = (MIB_IPADDRTABLE*)MyAlloc(sizeof(MIB_IPADDRTABLE));
dwSize = 0;
IPAddr.S_un.S_addr = ntohl(pIPAddrTable->table[1].dwAddr);
strIPaddr = inet_ntoa(IPAddr);
if(GetIpAddrTable(pIPAddrTable,&dwSize,0) == ERROR_INSUFFICIENT_BUFFER)
{
//空间不足,重新分配
MyFree(pIPAddrTable);
pIPAddrTable = (MIB_IPADDRTABLE *)MyAlloc(dwSize);
}
//获得信息并显示
if((dwRetVal = GetIpAddrTable(pIPAddrTable,&dwSize,0))!= NO_ERROR)
{
printf("Call to GetIpAddrTable failed.\n");
}
//IP 、掩码等是以DWORD的形式保存的
printf("Address: %u.%u.%u.%u\n",pIPAddrTable->table[0].dwAddr & 0x000000FF,
(pIPAddrTable->table[0].dwAddr & 0x0000FF00)>>8,
(pIPAddrtable->table[0].dwAddr & 0x00FF0000)>>16,
(pIPAddrtable->table[0].dwAddr & 0xFF000000)>>24);
printf("Mask: %lu\n",pIPAddrtable->table[0].dwMask);
printf("Index: %lu\n",pIPAddrTable->table[0].dwIndex);
printf("BCast: %lu\n",pIPAddrTable->table[0].dwBCastAddr);
printf("Reasm: %lu\n",pIPAddrTable->table[0].dwReasmSize);
//释放内存
MyFree(pIPAddrTable);
printf("---------------------------------\n");
printf("GetIPStatistics\n\n");
//定义变量,分配内存
MIB_IPSTATS *pStats;
pStats = (MIB_IPSTATS*)MyAlloc(sizeof(MIB_IPSTATS));
//获取信息
if((dwRetVal = GetIpStatistics(pStats)) != NO_ERRO)
{
printf("\tError getting stats.\n");
}
//显示
printf("\tNumber of IP addresses: %ld\n",pStats->dwNumAddr);
printf("\tNumber of Interfaces: %ld\n",pStats->dwNumIf);
printf("\tReceives: %ld\n",pStats->dwInReceives);
printf("\tOut Requests: %ld\n",pStats->dwOutRequests);
printf("\tRoutes: %ld\n",pStats->dwNumRoutes);
printf("\tTimeout Time: %ld\n",pStats->dwReasmTimeout);
printf("\tIn Delivers: %ld\n",pStats->dwInDelivers);
printf("\tIn Discards: %ld\n",pStats->dwInDiscards);
printf("\tTotal In: %ld\n",pStats->dwInDelivers + pStats->dwInDiscards);
printf("\tIn Header Errors: %ld\n",pSats->dwInHdrErrors);
MyFree(pStats); //释放
printf("------------------------------------------\n");
printf("GetTCPStatistics\n\n");
//定义变量,申请内存
MIB_TCPSTATS *pTCPStats;
pTCPStats = (MIB_TCPSTATS *)MyAlloc(sizeof(MIB_TCPSTATS));
//获取信息,并显示
if((dwRetVal = GetTcpStatistics(pTCPStats)) != NO_ERROR)
{
printf("Error getting TCP Stats.\n");
}
printf("\tActive Opens: %ld\n",pTCPStats->dwActiveOpens);
printf("\tPassive Opens: %ld\n",pTCPStats->dwPassiveOpens);
printf("\tSegments Recv: %ld\n",pTCPStats->dwInSegs);
printf("\tSegments Xmit: %ld\n",pTCPStats->dwOutSegs);
printf("\tTotal # Conxs: %ld\n",pTCPStats->dwNumConns);
MyFree(pTCPStats); //内存释放
printf("---------------------------------\n");
printf("GetAdapatersInfo\n\n");
//定义变量,分配内存
IP_ADAPTER_INFO *pAdapterInfo;
IP_ADAPTER_INFO *pAdapter;
pAdapterInfo = (IP_ADAPTER_INFO *)MyAlloc(sizeof(IP_ADAPTER_INFO));
ulOutBufLen = sizeof(IP_ADAPTER_INFO);
//测试,所需内存大小
if(GetAdaptersInfo(pAdapterInfo,&ulOutBufLen) != ERROR_SUCCESS)
{
MyFree(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *)MyAlloc(ulOutBufLen);
}
//获取信息
if((dwRetVal = GetAdaptersInfo(pAdapterInfo,&ulOutBufLen)) != NO_ERROR)
{
printf("Call to GetAdaptersInfo failed.\n");
}
pAdapter = pAdapterInfo;
//显示各个网卡的信息
while(pAdapter)
{
printf("\n\t******************\n");
printf("\tAdapter Name: \t%s\n",pAdapter->AdapterName); //网卡设备名
printf("\tAdapter Desc: \t%s\n",pAdapter->Description); //描述
printf("\tAdapter Addr: \t%s\n",pAdapter->Address); //MAC
printf("\tIP Addresss: \t%s\n",pAdapter->IpAddressList.IpAddress.String); //IP
printf("\tIP Mask: \t%s\n",pAdapter->IpAddressList.IpMask.String); //掩码
printf("\tGateway: \t%s\n",pAdapter->GatewayList.IpAddress.String); //网关
printf("\t***\n");
if(pAdapter->DhcpEnabled)//DHCP
{
printf("\tDHCP Enabled: Yes\n");
printf("\t\tDHBCP Server: \t%s\n",pAdapter->DhcpServer.IpAddress.String);
printf("\tLease Otained: %ld\n",pAdapter->LeaseOtained);
}
else
{
printf("\tDHCP Enaled: No\n");
}
if(pAdapter->HaveWins)//Wins
{
printf("\tHave Wins:Yes\n");
printf("\t\tPrimary Wins Server: \t%s\n",pAdapter->SecondaryWinsServer.String);
printf("\t\tSecondary Wins Server: \t%s\n",pAdapter->SecondaryWinsServer.IpAddress.String);
}
else
{
printf("\tHave Wins: No\n");
}
pAdapter = pAdapter->Next;
}
MyFree(pAdapterInfo);
}
//头文件
#include <stdio.h>
#include <windows.h>
#include <iphlpai.h>
//定义变量
ULONG ulOutBufLen;
DWORD dwRetVal;
HANDLE hHeap = GetProcessHeap();
//宏定义
#define MyAlloc(size) HeapAlloc(hHeap,0,size)
#define MyFree(ptr) HeapFree(hHeap,0,ptr)
//主函数
void main(int argc,char *argv[])
{
//获取网络参数
printf("----------------------------------\n");
printf("GetNetworkParams\n\n");
//定义变量
FIXED_INFO *pFixedInfo;
IP_ADDR_STRING *pIPAddr;
//分配内存
pFixedInfo = (FIXED_INFO*)MyAlloc(sizeof(FIXED_INFO));
ulOutBufLen = sizeof(FIXED_INFO);
if(GetNetworkParams(pFixedInfo,&ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
{
//空间不足,重新分配
MyFree(pFixedInfo);
pFixedInfo = (FIXED_INFO *)MyAlloc(ulOutBufLen);
}
//获取信息
if(dwRetVal = GetNetworkParams(pFixedInfo,&ulOutBufLen) != NO_ERROR)
{
printf("Call to GetNetworkParams failed.\n");
}
else
{
//打印输出
printf("\tHost Name: %s\n",pFixedInfo->HostName); //主机名
printf("\tDomain Name: %sn",pFixedInfo->DomainName); //DNS主机名
printf("\tDNS Servers:\n"); //DNS服务器
printf("\t\t%s\n",pFixedInfo->DnsServerList.IpAddress.String); //IP,字符串形式
pIPAddr = pFixedInfo->DnsServerList.Next; //备用DNS
while(pIPAddr)
{
printf("\t\t%s\n",pIPAddr->IpAddress.String);
pIPAddr = pIPAddr->Next;
}
if(pFixedInfo->EnableRouting)
{
printf("\tEnable Routing: Yes\n"); //是否路由
}
else
{
printf("\tEnable Routing: No\n");
}
if(pFixedInfo->EnableProxy)
{
printf("\tEnable Proxy: Yes\n"); //是否有代理
}
else
{
printf("\tEnable Proxy: No\n");
}
if(pFixedInfo->EnableDns)
{
printf("\tEnable DNS: Yes\n"); //是否有DNS
}
else
{
printf("\tEnable DNS: No\n");
}
}
//释放内存
MyFree(pFixedInfo);
printf("--------------------------------------\n");
printf("GetInterfaceInfo\n\n");
//定义变量,分配空间
PIP_INTERFACE_INFO pInfo;
ulOutBufLen = sizeof(IP_INTERFACE_INFO);
pInfo = (PIP_INTERFACE_INFO)MyAlloc(ulOutBufLen);
while(1)
{
dwRetVal = GetInterfaceInfo(pInfo,&ulOutBufLen);
//内存不足,重新分配
if(dwRetVal == ERROR_INSUFFICIENT_BUFFER)
{
MyFree(pInfo);
pInfo = (IP_INTERFACE_INFO *)MyAlloc(ulOutBufLen);
}
else if(dwRetVal == NO_ERROR)
{
//显示输出每个网卡的信息
int i;
printf("Num Adapters: %ld\n",pInfo->NumAdapters);
for(i = 0;i < pInfo->NumAdapters;i++)
{
printf("(%d)Adapter Name: %ws\n",i,pInfo->Adapter[i].Name);
printf("(%d)Adapter Index: %ld\n",i,pInfo->Adapter[i].Index);
}
break;
}
else
{
printf("GetInterfaceInfo: %u\n",GetLastError());
}
}
MyFree(pInfo); //释放内存
printf("----------------------------------------------\n");
printf("GetIpAddrTable\n\n");
//变量定义,内存分配
MIB_IPADDRTABLE *pIPAddrTable;
DWORD dwSize;
in_addr IPAddr;
char *strIpAddr;
pIPAddrTable = (MIB_IPADDRTABLE*)MyAlloc(sizeof(MIB_IPADDRTABLE));
dwSize = 0;
IPAddr.S_un.S_addr = ntohl(pIPAddrTable->table[1].dwAddr);
strIPaddr = inet_ntoa(IPAddr);
if(GetIpAddrTable(pIPAddrTable,&dwSize,0) == ERROR_INSUFFICIENT_BUFFER)
{
//空间不足,重新分配
MyFree(pIPAddrTable);
pIPAddrTable = (MIB_IPADDRTABLE *)MyAlloc(dwSize);
}
//获得信息并显示
if((dwRetVal = GetIpAddrTable(pIPAddrTable,&dwSize,0))!= NO_ERROR)
{
printf("Call to GetIpAddrTable failed.\n");
}
//IP 、掩码等是以DWORD的形式保存的
printf("Address: %u.%u.%u.%u\n",pIPAddrTable->table[0].dwAddr & 0x000000FF,
(pIPAddrTable->table[0].dwAddr & 0x0000FF00)>>8,
(pIPAddrtable->table[0].dwAddr & 0x00FF0000)>>16,
(pIPAddrtable->table[0].dwAddr & 0xFF000000)>>24);
printf("Mask: %lu\n",pIPAddrtable->table[0].dwMask);
printf("Index: %lu\n",pIPAddrTable->table[0].dwIndex);
printf("BCast: %lu\n",pIPAddrTable->table[0].dwBCastAddr);
printf("Reasm: %lu\n",pIPAddrTable->table[0].dwReasmSize);
//释放内存
MyFree(pIPAddrTable);
printf("---------------------------------\n");
printf("GetIPStatistics\n\n");
//定义变量,分配内存
MIB_IPSTATS *pStats;
pStats = (MIB_IPSTATS*)MyAlloc(sizeof(MIB_IPSTATS));
//获取信息
if((dwRetVal = GetIpStatistics(pStats)) != NO_ERRO)
{
printf("\tError getting stats.\n");
}
//显示
printf("\tNumber of IP addresses: %ld\n",pStats->dwNumAddr);
printf("\tNumber of Interfaces: %ld\n",pStats->dwNumIf);
printf("\tReceives: %ld\n",pStats->dwInReceives);
printf("\tOut Requests: %ld\n",pStats->dwOutRequests);
printf("\tRoutes: %ld\n",pStats->dwNumRoutes);
printf("\tTimeout Time: %ld\n",pStats->dwReasmTimeout);
printf("\tIn Delivers: %ld\n",pStats->dwInDelivers);
printf("\tIn Discards: %ld\n",pStats->dwInDiscards);
printf("\tTotal In: %ld\n",pStats->dwInDelivers + pStats->dwInDiscards);
printf("\tIn Header Errors: %ld\n",pSats->dwInHdrErrors);
MyFree(pStats); //释放
printf("------------------------------------------\n");
printf("GetTCPStatistics\n\n");
//定义变量,申请内存
MIB_TCPSTATS *pTCPStats;
pTCPStats = (MIB_TCPSTATS *)MyAlloc(sizeof(MIB_TCPSTATS));
//获取信息,并显示
if((dwRetVal = GetTcpStatistics(pTCPStats)) != NO_ERROR)
{
printf("Error getting TCP Stats.\n");
}
printf("\tActive Opens: %ld\n",pTCPStats->dwActiveOpens);
printf("\tPassive Opens: %ld\n",pTCPStats->dwPassiveOpens);
printf("\tSegments Recv: %ld\n",pTCPStats->dwInSegs);
printf("\tSegments Xmit: %ld\n",pTCPStats->dwOutSegs);
printf("\tTotal # Conxs: %ld\n",pTCPStats->dwNumConns);
MyFree(pTCPStats); //内存释放
printf("---------------------------------\n");
printf("GetAdapatersInfo\n\n");
//定义变量,分配内存
IP_ADAPTER_INFO *pAdapterInfo;
IP_ADAPTER_INFO *pAdapter;
pAdapterInfo = (IP_ADAPTER_INFO *)MyAlloc(sizeof(IP_ADAPTER_INFO));
ulOutBufLen = sizeof(IP_ADAPTER_INFO);
//测试,所需内存大小
if(GetAdaptersInfo(pAdapterInfo,&ulOutBufLen) != ERROR_SUCCESS)
{
MyFree(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *)MyAlloc(ulOutBufLen);
}
//获取信息
if((dwRetVal = GetAdaptersInfo(pAdapterInfo,&ulOutBufLen)) != NO_ERROR)
{
printf("Call to GetAdaptersInfo failed.\n");
}
pAdapter = pAdapterInfo;
//显示各个网卡的信息
while(pAdapter)
{
printf("\n\t******************\n");
printf("\tAdapter Name: \t%s\n",pAdapter->AdapterName); //网卡设备名
printf("\tAdapter Desc: \t%s\n",pAdapter->Description); //描述
printf("\tAdapter Addr: \t%s\n",pAdapter->Address); //MAC
printf("\tIP Addresss: \t%s\n",pAdapter->IpAddressList.IpAddress.String); //IP
printf("\tIP Mask: \t%s\n",pAdapter->IpAddressList.IpMask.String); //掩码
printf("\tGateway: \t%s\n",pAdapter->GatewayList.IpAddress.String); //网关
printf("\t***\n");
if(pAdapter->DhcpEnabled)//DHCP
{
printf("\tDHCP Enabled: Yes\n");
printf("\t\tDHBCP Server: \t%s\n",pAdapter->DhcpServer.IpAddress.String);
printf("\tLease Otained: %ld\n",pAdapter->LeaseOtained);
}
else
{
printf("\tDHCP Enaled: No\n");
}
if(pAdapter->HaveWins)//Wins
{
printf("\tHave Wins:Yes\n");
printf("\t\tPrimary Wins Server: \t%s\n",pAdapter->SecondaryWinsServer.String);
printf("\t\tSecondary Wins Server: \t%s\n",pAdapter->SecondaryWinsServer.IpAddress.String);
}
else
{
printf("\tHave Wins: No\n");
}
pAdapter = pAdapter->Next;
}
MyFree(pAdapterInfo);
}