从USB端口获取USB设备路径

时间:2013-10-01 06:35:11

标签: c++ windows winapi usb

我正在寻找一个使用USB端口名称(USB001)提供USB设备路径(\?\ usb#vid_04a9& pid_1097#207946#{28d78fad-5a12-11d1-ae5b-0000f803a8c2})的示例。

我有一个示例来获取所有已安装端口的设备路径。但我想将USB端口映射到USB打印机设备路径。

代码:

static const GUID GUID_DEVINTERFACE_USBPRINT =    {0x28d78fad,0x5a12,0x11D1,0xae,0x5b,0x00,0x00,0xf8,0x03,0xa8,0xc2};
HDEVINFO devs;
SP_DEVINFO_DATA devinfo;
SP_DEVICE_INTERFACE_DATA devinterface;
GUID intfce;
PSP_DEVICE_INTERFACE_DETAIL_DATA interface_detail;
ULONG index;
ULONG requiredLength;

intfce = GUID_DEVINTERFACE_USBPRINT;

devs = SetupDiGetClassDevs(&intfce,  //&GUID_DEVINTERFACE_USBPRINT,
                           NULL,
                           NULL,
                           DIGCF_PRESENT | DIGCF_ALLCLASSES|  DIGCF_DEVICEINTERFACE);

if (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_INVALID_FLAGS
           || devs == INVALID_HANDLE_VALUE) {
    SetupDiDestroyDeviceInfoList(devs);
    return;
}

ZeroMemory(&devinterface, sizeof(SP_DEVICE_INTERFACE_DATA));
devinterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
devinterface.Flags = 0;

for (index = 0;
     SetupDiEnumDeviceInterfaces(devs,
                                 0,
                                 &intfce,  //&GUID_DEVINTERFACE_USBPRINT,
                                 index,
                                 &devinterface);
     index++)
{
    // Clear out error list
    GetLastError();


 char* interfacename;

 // Allocate space
 interfacename = (char*) malloc(2048);

 // Zero out buffer
 ZeroMemory(interfacename, 2048);

 requiredLength = 0;
 if (!SetupDiGetDeviceInterfaceDetail(devs,
                                      &devinterface,
                                      NULL,
                                      0,
                                      &requiredLength,
                                      NULL)) {

     if (GetLastError() != 122){  // If not wrong size
         char* buf;
         FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                       FORMAT_MESSAGE_FROM_SYSTEM |
                       FORMAT_MESSAGE_IGNORE_INSERTS,
                       NULL,
                       GetLastError(),
                       0,
                       (LPTSTR) &buf,
                       0,
                       NULL);

         char* myerrmsg;
         myerrmsg = (char*) malloc(128);
         wsprintf(myerrmsg,"Error # = %d\n%s", GetLastError(), buf);
         //AfxMessageBox(myerrmsg, MB_OK, 0);
         return;
     }
 }

 interface_detail = (SP_DEVICE_INTERFACE_DETAIL_DATA*) malloc(requiredLength);
 interface_detail = (PSP_DEVICE_INTERFACE_DETAIL_DATA) calloc(1, requiredLength);

 if (interface_detail == NULL)
 {
    // AfxMessageBox("Memory allocation failed!",MB_OK,0);
 }
 else 
 {
     ZeroMemory(interface_detail, sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA));
     interface_detail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
     devinfo.cbSize = sizeof(SP_DEVINFO_DATA);
     SetupDiGetDeviceInterfaceDetail(devs,
                                     &devinterface,
                                     interface_detail,
                                     requiredLength,
                                     &requiredLength,
                                     &devinfo);

     //interfacename = interface_detail->DevicePath;
     strcpy_s(interfacename, 0x800, interface_detail->DevicePath);
}

1 个答案:

答案 0 :(得分:0)

如果您正在寻找打印机设备,请查看PRINTER_INFO_n结构。 PRINTER_INFO_5应该为您提供设备名称。 http://msdn.microsoft.com/en-us/library/windows/desktop/dd162848(v=vs.85).aspx