我尝试使用 WMI 来识别物理网络适配器而不是逻辑/软件适配器。我发现了这个 VBScript,它可以满足我的需要:
'WQL Query constants
const QRY_ALL_NIC = "select * from win32_networkadapter"
const QRY_NIC_ASSOC = "associators of {Win32_NetworkAdapter.DeviceID='"
const QRY_NIC_ASSOC2 = "'}"
const IRQCLASS = "Win32_IRQResource"
const DEVMEMCLASS = "Win32_DeviceMemoryAddress"
const PORTCLASS = "Win32_PortResource"
Set wmisrv_cimv2 = GetObject("winmgmts:root/cimv2")
'get a collection of all win32_networkadapters. We query the
'associators of each nic and see if
'any of the associator classes indicate they are physical adapters ie
'do they use memory addresses,'io port or IRQ resources.
set colNics = wmisrv_cimv2.execquery(QRY_ALL_NIC)
for each nic in colNics
set colassoc = wmisrv_cimv2.execquery(QRY_NIC_ASSOC & nic.DeviceID & QRY_NIC_ASSOC2)
for each assoc in colassoc
select case assoc.Path_.Class ' <---- what does Path_ correspond to?
case IRQCLASS, DEVMEMCLASS, PORTCLASS
wscript.echo "Network adapter " & nic.name & " : " & nic.MACAddress & vbcrlf
exit for
case else
end select
next
next
我一直在尝试用 C++ 实现这一点,但我无法弄清楚
Path_
条目的 associator
成员对应什么。
我已经枚举了每个
associator
使用 IWbemClassObject::GetNames()
返回的所有属性,但没有一个被称为 Path_
。有一个名为 __PATH
,但这似乎只是提供了 WMI 命名空间中 associator
的完整路径(并且无论如何都没有 Class
成员)。
我们可以使用 GetIfTable2 并检查
MIB_IF_ROW2::InterfaceAndOperStatusFlags::ConnectorPresent
连接器存在
设置网络接口上是否存在连接器。如果存在物理网络,则设置此值 适配器。
PMIB_IF_TABLE2 Table;
if (NOERROR == GetIfTable2(&Table))
{
if (ULONG NumEntries = Table->NumEntries)
{
PMIB_IF_ROW2 ptb = Table->Table;
do
{
if (ptb->InterfaceAndOperStatusFlags.ConnectorPresent)
{
DbgPrint("%ws\n", ptb->Description);
}
} while (ptb++, --NumEntries);
}
FreeMibTable(Table);
}