Page 2 of 2 FirstFirst 12
Results 21 to 35 of 35

Thread: How to get vendor id and product id of a USB device on windows system

  1. #21
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get vendor id and product id of a USB device on windows system

    DevicePath will be WCHAR array. You'll need to convert it to something like QString before using qDebug() etc. Or you can pass it to CreateFile, then use HidD_GetAttributes to get the above structure filled in.

  2. #22
    Join Date
    Apr 2010
    Posts
    48
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get vendor id and product id of a USB device on windows system

    Thank you for the reply dude...

    Below is my full source code to get the vendor id and product id from the usb device.

    after i run the my qt application i plug the usb device into the system.

    but when i print the pDetData->DevicePath;

    qDebug ()<<pDetData->DevicePath;

    i get the result as 0x4

    Whether 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

    Looking for your guidence..

    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,(PBY TE)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_DEVI NTERFACE_USB_DEVICE,InterfaceNumber,&Interface_Inf o))

    {

    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";



    }

    }

    }

  3. #23
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get vendor id and product id of a USB device on windows system

    As I said above, the reason is because DevicePath will be WCHAR array.

  4. #24
    Join Date
    Apr 2010
    Posts
    48
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get vendor id and product id of a USB device on windows system

    Even when i try to convert it to QString my application gets crashed.

    i used like below

    qDebug ()<<QString:::fromWCharArray ( pDetData->DevicePath );

    Any helps

  5. #25
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get vendor id and product id of a USB device on windows system

    Yes, because you are still passing a NULL pointer. Read my code fragment again.

  6. The following user says thank you to squidge for this useful post:

    newb (31st May 2010)

  7. #26
    Join Date
    Apr 2010
    Posts
    48
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get vendor id and product id of a USB device on windows system

    hi ..
    Thank you...i have corrected my code as you said.
    included these codes...
    SP_DEVICE_INTERFACE_DETAIL_DATA *pDetData = NULL;

    DWORD dwDetDataSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA) + 256;

    pDetData = (SP_DEVICE_INTERFACE_DETAIL_DATA*) malloc (dwDetDataSize);

    pDetData->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA);

    now ..

    qDebug ()<<QString::fromWCharArray ( pDetData->DevicePath );
    the above print statement returns the below
    "\\?\usb#vid_04f2&pid_0111#5&1ba5a77f&0&2#{a5dcbf1 0-6530-11d2-901f-00c04fb951ed}"
    but it seems to be for usb keyboard.
    i want to get the usb device which i plugged...
    how can i get it..
    Looking for some guidence...
    Thank you
    Last edited by newb; 31st May 2010 at 10:16.

  8. #27
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get vendor id and product id of a USB device on windows system

    then look at post #8 in this thread. Since you re-asked your question in post #9, ignoring my post, I assumed you wanted a list of USB devices, not the one that got plugged in.

  9. #28
    Join Date
    Apr 2010
    Posts
    48
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get vendor id and product id of a USB device on windows system

    sorry for the confusion..

    even if it gives the list of usb devices means ..why it displays only one ( usb keyboard )...

    why it doesnot display my usb modem which is plugged ...

    please help me up.
    Last edited by newb; 31st May 2010 at 10:34.

  10. #29
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get vendor id and product id of a USB device on windows system

    You'll need to step through the code and see where it exits your loop. Maybe one of the calls fail. You also seem to have some unnecessary code there, although it is difficult to read.

  11. #30
    Join Date
    Apr 2010
    Posts
    48
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get vendor id and product id of a USB device on windows system

    . . . .
    Last edited by newb; 31st May 2010 at 14:39.

  12. #31
    Join Date
    Apr 2010
    Posts
    48
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get vendor id and product id of a USB device on windows system

    Hi...

    the application comes and prints this

    "\\?\usb#vid_04f2&pid_0111#5&1ba5a77f&0&2#{a5dcbf1 0-6530-11d2-901f-00c04fb951ed}"

    again it goes to while loop ....

    here it gets breaked in the else statement...

    Qt Code:
    1. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
    2. {
    3. // Change the buffer size.
    4. if (buffer) LocalFree(buffer);
    5.  
    6. buffer = (LPTSTR)LocalAlloc(LPTR,buffersize);
    7.  
    8. }
    9.  
    10. else
    11. {
    12. qDebug ()<<"Here it quits the application";
    13.  
    14. // Insert error handling here.
    15. break;
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas in this....
    Last edited by newb; 31st May 2010 at 11:49.

  13. #32
    Join Date
    Apr 2010
    Posts
    48
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get vendor id and product id of a USB device on windows system

    Hello Qt Experts....

    Kindly guide me on my problem...

  14. #33
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get vendor id and product id of a USB device on windows system

    Say, did you write that code or copy it from somewhere? As you don't seem to understand how it works?

    Secondly, why are you expecting the call to fail with an insufficient buffer error code? Surely having a sufficient buffer size is a good thing, rather than a bad one?

  15. #34
    Join Date
    Apr 2010
    Posts
    48
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get vendor id and product id of a USB device on windows system

    Actually i want to get the vendor id and product id of a usb device which is get plugged....

    i searched for Qt related implementation but i don seem to get any sample source code .

    So i searched forums and links for that..

    so i referred the links and followed the code they have used ..

    Any ideas

  16. #35
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get vendor id and product id of a USB device on windows system

    Ok, well if you use what I say in post #8, that will tell you when a device is plugged/unplugged, which seems to be what you want.

    If you google it, there will no doubt be lots of other people who have used it, and thus example code.

Similar Threads

  1. Get the product/device name using QT library?
    By predatorz in forum Newbie
    Replies: 3
    Last Post: 14th January 2010, 03:53
  2. Vendor and Model Name from USB Devices
    By NoRulez in forum Qt Programming
    Replies: 6
    Last Post: 20th July 2009, 09:01
  3. Replies: 1
    Last Post: 19th September 2008, 15:43
  4. Problem whis open device in Windows
    By Pavka in forum Qt Programming
    Replies: 3
    Last Post: 19th November 2007, 10:13
  5. Ho to get the windows system directory with Qt4
    By rmagro in forum Qt Programming
    Replies: 4
    Last Post: 30th August 2007, 13:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.