The code you have written above will only get devices of type 'serial port'. Is that what you want?
The code you have written above will only get devices of type 'serial port'. Is that what you want?
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.
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_PRESENT | 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,(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 ()<<"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_DEVI NTERFACE_USB_DEVICE,InterfaceNumber,&Interface_Inf o))
{
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
Last edited by newb; 29th May 2010 at 08:11.
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.
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
Last edited by newb; 29th May 2010 at 11:11.
eg:
Qt Code:
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);To copy to clipboard, switch view to plain text mode
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;
Last edited by squidge; 29th May 2010 at 11:23.
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
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.
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";
}
}
}
As I said above, the reason is because DevicePath will be WCHAR array.
Even when i try to convert it to QString my application gets crashed.
i used like below
qDebug ()<<QString:::fromWCharArray ( pDetData->DevicePath );
Any helps
Yes, because you are still passing a NULL pointer. Read my code fragment again.
newb (31st May 2010)
Bookmarks