2012-12-18 42 views
12

Tôi đang cố gắng sử dụng các hàm SetupDi để liệt kê tất cả đường dẫn thiết bị của thiết bị USB được kết nối. Đường dẫn thiết bị là đường dẫn được sử dụng trong CreateFile() để tôi có thể giao tiếp với thiết bị.windows - Cách liệt kê tất cả đường dẫn thiết bị của thiết bị USB được kết nối?

Tuy nhiên, SetupDiGetDeviceInterface yêu cầu giao diện GUID nhưng tôi không đặc biệt tìm kiếm một giao diện cụ thể (trừ tất cả các USB được kết nối). Phần này đã được nhận xét là/* ??? */trong nguồn bên dưới.

Solutions đã thử:

Tôi đã cố gắng để cung cấp GUID_DEVCLASS_UNKNOWN = {0x4d36e97e, 0xe325, 0x11ce, {0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}}; nhưng điều này đã ném một lỗi "không có nhiều giao diện".

Tôi cũng đã cố gắng cung cấp deviceInfoData.ClassGuid vào SetupDiGetDeviceInterface nhưng tôi gặp lỗi tương tự như trên, "không có giao diện nào".

Câu hỏi:

Có một lớp giao diện chung mà sẽ bao gồm tất cả các thiết bị USB? (HID, chung chung, v.v.)

Hoặc có chức năng thay thế nào sẽ cho tôi đường dẫn đến thiết bị không? (Bản sao của cấu trúc SP_DEVICE_INTERFACE_DETAIL_DATA được trả về bởi SetupDiGetDeviceInterfaceDetail).

Nguồn:

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 
} 
+0

Tôi đã thử mã được đưa ra trong câu trả lời nhưng tôi nhận được một 'Vui lòng chọn một máy mục tiêu hợp lệ để triển khai từ trang thuộc tính dự án'. Nó xây dựng nhưng tôi không thể chạy nó. Bạn có cùng một vấn đề? Tôi đang sử dụng VS 2015 và WDK 10 trên Windows 7 – lads

Trả lời

16

MSDN nói rằng có một generic USB lớp giao diện thiết bị tên GUID_DEVINTERFACE_USB_DEVICE với GUID {A5DCBF10-6530-11D2-901F-00C04FB951ED}:

Người tài xế hub USB hệ thống cung cấp đăng ký các trường hợp GUID_DEVINTERFACE_USB_DEVICE để thông báo cho hệ thống và các ứng dụng về sự hiện diện của các thiết bị USB được gắn vào một hub USB.

Here's a code example dường như làm những gì bạn muốn làm, sử dụng GUINTERFACE_USB_DEVICE GUID.

+0

Chỉ cần thử nó ra và nó chính xác những gì tôi đang tìm kiếm! Cảm ơn! – Daniel

+0

Lưu ý cho người đọc trong tương lai: Hãy rất cẩn thận khi bạn đang sử dụng thiết bị ** giao diện ** class GUID và không phải là thiết bị ** setup ** class, chúng không giống nhau. [Xem bài viết này để biết thêm chi tiết.] (Https://docs.microsoft.com/en-us/windows-hardware/drivers/install/setup-classes-versus-interface-classes) – jrh

Các vấn đề liên quan