《Windows核心编程》の聊聊MIB相关结构

MIB_IPADDRTABLE结构是一张Ipv4地址入口表,其定义如下:

typedef struct _MIB_IPADDRTABLE {

  DWORD         dwNumEntries;  //表中Ipv4地址入口的个数

  MIB_IPADDRROW table[ANY_SIZE];  //一个指向实现为MIB_IPADDRROW结构数组

                                                                                      //Ipv4地址入口表的指针

} MIB_IPADDRTABLE, *PMIB_IPADDRTABLE;

 

MIB_IPADDRROW结构存储了MIB_IPADDRTABLE结构中特定的Ipv4地址的详细信息,其定义如下:

typedef struct _MIB_IPADDRROW {

  DWORD          dwAddr;   //遵循网络字节序的Ipv4地址

  DWORD          dwIndex;   //Ipv4地址关联的接口索引

  DWORD          dwMask;   //遵循网络字节序的Ipv4地址的子网掩码

  DWORD          dwBCastAddr;   //遵循网络字节序的广播地址,通常是Ipv4地址的//主机号设为全0或全1

  DWORD          dwReasmSize;   //接收到的数据报最大的重组装大小

  unsigned short unused1;   //该参数保留

  unsigned short wType;   //网址类型或状态,这个值可以是下表各个值的组合

} MIB_IPADDRROW, *PMIB_IPADDRROW;

 

GetIpAddrTable函数检索本地计算机上Ipv4地址映射表的接口,并将接口信息以MIB_IPADDRTABLE结构返回,其定义如下:

DWORD GetIpAddrTable(

  __out    PMIB_IPADDRTABLE pIpAddrTable,  //指向存储Ipv4地址映射表的接口的

//指针,这些接口以MIB_IPADDRTABLE结构表示

  __inout  PULONG pdwSize,  //当作为输入参数时,表示plpAddrTable所指向的内存

                                                              //的字节大小;当作为输出参数时,表示plpAddrTable指向的

//内存空间不够,这个值表示实际需要的字节大小

  __in     BOOL bOrder //该参数指示函数返回的映射表中的Ipv4地址是否按升序排

//列,为TRUE时表示进行排序

);

当函数执行成功时,返回值是NO_ERROR;执行失败时,返回值是下表之一:

 

下面是检索IP地址映射表的例子程序:

#include <winsock2.h>

#include <ws2tcpip.h>

#include <iphlpapi.h>

#include <stdio.h>

 

#pragma comment(lib, "iphlpapi.lib")

#pragma comment(lib, "ws2_32.lib")

 

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))

#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))

//也可以使用malloc()free()

 

int __cdecl main()

{

         int i;

         //GetIpAddrTable用到的变量

         PMIB_IPADDRTABLE pIPAddrTable;

         DWORD dwSize = 0;

         DWORD dwRetVal = 0;

         IN_ADDR IPAddr;

 

         //返回错误信息的变量

         LPVOID lpMsgBuf;

 

         //在调用AddIPAddress函数之前我们先调用GetIpAddrTable函数

         //来获得可以用来增加IP地址的适配器

         pIPAddrTable = (MIB_IPADDRTABLE*)MALLOC(sizeof(MIB_IPADDRTABLE));

         if(pIPAddrTable)

         {

                   //第一次调用GetIpAddrTable来获得所需的内存字节大小,并存入dwSize参数中

                   if(GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)

                   {

                            FREE(pIPAddrTable);

                            pIPAddrTable = (MIB_IPADDRTABLE*)MALLOC(dwSize);

                   }

                   if(pIPAddrTable == NULL)

                   {

                            printf("Memory allocation failed for GetIpAddrTable/n");

                            exit(1);

                   }

         }

 

         //第二次调用GetIpAddrTable函数来获得我们实际需要的数据

         if((dwRetVal = GetIpAddrTable(pIPAddrTable, &dwSize, 0)) != NO_ERROR)

         {

                   printf("GetIpAddrTable failed with error %d/n", dwRetVal);

                   if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |

                                               FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),

                                               (LPTSTR)&lpMsgBuf, 0, NULL))

                   {

                            printf("/tError : %s", lpMsgBuf);

                            LocalFree(lpMsgBuf);

                   }

                   exit(1);

         }

 

         printf("/tNum Entries: %ld/n", pIPAddrTable->dwNumEntries);

         for(i=0; i<(int)pIPAddrTable->dwNumEntries; i++)

         {

                   printf("/n/tInterface Index[%d]: /t%ld/n", i, pIPAddrTable->table[i].dwIndex);

                   IPAddr.S_un.S_addr = (u_long)pIPAddrTable->table[i].dwAddr;

                   printf("/tIP Address[%d]:     /t%s/n", i, inet_ntoa(IPAddr));

 

                   IPAddr.S_un.S_addr = (u_long)pIPAddrTable->table[i].dwMask;

                   printf("/tSubnet Mask[%d]:   /t%s/n", i, inet_ntoa(IPAddr));

 

                   IPAddr.S_un.S_addr = (u_long)pIPAddrTable->table[i].dwBCastAddr;

                   printf("/tBroadCast[%d]:   /t%s(%ld)/n", i, inet_ntoa(IPAddr), pIPAddrTable->table[i].dwBCastAddr);

                   printf("/tReassembly size[%d]:   /t%ld/n", i, pIPAddrTable->table[i].dwReasmSize);

 

                   printf("/tType and State[%d]: ", i);

                   if(pIPAddrTable->table[i].wType & MIB_IPADDR_PRIMARY)

                            printf("/tPrimary IP Address");

                   if(pIPAddrTable->table[i].wType & MIB_IPADDR_DYNAMIC)

                            printf("/tDynamic IP Address");

                   if(pIPAddrTable->table[i].wType & MIB_IPADDR_DISCONNECTED)

                            printf("/tAddress is on disconnected interface");

                   if(pIPAddrTable->table[i].wType & MIB_IPADDR_DELETED)

                            printf("/tAddress is being deleted");

                   if(pIPAddrTable->table[i].wType & MIB_IPADDR_TRANSIENT)

                            printf("/tTransient address");

                   printf("/n");

         }

 

         if(pIPAddrTable)

         {

                   FREE(pIPAddrTable);

                   pIPAddrTable = NULL;

         }

         exit(0);

}

在本人ACER电脑上运行结果如下:

posted on 2010-04-23 13:31  android开发实例  阅读(971)  评论(0编辑  收藏  举报

导航