郭卫东 发表于 2025-3-8 11:07:39

VC++ 获取目的IP的路由

GetBestRoute 函数获取到目的IP的最佳匹配路由。
第一个参数为:destination(目的IP)
第二个参数为:source(源IP)
通常不需要指定第二个source,这个一般用来匹配具体某一个网卡接口路由的,即source等于本机某个网卡的接口IP或网关。

GetBestInterface 函数是获取到目的IP的最佳网卡接口IFR_INDEX。
            bool Router::GetBestRoute(uint32_t destination, uint32_t source, MIB_IPFORWARDROW& route) noexcept
            {
                int err = ::GetBestRoute(destination, source, &route);
                return err == NO_ERROR;
            }

            bool Router::GetBestRoute(uint32_t destination, MIB_IPFORWARDROW& route) noexcept
            {
                return GetBestRoute(destination, 0, route);
            }

            int Router::GetBestInterface(uint32_t ip) noexcept
            {
                DWORD dwBestIfIndex = 0;
                int err = ::GetBestInterface(ip, &dwBestIfIndex);
                if (err != NO_ERROR)
                {
                  return -1;
                }

                return dwBestIfIndex;
            }

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: VC++ 获取目的IP的路由