本文介绍了如何获取供应商ID和产品ID的插入的USB设备在Windows上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在Windows平台上使用Qt。 我想从我的本地系统获取并显示插入的usb设备的供应商ID和产品ID。 > 下面是我的完整源代码,从usb设备获取供应商ID和产品ID。 我的qt应用程序它不会让我任何错误。 所以我把usb设备插入系统。 但我的打印语句显示结果如下 一旦你有了这个句柄,你就可以调用。 WINUSB_INTERFACE_HANDLE ( MSDN ),其中 DescriptorType 设置为 URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE 。我现在无法测试代码,但它看起来像这样: USB_DEVICE_DESCRIPTOR udd; memset(& udd,0,sizeof(udd)); ULONG LengthTransferred = 0; WinUsb_GetDescriptor( winusb_interface_handle,//由WinUsbInitialize返回 URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE, 0,//不知道我们需要这个 0x409,// not确定如果我们需要这个& udd, sizeof(udd),& LengthTransferred); 之后, udd-> idVendor 和 udd-> idProduct 应该有你想要的。 Microsoft用来提供所有这些的示例代码 ----------编辑以添加: ---------- Daniel K写道,代码应该是: USB_DEVICE_DESCRIPTOR udd; memset(& udd,0,sizeof(udd)); ULONG LengthTransferred = 0; WinUsb_GetDescriptor( winusb_interface_handle,//由WinUsbInitialize返回 USB_DEVICE_DESCRIPTOR_TYPE,// Daniel K的建议 0, 0x409,//要求英语& udd, sizeof(udd),& LengthTransferred); 有关详情,请参阅评论。 I am using Qt on windows platform.i want to get and display vendor id and product id of a plugged usb device from my local system.Below is my full source code to get the vendor id and product id from the usb device.when i run the my qt application it does not throw me any errors .so i plug the usb device into the system.but my print statement displays the result as belowqDebug ()<<pDetData->DevicePath;i get the result as 0x4Whether i have any implementation mistakes in my source code ?if so please guide me what i am doing wrong..Have i missed out any other functions ?Is it possible to get the vendor id and product id from the usb device based on my source code .( my implementation of the code ) ?kindly find my source code below static GUID GUID_DEVINTERFACE_USB_DEVICE = { 0xA5DCBF10L, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };HANDLE hInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE,NULL,NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);if ( hInfo == INVALID_HANDLE_VALUE ) { qDebug ()<<"invalid"; } else { qDebug ()<<"valid handle"; SP_DEVINFO_DATA DeviceInfoData; DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); SP_INTERFACE_DEVICE_DATA Interface_Info; Interface_Info.cbSize = sizeof(Interface_Info); BYTE Buf[1024]; DWORD i; DWORD InterfaceNumber= 0; PSP_DEVICE_INTERFACE_DETAIL_DATA pspdidd = (PSP_DEVICE_INTERFACE_DETAIL_DATA) Buf; for (i=0;SetupDiEnumDeviceInfo(hInfo,i,&DeviceInfoData);i++) { DWORD DataT; LPTSTR buffer = NULL; DWORD buffersize = 0; while (!SetupDiGetDeviceRegistryProperty( hInfo, &DeviceInfoData, SPDRP_DEVICEDESC, &DataT, (PBYTE)buffer, buffersize, &buffersize)) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // Change the buffer size. if (buffer) LocalFree(buffer); buffer = (LPTSTR)LocalAlloc(LPTR,buffersize); } else { // Insert error handling here. break; } qDebug ()<<(TEXT("Device Number %i is: %s\n"),i, buffer); if (buffer) LocalFree(buffer); if ( GetLastError() != NO_ERROR && GetLastError() != ERROR_NO_MORE_ITEMS ) { // Insert error handling here. qDebug ()<<"return false"; } InterfaceNumber = 0; // this just returns the first one, you can iterate on this if (SetupDiEnumDeviceInterfaces(hInfo, NULL, &GUID_DEVINTERFACE_USB_DEVICE, InterfaceNumber, &Interface_Info)) { printf("Got interface"); DWORD needed; pspdidd->cbSize = sizeof(*pspdidd); SP_DEVICE_INTERFACE_DETAIL_DATA *pDetData = NULL; DWORD dwDetDataSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) + 256; SetupDiGetDeviceInterfaceDetail(hInfo, &Interface_Info, pDetData,dwDetDataSize, NULL, &DeviceInfoData); qDebug ()<<pDetData->DevicePath; //qDebug ()<<QString::fromWCharArray(pDetData->DevicePath); } else { printf("\nNo interface"); //ErrorExit((LPTSTR) "SetupDiEnumDeviceInterfaces"); if ( GetLastError() == ERROR_NO_MORE_ITEMS) printf(", since there are no more items found."); else printf(", unknown reason."); } // Cleanup SetupDiDestroyDeviceInfoList(hInfo); qDebug ()<<"return true"; } }}--------------- Edited to add: -----------------Hi... the application comes and prints thisagain it goes to while loop .... here it gets breaked in the else statement... Qt Code: if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // Change the buffer size. if (buffer) LocalFree(buffer); buffer = (LPTSTR)LocalAlloc(LPTR,buffersize); } else { qDebug ()<<"Here it quits the application"; // Insert error handling here. break; } Any ideas in this.... 解决方案 After this line:SP_DEVICE_INTERFACE_DETAIL_DATA *pDetData = NULL;Add this:DWORD dwDetDataSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA) + 256;pDetData = (_SP_DEVICE_INTERFACE_DETAIL_DATA_A*) malloc (dwDetDataSize);pDetData->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA);After this line:qDebug ()<<pDetData->DevicePath;Add this:free(pDetData);But eventually you're going to have to read the docs for SetupDiGetDeviceInterfaceDetail(). Do it, there are lots of functions that work like this, with pointers to variable-size structs.-------- Edited to add: --------You're really going about this the wrong way. I see you're following the advice you got here, and it's taken you down the wrong path. idVendor and idProduct can only be found in the USB_DEVICE_DESCRIPTOR (MSDN).It looks like you already know how to get the device handle (using CreateFile()). After that, you call WinUsb_Initialize() (MSDN). That gets you a WINUSB_INTERFACE_HANDLE.Once you have that handle, you want to call WinUsb_GetDescriptor() (MSDN), with the DescriptorType set to URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE. I can't test code now, but it will look something like this:USB_DEVICE_DESCRIPTOR udd;memset(&udd, 0, sizeof(udd));ULONG LengthTransferred = 0;WinUsb_GetDescriptor( winusb_interface_handle, // returned by WinUsbInitialize URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE, 0, // not sure if we need this 0x409, // not sure if we need this &udd, sizeof(udd), &LengthTransferred);After that, udd->idVendor and udd->idProduct should have what you want.Microsoft used to supply sample code for all this in the DDK, and probably still does, but I don't have access to one.---------- Edited to add: ----------Daniel K writes that the code should really be:USB_DEVICE_DESCRIPTOR udd;memset(&udd, 0, sizeof(udd));ULONG LengthTransferred = 0;WinUsb_GetDescriptor( winusb_interface_handle, // returned by WinUsbInitialize USB_DEVICE_DESCRIPTOR_TYPE, // Daniel K's suggestion 0, 0x409, // asks for English &udd, sizeof(udd), &LengthTransferred);See the comments for further details. 这篇关于如何获取供应商ID和产品ID的插入的USB设备在Windows上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-19 14:46