如何通过WinApi判断默认网卡?
How to determine the default network adapter through WinApi?
有没有办法通过WindowsAPI判断哪个是primary/default网络适配器?
例如,如果我有一台有两块网卡的电脑,我需要知道系统使用哪一块上网,如果我有一个网络适配器和一个虚拟适配器也是如此。
我试过 GetAdaptersAddresses
but it doesn't show which is the favourite one, maybe with 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:
- 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.
- 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
获取网络接口卡的物理 (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
还有一件事需要考虑,每个适配器的 'Automatic Metric' 设置似乎选择最低设置作为首选(尽管不确定如何以编程方式访问此指标设置):
http://www.softminer.net/2011/09/setting-default-network-adapter-in.html
这个SO Answer explains how to determine the local IP address used to connect to the Internet (like Google's DNS servers), you can then compare this local IP address with the list returned by GetAdaptersAddresses判断是用哪个网卡上网。
有没有办法通过WindowsAPI判断哪个是primary/default网络适配器?
例如,如果我有一台有两块网卡的电脑,我需要知道系统使用哪一块上网,如果我有一个网络适配器和一个虚拟适配器也是如此。
我试过 GetAdaptersAddresses
but it doesn't show which is the favourite one, maybe with 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:
- 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.
- 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
获取网络接口卡的物理 (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
还有一件事需要考虑,每个适配器的 'Automatic Metric' 设置似乎选择最低设置作为首选(尽管不确定如何以编程方式访问此指标设置):
http://www.softminer.net/2011/09/setting-default-network-adapter-in.html
这个SO Answer explains how to determine the local IP address used to connect to the Internet (like Google's DNS servers), you can then compare this local IP address with the list returned by GetAdaptersAddresses判断是用哪个网卡上网。