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);
}
Umesha MS
  • 2,861
  • 8
  • 41
  • 60

1 Answers1

0

If you are looking to for a Printer device look at the PRINTER_INFO_n structures. PRINTER_INFO_5 should give you the device Name. http://msdn.microsoft.com/en-us/library/windows/desktop/dd162848(v=vs.85).aspx

xMRi
  • 14,982
  • 3
  • 26
  • 59
  • I want to get the Device path of a USB printer. For that in the above code, it will loop through all the USB printers and get the device path. But my requirement is User will enter port name. Form the port name i need to map it to device path. – Umesha MS Oct 01 '13 at 07:07