Dear friends..
Can anybody provide me the example of using SetupDiGetDeviceProperty ()
I have searched via google but i cant get it to work..
Thank you
Dear friends..
Can anybody provide me the example of using SetupDiGetDeviceProperty ()
I have searched via google but i cant get it to work..
Thank you
Following code
#include <windows.h>
#include <devguid.h> // for GUID_DEVCLASS_CDROM etc
#include <setupapi.h>
#include <cfgmgr32.h> // for MAX_DEVICE_ID_LEN, CM_Get_Parent and CM_Get_Device_ID
#define INITGUID
#include "c:\WinDDK\7600.16385.1\inc\api\devpkey.h"
#include <tchar.h>
#include <stdio.h>
#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
#pragma comment (lib, "setupapi.lib")
typedef BOOL (WINAPI *FN_SetupDiGetDeviceProperty)(
__in HDEVINFO DeviceInfoSet,
__in PSP_DEVINFO_DATA DeviceInfoData,
__in const DEVPROPKEY *PropertyKey,
__out DEVPROPTYPE *PropertyType,
__out_opt PBYTE PropertyBuffer,
__in DWORD PropertyBufferSize,
__out_opt PDWORD RequiredSize,
__in DWORD Flags
);
// List all USB devices with some additional information
void ListUsbDevices(void)
{
unsigned i, j;
DWORD dwSize, dwPropertyRegDataType;
DEVPROPTYPE ulPropertyType;
OSVERSIONINFO osvi;
CONFIGRET r;
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
const static LPCTSTR arPrefix[3] = {TEXT("VID_"), TEXT("PID_"), TEXT("MI_")};
TCHAR szDeviceInstanceID [MAX_DEVICE_ID_LEN];
TCHAR szDesc[1024];
LPTSTR pszToken, pszNextToken;
TCHAR szVid[MAX_DEVICE_ID_LEN], szPid[MAX_DEVICE_ID_LEN], szMi[MAX_DEVICE_ID_LEN];
#ifdef UNICODE
FN_SetupDiGetDeviceProperty fn_SetupDiGetDeviceProperty = (FN_SetupDiGetDeviceProperty)
GetProcAddress (GetModuleHandle (TEXT("Setupapi.dll")), "SetupDiGetDevicePropertyW");
#else
FN_SetupDiGetDeviceProperty fn_SetupDiGetDeviceProperty = (FN_SetupDiGetDeviceProperty)
GetProcAddress(GetModuleHandle(TEXT("Setupapi.dll")), "SetupDiGetDevicePropertyA");
#endif
// List all connected USB devices
hDevInfo = SetupDiGetClassDevs (NULL, TEXT("USB"), NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES);
if (hDevInfo == INVALID_HANDLE_VALUE)
return;
// Find the ones that are driverless
for (i = 0; ; i++) {
DeviceInfoData.cbSize = sizeof (DeviceInfoData);
if (!SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData))
break;
r = CM_Get_Device_ID(DeviceInfoData.DevInst, szDeviceInstanceID , MAX_PATH, 0);
if (r != CR_SUCCESS)
continue;
_tprintf (TEXT("%s\n"), szDeviceInstanceID );
if (SetupDiGetDeviceRegistryProperty (hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC,
&dwPropertyRegDataType, (BYTE*)szDesc,
sizeof(szDesc), // The size, in bytes
&dwSize))
_tprintf (TEXT(" Device Description: \"%s\"\n"), szDesc);
// Retreive the device description as reported by the device itself
memset(&osvi, 0, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if ( (GetVersionEx(&osvi) != 0)
&& (osvi.dwBuildNumber >= 7000) ) {
// On Vista and earlier, we can use only SPDRP_DEVICEDESC
// On Windows 7, the information we want ("Bus reported device description") is
// accessed through DEVPKEY_Device_BusReportedDeviceDesc
if (fn_SetupDiGetDeviceProperty && fn_SetupDiGetDeviceProperty (hDevInfo, &DeviceInfoData, &DEVPKEY_Device_BusReportedDeviceDesc,
&ulPropertyType, (BYTE*)szDesc, sizeof(szDesc), &dwSize, 0))
_tprintf (TEXT(" Bus Reported Device Description: \"%s\"\n"), szDesc);
}
pszToken = _tcstok_s (szDeviceInstanceID , TEXT("\\#&"), &pszNextToken);
while(pszToken != NULL) {
szVid[0] = TEXT('\0');
szPid[0] = TEXT('\0');
szMi[0] = TEXT('\0');
for (j = 0; j < 3; j++) {
if (_tcsncmp(pszToken, arPrefix[j], lstrlen(arPrefix[j])) == 0) {
switch(j) {
case 0:
_tcscpy_s(szVid, ARRAY_SIZE(szVid), pszToken);
break;
case 1:
_tcscpy_s(szPid, ARRAY_SIZE(szPid), pszToken);
break;
case 2:
_tcscpy_s(szMi, ARRAY_SIZE(szMi), pszToken);
break;
default:
break;
}
}
}
if (szVid[0] != TEXT('\0'))
_tprintf (TEXT(" vid: \"%s\"\n"), szVid);
if (szPid[0] != TEXT('\0'))
_tprintf (TEXT(" pid: \"%s\"\n"), szPid);
if (szMi[0] != TEXT('\0'))
_tprintf (TEXT(" mi: \"%s\"\n"), szMi);
pszToken = _tcstok_s (NULL, TEXT("\\#&"), &pszNextToken);
}
}
return;
}
int main()
{
ListUsbDevices();
return 0;
}
produce the following output on my Windows 7 computer
USB\VID_046D&PID_08AD\5&1B934348&0&1
Device Description: "Logitech USB Camera (Communicate STX)"
vid: "VID_046D"
pid: "PID_08AD"
USB\VID_046D&PID_08AD&MI_00\6&27D74997&0&0000
Device Description: "Logitech QuickCam Communicate STX"
vid: "VID_046D"
pid: "PID_08AD"
mi: "MI_00"
USB\VID_046D&PID_08AD&MI_01\6&27D74997&0&0001
Device Description: "Logitech Mic (Communicate STX)"
vid: "VID_046D"
pid: "PID_08AD"
mi: "MI_01"
USB\VID_046D&PID_C051\5&92D4DB4&0&1
Device Description: "Logitech USB G3 (MX518) Optical Mouse"
Bus Reported Device Description: "USB-PS/2 Optical Mouse"
vid: "VID_046D"
pid: "PID_C051"
USB\VID_0644&PID_0200\00000308F383
Device Description: "USB Mass Storage Device"
Bus Reported Device Description: "CA-200"
vid: "VID_0644"
pid: "PID_0200"
USB\VID_067B&PID_2305\5&157EE025&0&2
Device Description: "USB Printing Support"
Bus Reported Device Description: "IEEE-1284 Controller"
vid: "VID_067B"
pid: "PID_2305"
USB\VID_152E&PID_1640\0010101640008B615
Device Description: "HLDS USB DVD Storage Device"
Bus Reported Device Description: "SuperMulti RW "
vid: "VID_152E"
pid: "PID_1640"
USB\VID_413C&PID_2003\5&92D4DB4&0&2
Device Description: "USB Input Device"
Bus Reported Device Description: "Dell USB Keyboard"
vid: "VID_413C"
pid: "PID_2003"
The lines with "Bus Reported Device Description" display the results of SetupDiGetDeviceProperty
call.