PDA

View Full Version : How to get vendor id and product id of a USB device on windows system



newb
27th May 2010, 10:33
I want to get vendor id and product id of usb device which is plugged inside my windows system.

i am trying with win32 api functions.

i can able to know whether device is plugged or unplugged.

but i cant figure out how to get the vendor id and product id information of a usb device .

any links or samples to achieve this.

will this USB_DEVICE_DESCRIPTOR Structure help?

if so any code sample to use that.

Any help would be really great..

Thank you for your time

tbscope
27th May 2010, 10:48
Found this via google:
http://www.intel.com/intelpress/usb/examples/DUSBVC.PDF

newb
27th May 2010, 15:26
Thanks for the information friend.

USB_DEVICE_DESCRIPTOR pDevDesc;

USB_NODE_CONNECTION_INFORMATION pNodeCon;

how can i pass the USB_DEVICE_DESCRIPTOR to the USB_NODE_CONNECTION_INFORMATION structure.

after that i have to use like below for getting device information like vendor id and product id.

DeviceIoControl(hDevice, // device handle
IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION , // info of device property
NULL,
0,
pDevDesc, sizeof(PUSB_DEVICE_DESCRIPTOR), // output data buffer
&dwOutBytes, // out's length
(LPOVERLAPPED)NULL);


I have been searching for a long time but no help .

Kindly help me out.

daemonna
27th May 2010, 16:37
use http://www.libusb.org/

it's cross-platform and GPL, so less worries in future :)

squidge
27th May 2010, 17:42
You can also scan the registry - Windows keeps a list of plugged in USB devices and there VID/PID for driver purposes, but libusb is more portable.

newb
28th May 2010, 05:47
Thank you all for the reply

what about trying with win32 api or WMI.

newb
28th May 2010, 08:09
Dear all,

Kindly find my code which i wrote to get get vendor id and product id of a USB device on windows system

// Creates or opens a file or I/O device.
HANDLE hDevice = CreateFile(TEXT("\\\\.\\G:"), // drive to open
0, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL);

PUSB_DEVICE_DESCRIPTOR pDevDesc;

DeviceIoControl(hDevice,// device handle
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION , // info of device property
NULL,
0,
pDevDesc, sizeof(PUSB_DEVICE_DESCRIPTOR), // output data buffer
&dwOutBytes, // out's length
(LPOVERLAPPED)NULL);


qDebug ()<<pDevDesc->iManufacturer;

qDebug ()<<pDevDesc->idProduct;

qDebug ()<<pDevDesc->bLength;

qDebug ()<<pDevDesc->idVendor;

qDebug ()<<pDevDesc->iSerialNumber;


when i print this i get the below output

0
0
3
0
13

are the above output are correct?

if not how can i achive this please guide me i need to get vendor id and product id from the connected usb device?

Any info would be great help

Thank you for your time.

squidge
28th May 2010, 08:16
Device Manager and right click the device. Windows will show you the VID and PID of the device so you can compare.

But the above seems incorrect.

What are you trying to do exactly? If it's to notice when a device is inserted or removed, there is buildin APIs for this and you will then get a message telling you when a device is inserted or removed - no need to do it yourself. Eg. Look at WM_DEVICECHANGE

newb
28th May 2010, 08:20
To be specific , my application has to display the vendor id and product id of a usb device

Any suggestions on this...

squidge
28th May 2010, 13:12
I'd start with SetupDiGetClassDevs()

newb
28th May 2010, 15:10
Thank you fatjuicymole for your reply.

i did tried that do .

below is the code i tried.

HANDLE h = SetupDiGetClassDevs(&GUID_CLASS_COMPORT,NULL,NULL,DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);

if ( h == INVALID_HANDLE_VALUE )
{
qDebug ()<<"invalid";
}
else
{
qDebug ()<<"valid handle";

qDebug ()<<h;


}
Handle is valid .

can you please lead me further to proceed.

Sorry to be annoying because i am very to new Qt and windows programming

Thank you for your time.

squidge
28th May 2010, 15:19
So your only looking for serial ports?

newb
28th May 2010, 15:43
For the usb device i need to get the device informations

Any helps please

squidge
28th May 2010, 18:00
The code you have written above will only get devices of type 'serial port'. Is that what you want?

schnitzel
28th May 2010, 23:26
does the manufacturer of that particular device you are interested in provide some sort of API?
For example FTDI provides an API free of charge for their USB devices.

newb
29th May 2010, 04:14
Hello fatjuicymole...

please find my code

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_PRESE NT | DIGCF_INTERFACEDEVICE);

if ( hInfo == INVALID_HANDLE_VALUE )
{
qDebug ()<<"invalid";
}
else
{
qDebug ()<<"valid handle";

qDebug ()<<hInfo;


}

SP_DEVINFO_DATA DeviceInfoData;

DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

SP_INTERFACE_DEVICE_DATA Interface_Info;

Interface_Info.cbSize = sizeof(Interface_Info);

//
DWORD i;

DWORD InterfaceNumber= 0;

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 ()<<"hhhh";

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

//DeviceInterfaceData.cbSize = sizeof(DeviceInterfaceData);

if (SetupDiEnumDeviceInterfaces(hInfo,NULL,&GUID_DEVINTERFACE_USB_DEVICE,InterfaceNumber,&Interface_Info))
{

printf("\nGot interface");

DWORD needed;

h = SetupDiGetDeviceInterfaceDetail(hInfo, &Interface_Info, NULL, 0, &needed,&DeviceInfoData);





//qDebug ()<<Interface_Info.InterfaceClassGuid;




}




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









//
}
}


Does this code seem correct?

i dont know how to get the information from the device

please lead me further..

Thank you

squidge
29th May 2010, 10:27
Once you have SetupDiGetDeviceInterfaceDetail working ok, you have the vid and pid already :)

However, you are saying you don't want the information as your passing NULL to that function.

newb
29th May 2010, 10:32
can you please provide me some leads to proceed further please.

i am stuck on this fuctionality.you may save me from this problem.

Looking for your guidence

Thank you for your time

squidge
29th May 2010, 11:15
eg:



SP_DEVICE_INTERFACE_DETAIL_DATA *pDetData = NULL;
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);
bOk = SetupDiGetDeviceInterfaceDetail (hDevInfo, &ifcData, pDetData, dwDetDataSize, NULL, &devdata);


pDetData->DevicePath contains VID and PID in string form (eg. VID_xxxx&PID_xxxx), or you can use HidD_GetAttributes function in HID.DLL to extract these to a structure:

typedef struct _HIDD_ATTRIBUTES {
ULONG Size;
USHORT VendorID;
USHORT ProductID;
USHORT VersionNumber;
} HIDD_ATTRIBUTES, *PHIDD_ATTRIBUTES;

newb
29th May 2010, 11:38
Thank you for the replay friend...

qDebug ()<<pDetData->DevicePath;

when i print this i get the below result.

0x4

can you please tell me why i get this result as above

squidge
29th May 2010, 11:53
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.

newb
29th May 2010, 13:29
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..:crying:

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



}

}

}

squidge
29th May 2010, 14:47
As I said above, the reason is because DevicePath will be WCHAR array.

newb
29th May 2010, 14:59
Even when i try to convert it to QString my application gets crashed.

i used like below

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

Any helps

squidge
29th May 2010, 15:23
Yes, because you are still passing a NULL pointer. Read my code fragment again.

newb
31st May 2010, 04:43
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#{a5dcbf10-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

squidge
31st May 2010, 10:02
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.

newb
31st May 2010, 10:09
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.

squidge
31st May 2010, 10:56
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.

newb
31st May 2010, 11:02
. . . .

newb
31st May 2010, 11:23
Hi...

the application comes and prints this

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

again it goes to while loop ....

here it gets breaked in the else statement...


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....

newb
31st May 2010, 14:41
Hello Qt Experts....

Kindly guide me on my problem...

squidge
31st May 2010, 17:18
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?

newb
1st June 2010, 09:28
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

squidge
1st June 2010, 10:59
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.