• VC 获取本机路由表 相当于命令route print



    PMIB_IPFORWARDTABLE MyGetIpForwardTable(BOOL bOrder)
    {
     PMIB_IPFORWARDTABLE pIpRouteTab = NULL;
     DWORD dwActualSize = 0;

     // 查询所需缓冲区的大小
     if(::GetIpForwardTable(pIpRouteTab, &dwActualSize, bOrder) == ERROR_INSUFFICIENT_BUFFER)
     {
      // 为MIB_IPFORWARDTABLE结构申请内存
      pIpRouteTab = (PMIB_IPFORWARDTABLE)::GlobalAlloc(GPTR, dwActualSize);
      // 获取路由表
      if(::GetIpForwardTable(pIpRouteTab, &dwActualSize, bOrder) == NO_ERROR)
       return pIpRouteTab;
      ::GlobalFree(pIpRouteTab);
     }
     return NULL;
    }
    void PrintIpForwardTable()
    {
     PMIB_IPFORWARDTABLE pIpRouteTable = MyGetIpForwardTable(TRUE);
     if(pIpRouteTable != NULL)
     {
      DWORD i, dwCurrIndex;
      struct in_addr inadDest;
      struct in_addr inadMask;
      struct in_addr inadGateway; 
      PMIB_IPADDRTABLE pIpAddrTable = NULL;

      char szDestIp[128];
      char szMaskIp[128];
      char szGatewayIp[128];

      printf("Active Routes:\n\n");

      printf("  Network Address          Netmask  Gateway Address        Interface  Metric\n");
      for (i = 0; i < pIpRouteTable->dwNumEntries; i++)
      {
       dwCurrIndex = pIpRouteTable->table[i].dwForwardIfIndex;

       // 目的地址
       inadDest.s_addr = pIpRouteTable->table[i].dwForwardDest;
       // 子网掩码
       inadMask.s_addr = pIpRouteTable->table[i].dwForwardMask;
       // 网关地址
       inadGateway.s_addr = pIpRouteTable->table[i].dwForwardNextHop;

       strcpy(szDestIp, inet_ntoa(inadDest));
       strcpy(szMaskIp, inet_ntoa(inadMask));
       strcpy(szGatewayIp, inet_ntoa(inadGateway));
       printf("  %15s %16s %16s %16d %7d\n",
        szDestIp,
        szMaskIp,
        szGatewayIp,
        pIpRouteTable->table[i].dwForwardIfIndex,    // 可以在此调用GetIpAddrTable获取索引对应的IP地址
        pIpRouteTable->table[i].dwForwardMetric1);
      }
      //MyFreeIpForwardTable(pIpRouteTable);
     }
    }

  • 相关阅读:
    剑指Offer-Python(6-10)
    Python对MySQL进行增删查改
    剑指Offer-Python(1-5)
    转载:Python中collections模块
    读取单词文件并查某个单词出现的次数
    Python正则表达式-换行的匹配
    Python爬虫-换行的匹配
    转载:Pycharm的常用快捷键
    Python 正则表达式
    Python的类与对象
  • 原文地址:https://www.cnblogs.com/iTaoqi/p/2504085.html
Copyright © 2020-2023  润新知