gpt4 book ai didi

winapi - 如何通过WinApi判断默认网卡?

转载 作者:行者123 更新时间:2023-12-01 12:29:18 25 4
gpt4 key购买 nike

有没有办法通过 Windows API 来确定哪个是主要/默认网络适配器?

例如,如果我的 PC 有两个网卡,我需要知道系统使用哪个网卡上网,如果我有一个网络适配器和一个虚拟适配器,则类似。

我试过 GetAdaptersAddresses但它没有显示哪个是最喜欢的,也许是 GetBestInterface

最佳答案

如何使用 GetAdaptersInfo并寻找满足您要求的 IP 范围?

或者,遇到这个 (WMI):

https://msdn.microsoft.com/en-us/library/windows/desktop/aa394216(v=vs.85).aspx

~剪断:

Once you have done so, you will likely have reduced your list to one or two configured adapters.

You can also use the following procedure to find the default adapter:

  1. Run the following query: "SELECT InterfaceIndex, Destination FROM Win32_IP4RouteTable WHERE Destination='0.0.0.0'" You should only have one default network destination 0.0.0.0.
  2. Use the InterfaceIndex to retrieve the Network Adapter you want. "SELECT * FROM Win32_NetworkAdapter WHERE InterfaceIndex=" + insertVariableHere

这是一篇声称可以确定默认值的 CodeProject 文章:

http://www.codeproject.com/Articles/13421/Getting-the-Physical-MAC-address-of-a-Network-Inte

获取网络接口(interface)卡的物理 (MAC) 地址并确定它是否是多宿主系统上的主适配器

Finding out if the adapter with the given index is the primary adapter In order to find out if the adapter with the given index is the primary adapter, I had to add a function to the dialog class CNetCfgDlg. This code iterates over the m_pAdapters array, comparing the given adapter index with the index for each adapter in the array. If the given adapter index is equal to the smallest index of all adapters in the array, then it is the primary adapter

还有一件事需要考虑,每个适配器都有“自动度量”设置,它似乎选择最低设置作为首选(尽管不确定如何以编程方式访问此度量设置):

http://www.softminer.net/2011/09/setting-default-network-adapter-in.html

关于winapi - 如何通过WinApi判断默认网卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35879894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com