C++ 如何枚举所有可用的网络接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5213629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enumerate all available network interfaces?
提问by Andrew
How to enumerate all network interfaces currently available on the computer (including virtual, non-connected, loopback etc)?
如何枚举计算机上当前可用的所有网络接口(包括虚拟、非连接、环回等)?
I need to know their IP4/6, Mask, Gateway, DNS, WINS etc
我需要知道他们的 IP4/6、掩码、网关、DNS、WINS 等
Language:C++, WinAPI
语言:C++、WinAPI
System:Windows 2000 and higher (including Win7)
系统:Windows 2000及以上(含Win7)
采纳答案by bmargulies
Have a look at http://www.codeproject.com/KB/IP/netcfg.aspx. It's a giant example of what you want to do.
看看http://www.codeproject.com/KB/IP/netcfg.aspx。这是您想要做什么的一个很好的例子。
回答by Jerry Coffin
It sounds like you want a combination of a few different functions.
听起来您想要几个不同功能的组合。
To get a list of adapters with their IPv4 addresses/masks, associated WINS servers and DNS servers, you can use GetAdaptersInfo
. To get IPv6 addresses, you can use GetAdaptersAddresses
.
要获取带有 IPv4 地址/掩码、关联的 WINS 服务器和 DNS 服务器的适配器列表,您可以使用GetAdaptersInfo
. 要获取 IPv6 地址,您可以使用GetAdaptersAddresses
.
Depending on what is included in your "etc." you might also want GetIfTable
and GetIfEntry
. GetIfTable
gets a list of network adapters. GetIfEntry
gets you information about each. These retrieve things like the amount of data that's been transmitted/retrieved over a particular interface. There's also a GetIfTable2
/GetIfEntry2
that are only for Vista and newer that get more information.
取决于您的“等”中包含的内容。你可能还想要GetIfTable
和GetIfEntry
。GetIfTable
获取网络适配器列表。GetIfEntry
获取有关每个的信息。这些检索诸如通过特定接口传输/检索的数据量之类的内容。还有一个GetIfTable2
/GetIfEntry2
仅适用于 Vista 和更新版本,可以获取更多信息。
If memory serves, GetIfTable
/GetIfEntry
list allthe interfaces in the machine. GetAdaptersInfo
only works with the "real" adapters.
如果没记错的话,GetIfTable
/GetIfEntry
列出机器中的所有接口。GetAdaptersInfo
仅适用于“真正的”适配器。
回答by Steve-o
Perversely you can use getaddrinfo ("..localmachine")
to enumerate interfaces on Windows 2003+.
相反,您可以使用getaddrinfo ("..localmachine")
枚举 Windows 2003+ 上的接口。
http://msdn.microsoft.com/en-us/library/ms738520(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/ms738520(v=vs.85).aspx
Windows 7 with Service Pack 1 (SP1) and Windows Server 2008 R2 with Service Pack 1 (SP1) add support to hide interfaces.
带有 Service Pack 1 (SP1) 的 Windows 7 和带有 Service Pack 1 (SP1) 的 Windows Server 2008 R2 添加了对隐藏界面的支持。