BibinVarghese


What i want to do is identify which type of device has been plugged in to the system. According to the MSDN the following code must work. According to MSDN, if i plug in a Scanner, the following code will recognize it. The SetupDiGetClassRegistryProperty() function will return the device type in the dwData variable. i.e. the true part of the if will execute.

But in practice the
SetupDiGetClassRegistryProperty() never returns the FILE_DEVICE_SCANNER value in dwdata.

DWORD class_datatype = 0, dwData = 0;

HDEVINFO dev_info =SetupDiGetClassDevs(NULL,NULL,NULL,DIGCF_ALLCLASSES | DIGCF_DEVICEINTERFACE );

SP_DEVINFO_DATA DeviceInfoData;
int i=0;
while (i>=0)

{

DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

bRes =SetupDiEnumDeviceInfo(dev_info,(DWORD)i, &DeviceInfoData);

if (GetLastError() == ERROR_NO_MORE_ITEMS)

break;

bRes=SetupDiGetClassRegistryProperty(&DeviceInfoData.ClassGuid, SPCRP_DEVTYPE,&class_datatype,(LPBYTE)&dwData,sizeof(dwData),&required_size,NULL, NULL);

if(dwData == FILE_DEVICE_SCANNER)

{

//DO somethings

}

i++;

}

SetupDiDestroyDeviceInfoList(dev_info);


What am i doing wrong Any help will be appreciated

What i want is that I must be able to programmatically recognize which type of device has been plugged in to the system (eg. scanner, flash drive, external hard disk, printer etc.). The various device types are defined by windows itself ( The following link contains the list of device types defined by windows : http://doc.ddart.net/msdn/header/include/winioctl.h.html ). My application must be able to recognize many of the devices specified there.

Any help regarding any other method to do this will also be appreciated.

Thanks




Re: Visual C++ General How to identify the Type of Device plugged in to the PC

Ramkrishna Pawar

Do you mean you want to identify already attached device Or the one inserted just now




Re: Visual C++ General How to identify the Type of Device plugged in to the PC

Marius Bancila

this looks strange to me:

Code Snippet

bRes =SetupDiEnumDeviceInfo(dev_info,(DWORD)i, &DeviceInfoData);

if (GetLastError() == ERROR_NO_MORE_ITEMS)

Why don't you check bRes first

Anyway, take a look at this example from MSDN: http://msdn2.microsoft.com/en-us/library/aa363250.aspx.






Re: Visual C++ General How to identify the Type of Device plugged in to the PC

Anifaa

Ramkrishna Pawar wrote:
Do you mean you want to identify already attached device Or the one inserted just now


Both. When my program starts executing i want to detect all the currently connected devices and get their device types. After it has started executing, whenever a new device is connected, then my program must be able to detect it and get its device type.

I am able to detect that a device has been plugged in to the pc. But, i am unable to get its device type (like scanner, printer, flash drive, external hard disk, etc.). I need to detect the device type for devices plugged in through USB and FireWire.

Any suggestions

Marius Bancila wrote:

this looks strange to me:
Code Snippet
bRes =SetupDiEnumDeviceInfo(dev_info,(DWORD)i, &DeviceInfoData);
if (GetLastError() == ERROR_NO_MORE_ITEMS)

Why don't you check bRes first


SetupDiEnumDeviceInfo() return a bool which indicates whether it was able to get the data or not.

Marius Bancila wrote:

Anyway, take a look at this example from MSDN: http://msdn2.microsoft.com/en-us/library/aa363250.aspx.


I went through the link given by you. After reading it, i changed the code given there to meet my requirement i.e. i changed the values passed in the SetupDiGetDeviceRegistryProperty() . The code i used is given below :

HDEVINFO hDevInfo;
// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_ALLCLASSES );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return 1;
}

// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData);i++)
{
DWORD buffersize = sizeof(dwData);
DWORD DataT = 0;
dwData =0;
while (!SetupDiGetDeviceRegistryProperty(hDevInfo,&DeviceInfoData,SPDRP_DEVTYPE ,NULL,(PBYTE)&dwData,buffersize,&buffersize))
{
DataT = GetLastError();
if (DataT == ERROR_INSUFFICIENT_BUFFER)
{
if (DataT) //this code is just to break execution while debugging when error occurs
DataT++;
}
else
{
// Insert error handling here.
TCHAR szBuf[80];
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,DataT,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &lpMsgBuf,0, NULL );
wsprintf(szBuf, _T("failed with error %d: %s"), DataT, lpMsgBuf);
MessageBox(NULL, szBuf, _T("Error"), MB_OK);
LocalFree(lpMsgBuf);
break;
}
}

if (dwData)
dwData++; //this code is just to see that a valid value for dwdata was returned while debugging

}

if ( GetLastError()!=NO_ERROR && GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return 1;
}

// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);



In the above code the SetupDiGetDeviceRegistryProperty() always fails. The GetLastError() returns the value 13, which is being translated by FormatMessage() as 'The Data is InValid'.








Re: Visual C++ General How to identify the Type of Device plugged in to the PC

Anifaa


What I need is a code that can detect the Device Type (i.e. scanner, printer, flash drive, External hard disk, external cdrom etc.) of the devices that are connected to a pc through Firewire and/or USB ports.

Any Suggestions