之前的一句话:大部分文本和代码如下,但我尝试尽可能详细地解释我的问题(随机发生):
我的应用程序通过 Setup API 处理网络接口,随机失败,很可能是在尝试使用SetupDiGetDeviceRegistryProperty()时。我遇到的主要问题是SetupDiEnumDeviceInfo()返回的设备列表(我使用它来获取将传递给SetupDiGetDeviceRegistryProperty()的特定网络适配器的索引)以“意外”方式改变。我的第一个问题是设备从该列表中出现和/或消失。如此处所述,我首先发现使用
const GUID *netGUID = &GUID_DEVCLASS_NET;
SetupDiGetClassDevs(netGUID, 0, 0, DIGCF_PROFILE)
代替
const GUID *netGUID = &GUID_DEVCLASS_NET;
SetupDiGetClassDevs(netGUID, 0, 0, DIGCF_PRESENT)
总是返回可以在设备管理器中看到的所有网络适配器(包括“隐藏”=“未安装”的)。到目前为止,这符合预期 - 使用 Windows 10(64 位)。
在很高兴它工作后,我编译了该应用程序以用于 Windows 7 32 位并对其进行测试。不幸的是,我必须看到仍然存在问题 - SetupDiEnumDeviceInfo() 仍按预期返回所有网络适配器,但顺序完全不同,并且 (a) 如果SetupDiGetDeviceRegistryProperty()因ERROR_INVALID_DATA失败而发生后续调用。大多数时候我得到
WAN Miniport (IP), WAN Miniport (IPv6), WAN Miniport (PPPOE), Microsoft ISATAP Adapter, Microsoft ISATAP Adapter #2, WAN Miniport (PPTP), Microsoft ISATAP Adapter #3, WAN Miniport (SSTP), Intel(R) I210 Gigabit Network Connection, Intel(R) Ethernet Connection I219-LM, RAS Async Adapter, WAN Miniport (IKEv2), WAN Miniport (L2TP), WAN Miniport (Network Monitor)
但有时顺序会这样改变:
Intel(R) I210 Gigabit Network Connection,Intel(R) Ethernet Connection I219-LM,Microsoft ISATAP Adapter,Microsoft ISATAP Adapter #2,Microsoft ISATAP Adapter #3,WAN Miniport (IKEv2),WAN Miniport (L2TP),WAN Miniport (Network Monitor),WAN Miniport (IP),WAN Miniport (IPv6),WAN Miniport (PPPOE),WAN Miniport (PPTP),WAN Miniport (SSTP),RAS Async Adapter
使用下面的(Qt)代码可能会导致我确定的索引不再匹配预期的设备。有没有办法防止设备列表的顺序改变,或者我做错了什么?
// Get a list of all network adpters known by the system
const QStringList adapterNames()
{
HDEVINFO hIfDevs;
quint32 index = 0;
SP_DEVINFO_DATA infoData;
const GUID *netGUID = &GUID_DEVCLASS_NET;
QStringList adapterNames = QStringList();
CHAR friendlyName[MAX_DEVICE_NAME];
hIfDevs = SetupDiGetClassDevs(netGUID, 0, 0, DIGCF_PRESENT);
if (hIfDevs == INVALID_HANDLE_VALUE)
{
qWarning() << "Getting device class info failed with error " << GetLastError();
return QStringList();
}
SecureZeroMemory(&infoData, sizeof(SP_DEVINFO_DATA));
infoData.cbSize = sizeof(SP_DEVINFO_DATA);
while (SetupDiEnumDeviceInfo(hIfDevs, index, &infoData))
{
SecureZeroMemory(friendlyName, sizeof(friendlyName));
if (!SetupDiGetDeviceRegistryPropertyA(hIfDevs, &infoData, SPDRP_FRIENDLYNAME, NULL, (LPBYTE)friendlyName, MAX_DEVICE_NAME, NULL))
{
if (!SetupDiGetDeviceRegistryPropertyA(hIfDevs, &infoData, SPDRP_DEVICEDESC, NULL, (LPBYTE)friendlyName, MAX_DEVICE_NAME, NULL))
{
qWarning() << "SetupDiGetDeviceRegistryPropertyA() for #" << index << "returned error " << GetLastError();
}
}
if(friendlyName[0])
{
adapterNames.append(QString::fromLocal8Bit(friendlyName));
}
index++;
}
SetupDiDestroyDeviceInfoList(hIfDevs);
return adapterNames;
}
被使用:
// Get PCI bus/slot/device/funcntion number (as 0xBBSSDDFF)
bool getPCIHWaddr(quint32 deviceIndex, ...)
{
HDEVINFO hIfDevs;
SP_DEVINFO_DATA infoData;
const GUID *netGUID = &GUID_DEVCLASS_NET;
if (hIfDevs == INVALID_HANDLE_VALUE)
{
qCCritical(lcOS) << "getPCIHWaddr: Getting device class info failed with error " << GetLastError();
return 0;
}
hIfDevs = SetupDiGetClassDevs(netGUID, 0, 0, DIGCF_PRESENT);
if (hIfDevs == INVALID_HANDLE_VALUE)
{
qCritical() << "getPCIHWaddr: Getting device class info failed with error " << GetLastError();
return 0;
}
if (!SetupDiEnumDeviceInfo(hIfDevs, deviceIndex, &infoData))
{
qCritical() << "getPCIHWaddr: Enumerating device information failed with error " << GetLastError();
return 0;
}
// --> In failing scenario, this call returns ERROR_INVALID_DATA (13)
if (!SetupDiGetDeviceRegistryPropertyA(hIfDevs, &infoData, SPDRP_BUSNUMBER, NULL, (LPBYTE)&devProp, sizeof(quint32), NULL))
{
qCritical() << "getPCIHWaddr: Query PCI bus number failed with error " << GetLastError();
return 0;
}
...
}
被使用:
// Get informations on adapter by interface ID found in Registry (HKLM\SYSTEM\CurrentControlSet\Services\TcpIp\Parameter\Interfaces)
bool netIfInfo(const QString &id,...)
{
QStringList adapterNames = adapterNames();
/*
* ...
* Lot of stuff enumerating data from GetIfTable(), GetAdaptersAddresses(), until the expected deivce was found (stored QString deviceDesc)
* ...
*/
// --> Here it happens...
if (!getPCIHWaddr(adapterNames.indexOf(deviceDesc), &PCIAddr, &PnPId))
{
...
}
...
}