Sto provando a utilizzare le funzioni SetupDi per enumerare il percorso del dispositivo di tutti i dispositivi USB collegati. Il percorso del dispositivo è il percorso utilizzato in CreateFile() in modo che possa comunicare con il dispositivo.windows - Come enumerare il percorso del dispositivo di tutti i dispositivi USB collegati?
Tuttavia, SetupDiGetDeviceInterface richiede un GUID di interfaccia ma non sono specificamente alla ricerca di una particolare interfaccia (diversa da tutte le USB connesse). Questa parte è stata commentata come/* ??? */nella sorgente sottostante.
Solutions Tentativo:
ho cercato di fornire GUID_DEVCLASS_UNKNOWN = {0x4d36e97e, 0xe325, 0x11ce, {0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18}}; ma questo ha gettato un errore "non più interfacce".
Ho anche provato a fornire deviceInfoData.ClassGuid in SetupDiGetDeviceInterface ma ottengo lo stesso errore di cui sopra, "non più interfacce".
Domande:
C'è una classe un'interfaccia generale che coprirà tutti i dispositivi USB? (HID, generico, ecc.)
Oppure esiste una funzione alternativa che mi fornirà il percorso per il dispositivo? (Informazioni sulla struttura SP_DEVICE_INTERFACE_DETAIL_DATA restituita da SetupDiGetDeviceInterfaceDetail).
Fonte:
HDEVINFO deviceInfoList
SP_DEVINFO_DATA deviceInfoData;
deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData = NULL;
DWORD requiredLength = 0;
char *hardwareID = 0;
// Retrieve a list of all present devices
deviceInfoList = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE | DIGCF_ALLCLASSES);
if (deviceInfoList == INVALID_HANDLE_VALUE) {
SetupDiDestroyDeviceInfoList(deviceInfoList);
return false;
}
// Iterate over the list
for (DWORD i = 0; SetupDiEnumDeviceInfo(deviceInfoList, i, &deviceInfoData); i++) {
if (deviceInterfaceDetailData) LocalFree(deviceInterfaceDetailData);
requiredLength = 0;
SetupDiGetDeviceRegistryProperty(deviceInfoList, &deviceInfoData, SPDRP_HARDWAREID, &DataT, NULL, 0, &requiredLength);
if (requiredLength <= 0) {
SetupDiDestroyDeviceInfoList(deviceInfoList);
return false;
}
hardwareID = new char[requiredLength]();
SetupDiGetDeviceRegistryProperty(deviceInfoList, &deviceInfoData, SPDRP_HARDWAREID, &DataT, (PBYTE)hardwareID, requiredLength, NULL);
// Parse hardwareID for vendor ID and product ID
delete hardwareID;
hardwareID = 0;
deviceInterfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
// Requires an interface GUID, for which I have none to specify
if (!SetupDiEnumDeviceInterfaces(deviceInfoList, &deviceInfoData, /* ??? */, 0, &deviceInterfaceData)) {
SetupDiDestroyDeviceInfoList(deviceInfoList);
return false;
}
if (!SetupDiGetDeviceInterfaceDetail(deviceInfoList, &deviceInterfaceData, NULL, 0, &requiredLength, NULL)) {
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER && requiredLength > 0) {
deviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR, requiredLength);
if (!deviceInterfaceDetailData) {
SetupDiDestroyDeviceInfoList(deviceInfoList);
return false;
}
} else {
SetupDiDestroyDeviceInfoList(deviceInfoList);
return false;
}
}
deviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(deviceInfoList, &deviceInterfaceData, deviceInterfaceDetailData, requiredLength, NULL, &deviceInfoData)) {
SetupDiDestroyDeviceInfoList(deviceInfoList);
return false;
}
SetupDiDestroyDeviceInfoList(deviceInfoList);
// deviceInterfaceDetailData->DevicePath yields the device path
}
Ho provato il codice indicato nella risposta, ma ottengo un "Si prega di selezionare una macchina di destinazione valida per la distribuzione dalla pagina delle proprietà del progetto". Costruisce ma non posso eseguirlo. Hai avuto lo stesso problema? Sto usando VS 2015 e WDK 10 su Windows 7 – lads