0

I am looking for a sample that gives USB device path (\?\usb#vid_04a9&pid_1097#207946#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}) using USB port name (USB001).

I got a sample to get the device path of all installed ports. But I want to map USB port to USB printer Device path.

Code:

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);
}
4

1 回答 1

0

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

于 2013-10-01T06:39:49.583 回答