Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to get vendor id and product id of a plugged usb device on windows
    text
    copied!<p>I am using Qt on windows platform.</p> <p>i want to get and display vendor id and product id of a plugged usb device from my local system.</p> <p>Below is my full source code to get the vendor id and product id from the usb device.</p> <p>when i run the my qt application it does not throw me any errors .</p> <p>so i plug the usb device into the system.</p> <p>but my print statement displays the result as below</p> <p><code>qDebug ()&lt;&lt;pDetData-&gt;DevicePath;</code></p> <p>i get the result as 0x4</p> <p>Whether i have any implementation mistakes in my source code ?</p> <p>if so please guide me what i am doing wrong..</p> <p>Have i missed out any other functions ?</p> <p>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 ) ?</p> <p>kindly find my source code below </p> <pre><code>static GUID GUID_DEVINTERFACE_USB_DEVICE = { 0xA5DCBF10L, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } }; HANDLE hInfo = SetupDiGetClassDevs(&amp;GUID_DEVINTERFACE_USB_DEVICE,NULL,NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE); if ( hInfo == INVALID_HANDLE_VALUE ) { qDebug ()&lt;&lt;"invalid"; } else { qDebug ()&lt;&lt;"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,&amp;DeviceInfoData);i++) { DWORD DataT; LPTSTR buffer = NULL; DWORD buffersize = 0; while (!SetupDiGetDeviceRegistryProperty( hInfo, &amp;DeviceInfoData, SPDRP_DEVICEDESC, &amp;DataT, (PBYTE)buffer, buffersize, &amp;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 ()&lt;&lt;(TEXT("Device Number %i is: %s\n"),i, buffer); if (buffer) LocalFree(buffer); if ( GetLastError() != NO_ERROR &amp;&amp; GetLastError() != ERROR_NO_MORE_ITEMS ) { // Insert error handling here. qDebug ()&lt;&lt;"return false"; } InterfaceNumber = 0; // this just returns the first one, you can iterate on this if (SetupDiEnumDeviceInterfaces(hInfo, NULL, &amp;GUID_DEVINTERFACE_USB_DEVICE, InterfaceNumber, &amp;Interface_Info)) { printf("Got interface"); DWORD needed; pspdidd-&gt;cbSize = sizeof(*pspdidd); SP_DEVICE_INTERFACE_DETAIL_DATA *pDetData = NULL; DWORD dwDetDataSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) + 256; SetupDiGetDeviceInterfaceDetail(hInfo, &amp;Interface_Info, pDetData,dwDetDataSize, NULL, &amp;DeviceInfoData); qDebug ()&lt;&lt;pDetData-&gt;DevicePath; //qDebug ()&lt;&lt;QString::fromWCharArray(pDetData-&gt;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 ()&lt;&lt;"return true"; } } } </code></pre> <p><strong>--------------- Edited to add: -----------------</strong></p> <p>Hi... the application comes and prints this</p> <blockquote> <p>\?\usb#vid_04f2&amp;pid_0111#5&amp;1ba5a77f&amp;0&amp;2#{a5dcbf1 0-6530-11d2-901f-00c04fb951ed}</p> </blockquote> <p>again it goes to while loop .... here it gets breaked in the else statement... </p> <p>Qt Code: </p> <pre><code>if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // Change the buffer size. if (buffer) LocalFree(buffer); buffer = (LPTSTR)LocalAlloc(LPTR,buffersize); } else { qDebug ()&lt;&lt;"Here it quits the application"; // Insert error handling here. break; } </code></pre> <p>Any ideas in this.... </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload