Getting USB Device path from USB port

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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

回答1:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!