Setupdigetdeviceproperty Usage Example

SetupDiGetDeviceProperty usage example

The 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 <tchar.h>
#include <stdio.h>

//#include "c:\WinDDK\7600.16385.1\inc\api\devpkey.h"

// include DEVPKEY_Device_BusReportedDeviceDesc from WinDDK\7600.16385.1\inc\api\devpropdef.h
#ifdef DEFINE_DEVPROPKEY
#undef DEFINE_DEVPROPKEY
#endif
#ifdef INITGUID
#define DEFINE_DEVPROPKEY(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8, pid) EXTERN_C const DEVPROPKEY DECLSPEC_SELECTANY name = { { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }, pid }
#else
#define DEFINE_DEVPROPKEY(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8, pid) EXTERN_C const DEVPROPKEY name
#endif // INITGUID

// include DEVPKEY_Device_BusReportedDeviceDesc from WinDDK\7600.16385.1\inc\api\devpkey.h
DEFINE_DEVPROPKEY(DEVPKEY_Device_BusReportedDeviceDesc, 0x540b947e, 0x8b40, 0x45bc, 0xa8, 0xa2, 0x6a, 0x0b, 0x89, 0x4c, 0xbd, 0xa2, 4); // DEVPROP_TYPE_STRING
DEFINE_DEVPROPKEY(DEVPKEY_Device_ContainerId, 0x8c7ed206, 0x3f8a, 0x4827, 0xb3, 0xab, 0xae, 0x9e, 0x1f, 0xae, 0xfc, 0x6c, 2); // DEVPROP_TYPE_GUID
DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 14); // DEVPROP_TYPE_STRING
DEFINE_DEVPROPKEY(DEVPKEY_DeviceDisplay_Category, 0x78c34fc8, 0x104a, 0x4aca, 0x9e, 0xa4, 0x52, 0x4d, 0x52, 0x99, 0x6e, 0x57, 0x5a); // DEVPROP_TYPE_STRING_LIST
DEFINE_DEVPROPKEY(DEVPKEY_Device_LocationInfo, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 15); // DEVPROP_TYPE_STRING
DEFINE_DEVPROPKEY(DEVPKEY_Device_Manufacturer, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 13); // DEVPROP_TYPE_STRING
DEFINE_DEVPROPKEY(DEVPKEY_Device_SecuritySDS, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 26); // DEVPROP_TYPE_SECURITY_DESCRIPTOR_STRING

#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))

#pragma comment (lib, "setupapi.lib")

typedef BOOL (WINAPI *FN_SetupDiGetDevicePropertyW)(
__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 ListDevices (CONST GUID *pClassGuid, LPCTSTR pszEnumerator)
{
unsigned i, j;
DWORD dwSize, dwPropertyRegDataType;
DEVPROPTYPE ulPropertyType;
CONFIGRET status;
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], szHardwareIDs[4096];
WCHAR szBuffer[4096];
LPTSTR pszToken, pszNextToken;
TCHAR szVid[MAX_DEVICE_ID_LEN], szPid[MAX_DEVICE_ID_LEN], szMi[MAX_DEVICE_ID_LEN];
FN_SetupDiGetDevicePropertyW fn_SetupDiGetDevicePropertyW = (FN_SetupDiGetDevicePropertyW)
GetProcAddress (GetModuleHandle (TEXT("Setupapi.dll")), "SetupDiGetDevicePropertyW");

// List all connected USB devices
hDevInfo = SetupDiGetClassDevs (pClassGuid, pszEnumerator, NULL,
pClassGuid != NULL ? DIGCF_PRESENT: DIGCF_ALLCLASSES | DIGCF_PRESENT);
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;

status = CM_Get_Device_ID(DeviceInfoData.DevInst, szDeviceInstanceID , MAX_PATH, 0);
if (status != CR_SUCCESS)
continue;

// Display device instance ID
_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);

if (SetupDiGetDeviceRegistryProperty (hDevInfo, &DeviceInfoData, SPDRP_HARDWAREID,
&dwPropertyRegDataType, (BYTE*)szHardwareIDs,
sizeof(szHardwareIDs), // The size, in bytes
&dwSize)) {
LPCTSTR pszId;
_tprintf (TEXT(" Hardware IDs:\n"));
for (pszId=szHardwareIDs;
*pszId != TEXT('\0') && pszId + dwSize/sizeof(TCHAR) <= szHardwareIDs + ARRAYSIZE(szHardwareIDs);
pszId += lstrlen(pszId)+1) {

_tprintf (TEXT(" \"%s\"\n"), pszId);
}
}

// Retreive the device description as reported by the device itself
// 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_SetupDiGetDevicePropertyW && fn_SetupDiGetDevicePropertyW (hDevInfo, &DeviceInfoData, &DEVPKEY_Device_BusReportedDeviceDesc,
&ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) {

if (fn_SetupDiGetDevicePropertyW (hDevInfo, &DeviceInfoData, &DEVPKEY_Device_BusReportedDeviceDesc,
&ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0))
_tprintf (TEXT(" Bus Reported Device Description: \"%ls\"\n"), szBuffer);
if (fn_SetupDiGetDevicePropertyW (hDevInfo, &DeviceInfoData, &DEVPKEY_Device_Manufacturer,
&ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) {
_tprintf (TEXT(" Device Manufacturer: \"%ls\"\n"), szBuffer);
}
if (fn_SetupDiGetDevicePropertyW (hDevInfo, &DeviceInfoData, &DEVPKEY_Device_FriendlyName,
&ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) {
_tprintf (TEXT(" Device Friendly Name: \"%ls\"\n"), szBuffer);
}
if (fn_SetupDiGetDevicePropertyW (hDevInfo, &DeviceInfoData, &DEVPKEY_Device_LocationInfo,
&ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) {
_tprintf (TEXT(" Device Location Info: \"%ls\"\n"), szBuffer);
}
if (fn_SetupDiGetDevicePropertyW (hDevInfo, &DeviceInfoData, &DEVPKEY_Device_SecuritySDS,
&ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0)) {
// See Security Descriptor Definition Language on MSDN
// (http://msdn.microsoft.com/en-us/library/windows/desktop/aa379567(v=vs.85).aspx)
_tprintf (TEXT(" Device Security Descriptor String: \"%ls\"\n"), szBuffer);
}
if (fn_SetupDiGetDevicePropertyW (hDevInfo, &DeviceInfoData, &DEVPKEY_Device_ContainerId,
&ulPropertyType, (BYTE*)szDesc, sizeof(szDesc), &dwSize, 0)) {
StringFromGUID2((REFGUID)szDesc, szBuffer, ARRAY_SIZE(szBuffer));
_tprintf (TEXT(" ContainerId: \"%ls\"\n"), szBuffer);
}
if (fn_SetupDiGetDevicePropertyW (hDevInfo, &DeviceInfoData, &DEVPKEY_DeviceDisplay_Category,
&ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize, 0))
_tprintf (TEXT(" Device Display Category: \"%ls\"\n"), szBuffer);
}

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 _tmain()
{
// List all connected USB devices
_tprintf (TEXT("---------------\n"));
_tprintf (TEXT("- USB devices -\n"));
_tprintf (TEXT("---------------\n"));
ListDevices(NULL, TEXT("USB"));

_tprintf (TEXT("\n"));
_tprintf (TEXT("-------------------\n"));
_tprintf (TEXT("- USBSTOR devices -\n"));
_tprintf (TEXT("-------------------\n"));
ListDevices(NULL, TEXT("USBSTOR"));

_tprintf (TEXT("\n"));
_tprintf (TEXT("--------------\n"));
_tprintf (TEXT("- SD devices -\n"));
_tprintf (TEXT("--------------\n"));
ListDevices(NULL, TEXT("SD"));

//_tprintf (TEXT("\n"));
//ListDevices(&GUID_DEVCLASS_USB, NULL);
//_tprintf (TEXT("\n"));

_tprintf (TEXT("\n"));
_tprintf (TEXT("-----------\n"));
_tprintf (TEXT("- Volumes -\n"));
_tprintf (TEXT("-----------\n"));
//ListDevices(NULL, TEXT("STORAGE\\VOLUME"));
//_tprintf (TEXT("\n"));
ListDevices(&GUID_DEVCLASS_VOLUME, NULL);

_tprintf (TEXT("\n"));
_tprintf (TEXT("----------------------------\n"));
_tprintf (TEXT("- devices with disk drives -\n"));
_tprintf (TEXT("----------------------------\n"));
ListDevices(&GUID_DEVCLASS_DISKDRIVE, NULL);

return 0;
}

produces the following output on my Windows 7 computer

---------------
- USB devices -
---------------
USB\ROOT_HUB20\4&1C1548F&0
Device Description: "USB Root Hub"
Hardware IDs:
"USB\ROOT_HUB20&VID8086&PID3B3C&REV0006"
"USB\ROOT_HUB20&VID8086&PID3B3C"
"USB\ROOT_HUB20"
USB\ROOT_HUB20\4&2851D18A&0
Device Description: "USB Root Hub"
Hardware IDs:
"USB\ROOT_HUB20&VID8086&PID3B34&REV0006"
"USB\ROOT_HUB20&VID8086&PID3B34"
"USB\ROOT_HUB20"
USB\VID_046D&PID_C52B\6&32FEB3AB&0&2
Device Description: "USB Composite Device"
Hardware IDs:
"USB\VID_046D&PID_C52B&REV_1201"
"USB\VID_046D&PID_C52B"
Bus Reported Device Description: "USB Receiver"
Device Manufacturer: "(Standard USB Host Controller)"
Device Location Info: "Port_#0002.Hub_#0003"
ContainerId: "{AB5F3BBF-21FC-11E2-9436-70F3954A2325}"
vid: "VID_046D"
pid: "PID_C52B"
USB\VID_046D&PID_C52B&MI_00\7&33519F3A&0&0000
Device Description: "USB Input Device (Logitech Download Assistant)"
Hardware IDs:
"USB\VID_046D&PID_C52B&REV_1201&MI_00"
"USB\VID_046D&PID_C52B&MI_00"
Bus Reported Device Description: "USB Receiver"
Device Manufacturer: "Logitech (x64)"
Device Location Info: "0000.001a.0000.001.002.000.000.000.000"
ContainerId: "{AB5F3BBF-21FC-11E2-9436-70F3954A2325}"
vid: "VID_046D"
pid: "PID_C52B"
mi: "MI_00"
USB\VID_046D&PID_C52B&MI_01\7&33519F3A&0&0001
Device Description: "USB Input Device"
Hardware IDs:
"USB\VID_046D&PID_C52B&REV_1201&MI_01"
"USB\VID_046D&PID_C52B&MI_01"
Bus Reported Device Description: "USB Receiver"
Device Manufacturer: "(Standard system devices)"
Device Location Info: "0000.001a.0000.001.002.000.000.000.000"
ContainerId: "{AB5F3BBF-21FC-11E2-9436-70F3954A2325}"
vid: "VID_046D"
pid: "PID_C52B"
mi: "MI_01"
USB\VID_046D&PID_C52B&MI_02\7&33519F3A&0&0002
Device Description: "Logitech Unifying USB receiver"
Hardware IDs:
"USB\VID_046D&PID_C52B&REV_1201&MI_02"
"USB\VID_046D&PID_C52B&MI_02"
Bus Reported Device Description: "USB Receiver"
Device Manufacturer: "Logitech"
Device Location Info: "0000.001a.0000.001.002.000.000.000.000"
ContainerId: "{AB5F3BBF-21FC-11E2-9436-70F3954A2325}"
vid: "VID_046D"
pid: "PID_C52B"
mi: "MI_02"
USB\VID_05C6&PID_9205\6&7A6FBD7&0&4
Device Description: "Qualcomm Gobi 2000 USB Composite Device 9205"
Hardware IDs:
"USB\VID_05C6&PID_9205&REV_0002"
"USB\VID_05C6&PID_9205"
Bus Reported Device Description: "Qualcomm Gobi 2000"
Device Manufacturer: "Qualcomm Incorporated"
Device Location Info: "Port_#0004.Hub_#0004"
ContainerId: "{00000000-0000-0000-FFFF-FFFFFFFFFFFF}"
vid: "VID_05C6"
pid: "PID_9205"
USB\VID_05C6&PID_9205&MI_00\7&210D4D2D&1&0000
Device Description: "Qualcomm Gobi 2000 HS-USB Mobile Broadband Device 9205"
Hardware IDs:
"USB\VID_05C6&PID_9205&REV_0002&MI_00"
"USB\VID_05C6&PID_9205&MI_00"
Bus Reported Device Description: "Qualcomm Gobi 2000"
Device Manufacturer: "Qualcomm Incorporated"
Device Location Info: "0000.001d.0000.001.004.000.000.000.000"
ContainerId: "{00000000-0000-0000-FFFF-FFFFFFFFFFFF}"
vid: "VID_05C6"
pid: "PID_9205"
mi: "MI_00"
USB\VID_05C6&PID_9205&MI_01\7&210D4D2D&1&0001
Device Description: "Qualcomm Gobi 2000 HS-USB Diagnostics 9205"
Hardware IDs:
"USB\VID_05C6&PID_9205&REV_0002&MI_01"
"USB\VID_05C6&PID_9205&MI_01"
Bus Reported Device Description: "Qualcomm Gobi 2000"
Device Manufacturer: "Qualcomm Incorporated"
Device Friendly Name: "Qualcomm Gobi 2000 HS-USB Diagnostics 9205 (COM6)"
Device Location Info: "0000.001d.0000.001.004.000.000.000.000"
ContainerId: "{00000000-0000-0000-FFFF-FFFFFFFFFFFF}"
vid: "VID_05C6"
pid: "PID_9205"
mi: "MI_01"
USB\VID_05C6&PID_9205&MI_02\7&210D4D2D&1&0002
Device Description: "Qualcomm Gobi 2000 HS-USB Modem 9205"
Hardware IDs:
"USB\VID_05C6&PID_9205&REV_0002&MI_02"
"USB\VID_05C6&PID_9205&MI_02"
Bus Reported Device Description: "Qualcomm Gobi 2000"
Device Manufacturer: "Qualcomm Incorporated"
Device Friendly Name: "Qualcomm Gobi 2000 HS-USB Modem 9205"
Device Location Info: "0000.001d.0000.001.004.000.000.000.000"
ContainerId: "{00000000-0000-0000-FFFF-FFFFFFFFFFFF}"
vid: "VID_05C6"
pid: "PID_9205"
mi: "MI_02"
USB\VID_05C6&PID_9205&MI_03\7&210D4D2D&1&0003
Device Description: "Qualcomm Gobi 2000 HS-USB NMEA 9205"
Hardware IDs:
"USB\VID_05C6&PID_9205&REV_0002&MI_03"
"USB\VID_05C6&PID_9205&MI_03"
Bus Reported Device Description: "Qualcomm Gobi 2000"
Device Manufacturer: "Qualcomm Incorporated"
Device Friendly Name: "Qualcomm Gobi 2000 HS-USB NMEA 9205 (COM7)"
Device Location Info: "0000.001d.0000.001.004.000.000.000.000"
ContainerId: "{00000000-0000-0000-FFFF-FFFFFFFFFFFF}"
vid: "VID_05C6"
pid: "PID_9205"
mi: "MI_03"
USB\VID_0781&PID_7108\00000000000000031753
Device Description: "USB Mass Storage Device"
Hardware IDs:
"USB\Vid_0781&Pid_7108&Rev_2000"
"USB\Vid_0781&Pid_7108"
vid: "VID_0781"
pid: "PID_7108"
USB\VID_0930&PID_6545\00D0C9CCDF49EBC06000806C
Device Description: "USB Mass Storage Device"
Hardware IDs:
"USB\Vid_0930&Pid_6545&Rev_0100"
"USB\Vid_0930&Pid_6545"
vid: "VID_0930"
pid: "PID_6545"
USB\VID_0A5C&PID_217F\70F3954A2325
Device Description: "ThinkPad Bluetooth 3.0"
Hardware IDs:
"USB\VID_0A5C&PID_217F&REV_0360"
"USB\VID_0A5C&PID_217F"
Bus Reported Device Description: "Broadcom Bluetooth Device"
Device Manufacturer: "Broadcom"
Device Location Info: "Port_#0004.Hub_#0003"
ContainerId: "{00000000-0000-0000-FFFF-FFFFFFFFFFFF}"
vid: "VID_0A5C"
pid: "PID_217F"
USB\VID_147E&PID_2016\6&32FEB3AB&0&3
Device Description: "TouchChip Fingerprint Coprocessor (WBF advanced mode)"
Hardware IDs:
"USB\VID_147E&PID_2016&REV_0002"
"USB\VID_147E&PID_2016"
Bus Reported Device Description: "Biometric Coprocessor"
Device Manufacturer: "AuthenTec"
Device Location Info: "Port_#0003.Hub_#0003"
Device Security Descriptor String: "D:P(A;;GA;;;BA)(A;;GA;;;SY)"
ContainerId: "{00000000-0000-0000-FFFF-FFFFFFFFFFFF}"
vid: "VID_147E"
pid: "PID_2016"
USB\VID_17EF&PID_480F\6&32FEB3AB&0&6
Device Description: "USB Composite Device"
Hardware IDs:
"USB\VID_17EF&PID_480F&REV_2345"
"USB\VID_17EF&PID_480F"
Bus Reported Device Description: "Integrated Camera"
Device Manufacturer: "(Standard USB Host Controller)"
Device Location Info: "Port_#0006.Hub_#0003"
ContainerId: "{00000000-0000-0000-FFFF-FFFFFFFFFFFF}"
vid: "VID_17EF"
pid: "PID_480F"
USB\VID_17EF&PID_480F&MI_00\7&137E78B0&0&0000
Device Description: "Integrated Camera"
Hardware IDs:
"USB\VID_17EF&PID_480F&REV_2345&MI_00"
"USB\VID_17EF&PID_480F&MI_00"
Bus Reported Device Description: "Integrated Camera"
Device Manufacturer: "Ricoh"
Device Friendly Name: "Integrated Camera"
Device Location Info: "0000.001a.0000.001.006.000.000.000.000"
ContainerId: "{00000000-0000-0000-FFFF-FFFFFFFFFFFF}"
Device Display Category: "Imaging.Webcam"
vid: "VID_17EF"
pid: "PID_480F"
mi: "MI_00"
USB\VID_8087&PID_0020\5&15BBD570&0&1
Device Description: "Generic USB Hub"
Hardware IDs:
"USB\VID_8087&PID_0020&REV_0000"
"USB\VID_8087&PID_0020"
vid: "VID_8087"
pid: "PID_0020"
USB\VID_8087&PID_0020\5&29432BF7&0&1
Device Description: "Generic USB Hub"
Hardware IDs:
"USB\VID_8087&PID_0020&REV_0000"
"USB\VID_8087&PID_0020"
vid: "VID_8087"
pid: "PID_0020"

-------------------
- USBSTOR devices -
-------------------
USBSTOR\DISK&VEN_KINGSTON&PROD_DATATRAVELER_108&REV_PMAP\00D0C9CCDF49EBC06000806C&0
Device Description: "Disk drive"
Hardware IDs:
"USBSTOR\DiskKingstonDataTraveler_108PMAP"
"USBSTOR\DiskKingstonDataTraveler_108"
"USBSTOR\DiskKingston"
"USBSTOR\KingstonDataTraveler_108P"
"KingstonDataTraveler_108P"
"USBSTOR\GenDisk"
"GenDisk"
Bus Reported Device Description: "Kingston DataTraveler 108 USB Device"
Device Manufacturer: "(Standard disk drives)"
Device Friendly Name: "Kingston DataTraveler 108 USB Device"
ContainerId: "{D9CC9C62-4C1D-5CC2-953C-9B0E27AB05E0}"
USBSTOR\DISK&VEN_SANDISK&PROD_CRUZER_TITANIUM&REV_2000\00000000000000031753&0
Device Description: "Disk drive"
Hardware IDs:
"USBSTOR\DiskSanDisk_Cruzer_Titanium_2000"
"USBSTOR\DiskSanDisk_Cruzer_Titanium_"
"USBSTOR\DiskSanDisk_"
"USBSTOR\SanDisk_Cruzer_Titanium_2"
"SanDisk_Cruzer_Titanium_2"
"USBSTOR\GenDisk"
"GenDisk"
Bus Reported Device Description: "SanDisk Cruzer Titanium USB Device"
Device Manufacturer: "(Standard disk drives)"
Device Friendly Name: "SanDisk Cruzer Titanium USB Device"
ContainerId: "{DB834D8A-6F58-11E2-AA64-70F3954A2325}"

--------------
- SD devices -
--------------
SD\VID_74&OID_4A45&PID_USD&REV_1.0\5&3369D5EF&0&0
Device Description: "SD Storage Card"
Hardware IDs:
"SD\VID_74&OID_4a45&PID_USD&REV_1.0"
"SD\VID_74&OID_4a45&PID_USD"
Bus Reported Device Description: "SD Memory Card"
Device Manufacturer: "Generic"
Device Friendly Name: "SD Memory Card"
ContainerId: "{C17922A4-7814-11E2-BF78-70F3954A2325}"
vid: "VID_74"
pid: "PID_USD"

-----------
- Volumes -
-----------
STORAGE\VOLUME\_??_USBSTOR#DISK&VEN_SANDISK&PROD_CRUZER_TITANIUM&REV_2000#00000000000000031753&0#{53F56307-B6BF-11D0-94F2-00A0C91EFB8B}
Device Description: "Generic volume"
Hardware IDs:
"STORAGE\Volume"
STORAGE\VOLUME\{0A6B09D2-D440-11E1-9886-806E6F6E6963}#0000000000100000
Device Description: "Generic volume"
Hardware IDs:
"STORAGE\Volume"
STORAGE\VOLUME\{0A6B09D2-D440-11E1-9886-806E6F6E6963}#0000000006500000
Device Description: "Generic volume"
Hardware IDs:
"STORAGE\Volume"
STORAGE\VOLUME\_??_USBSTOR#DISK&VEN_KINGSTON&PROD_DATATRAVELER_108&REV_PMAP#00D0C9CCDF49EBC06000806C&0#{53F56307-B6BF-11D0-94F2-00A0C91EFB8B}
Device Description: "Generic volume"
Hardware IDs:
"STORAGE\Volume"
STORAGE\VOLUME\_??_SD#VID_74&OID_4A45&PID_USD&REV_1.0#5&3369D5EF&0&0#{53F56307-B6BF-11D0-94F2-00A0C91EFB8B}
Device Description: "Generic volume"
Hardware IDs:
"STORAGE\Volume"
vid: "VID_74"
pid: "PID_USD"

----------------------------
- devices with disk drives -
----------------------------
IDE\DISKSAMSUNG_SSD_830_SERIES__________________CXM02B1Q\4&398487B7&0&0.0.0
Device Description: "Disk drive"
Hardware IDs:
"IDE\DiskSAMSUNG_SSD_830_Series__________________CXM02B1Q"
"IDE\SAMSUNG_SSD_830_Series__________________CXM02B1Q"
"IDE\DiskSAMSUNG_SSD_830_Series__________________"
"SAMSUNG_SSD_830_Series__________________CXM02B1Q"
"GenDisk"
Bus Reported Device Description: "SAMSUNG SSD 830 Series"
Device Manufacturer: "(Standard disk drives)"
Device Friendly Name: "SAMSUNG SSD 830 Series"
Device Location Info: "0"
ContainerId: "{00000000-0000-0000-FFFF-FFFFFFFFFFFF}"
USBSTOR\DISK&VEN_KINGSTON&PROD_DATATRAVELER_108&REV_PMAP\00D0C9CCDF49EBC06000806C&0
Device Description: "Disk drive"
Hardware IDs:
"USBSTOR\DiskKingstonDataTraveler_108PMAP"
"USBSTOR\DiskKingstonDataTraveler_108"
"USBSTOR\DiskKingston"
"USBSTOR\KingstonDataTraveler_108P"
"KingstonDataTraveler_108P"
"USBSTOR\GenDisk"
"GenDisk"
Bus Reported Device Description: "Kingston DataTraveler 108 USB Device"
Device Manufacturer: "(Standard disk drives)"
Device Friendly Name: "Kingston DataTraveler 108 USB Device"
ContainerId: "{D9CC9C62-4C1D-5CC2-953C-9B0E27AB05E0}"
SD\VID_74&OID_4A45&PID_USD&REV_1.0\5&3369D5EF&0&0
Device Description: "SD Storage Card"
Hardware IDs:
"SD\VID_74&OID_4a45&PID_USD&REV_1.0"
"SD\VID_74&OID_4a45&PID_USD"
Bus Reported Device Description: "SD Memory Card"
Device Manufacturer: "Generic"
Device Friendly Name: "SD Memory Card"
ContainerId: "{C17922A4-7814-11E2-BF78-70F3954A2325}"
vid: "VID_74"
pid: "PID_USD"
USBSTOR\DISK&VEN_SANDISK&PROD_CRUZER_TITANIUM&REV_2000\00000000000000031753&0
Device Description: "Disk drive"
Hardware IDs:
"USBSTOR\DiskSanDisk_Cruzer_Titanium_2000"
"USBSTOR\DiskSanDisk_Cruzer_Titanium_"
"USBSTOR\DiskSanDisk_"
"USBSTOR\SanDisk_Cruzer_Titanium_2"
"SanDisk_Cruzer_Titanium_2"
"USBSTOR\GenDisk"
"GenDisk"
Bus Reported Device Description: "SanDisk Cruzer Titanium USB Device"
Device Manufacturer: "(Standard disk drives)"
Device Friendly Name: "SanDisk Cruzer Titanium USB Device"
ContainerId: "{DB834D8A-6F58-11E2-AA64-70F3954A2325}"

The lines with "Bus Reported Device Description" displays results of SetupDiGetDeviceProperty call. You can get some additional information about devices if you would follow another answers: this one and another one.

Why is SetupDiGetDeviceProperty function not working?

SetupDiGetDeviceProperty requires Vista or later, as described in the documentation. You must therefore defined WINVER and _WIN32_WINNT accordingly.

#define WINVER 0x0600
#define _WIN32_WINNT 0x0600

My guess is that your project targets an earlier version of Windows.

Alternatively you can define them in the project options, or on the command line. More details here.

If that is not the answer then is it possible that you are using an out-of-date version of the SDK that pre-dates Vista?

C# reading a device property

That information is not available through WMI. You'll have to use the Setup API for that. Since this is a C/C++ API, you'll have to import and define some types yourself in C#.

P/Invoke definitions

static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);

static readonly Guid DisplayAdapter = new Guid("{5B45201D-F2F2-4F3B-85BB-30FF1F953599}");

[Flags]
enum DiGetClassFlags : uint
{
DIGCF_DEFAULT = 0x00000001, // only valid with DIGCF_DEVICEINTERFACE
DIGCF_PRESENT = 0x00000002,
DIGCF_ALLCLASSES = 0x00000004,
DIGCF_PROFILE = 0x00000008,
DIGCF_DEVICEINTERFACE = 0x00000010,
}

[Flags]
enum DEVPROPTYPE : ulong
{
DEVPROP_TYPEMOD_ARRAY = 0x00001000,
DEVPROP_TYPEMOD_LIST = 0x00002000,

DEVPROP_TYPE_EMPTY = 0x00000000, // nothing, no property data
DEVPROP_TYPE_NULL = 0x00000001, // null property data
DEVPROP_TYPE_SBYTE = 0x00000002, // 8-bit signed int (SBYTE)
DEVPROP_TYPE_BYTE = 0x00000003, // 8-bit unsigned int (BYTE)
DEVPROP_TYPE_INT16 = 0x00000004, // 16-bit signed int (SHORT)
DEVPROP_TYPE_UINT16 = 0x00000005, // 16-bit unsigned int (USHORT)
DEVPROP_TYPE_INT32 = 0x00000006, // 32-bit signed int (LONG)
DEVPROP_TYPE_UINT32 = 0x00000007, // 32-bit unsigned int (ULONG)
DEVPROP_TYPE_INT64 = 0x00000008, // 64-bit signed int (LONG64)
DEVPROP_TYPE_UINT64 = 0x00000009, // 64-bit unsigned int (ULONG64)
DEVPROP_TYPE_FLOAT = 0x0000000A, // 32-bit floating-point (FLOAT)
DEVPROP_TYPE_DOUBLE = 0x0000000B, // 64-bit floating-point (DOUBLE)
DEVPROP_TYPE_DECIMAL = 0x0000000C, // 128-bit data (DECIMAL)
DEVPROP_TYPE_GUID = 0x0000000D, // 128-bit unique identifier (GUID)
DEVPROP_TYPE_CURRENCY = 0x0000000E, // 64 bit signed int currency value (CURRENCY)
DEVPROP_TYPE_DATE = 0x0000000F, // date (DATE)
DEVPROP_TYPE_FILETIME = 0x00000010, // filetime (FILETIME)
DEVPROP_TYPE_BOOLEAN = 0x00000011, // 8-bit boolean (DEVPROP_BOOLEAN)
DEVPROP_TYPE_STRING = 0x00000012, // null-terminated string
DEVPROP_TYPE_STRING_LIST = (DEVPROP_TYPE_STRING | DEVPROP_TYPEMOD_LIST), // multi-sz string list
DEVPROP_TYPE_SECURITY_DESCRIPTOR = 0x00000013, // self-relative binary SECURITY_DESCRIPTOR
DEVPROP_TYPE_SECURITY_DESCRIPTOR_STRING = 0x00000014, // security descriptor string (SDDL format)
DEVPROP_TYPE_DEVPROPKEY = 0x00000015, // device property key (DEVPROPKEY)
DEVPROP_TYPE_DEVPROPTYPE = 0x00000016, // device property type (DEVPROPTYPE)
DEVPROP_TYPE_BINARY = (DEVPROP_TYPE_BYTE | DEVPROP_TYPEMOD_ARRAY), // custom binary data
DEVPROP_TYPE_ERROR = 0x00000017, // 32-bit Win32 system error code
DEVPROP_TYPE_NTSTATUS = 0x00000018, // 32-bit NTSTATUS code
DEVPROP_TYPE_STRING_INDIRECT = 0x00000019, // string resource (@[path\]<dllname>,-<strId>)

MAX_DEVPROP_TYPE = 0x00000019,
MAX_DEVPROP_TYPEMOD = 0x00002000,

DEVPROP_MASK_TYPE = 0x00000FFF,
DEVPROP_MASK_TYPEMOD = 0x0000F000
}

[StructLayout(LayoutKind.Sequential)]
struct SP_DEVINFO_DATA
{
public uint cbSize;
public Guid classGuid;
public uint devInst;
public IntPtr reserved;
}

[StructLayout(LayoutKind.Sequential)]
struct DEVPROPKEY
{
public Guid fmtid;
public UInt32 pid;
}

[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr SetupDiGetClassDevs(ref Guid classGuid, [MarshalAs(UnmanagedType.LPTStr)] string enumerator, IntPtr hwndParent, DiGetClassFlags flags);

[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool SetupDiEnumDeviceInfo([In] IntPtr hDevInfo, [In] uint memberIndex, [In, Out] ref SP_DEVINFO_DATA deviceInfoData);

[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SetupDiGetDeviceProperty([In] IntPtr hDevInfo, [In] ref SP_DEVINFO_DATA deviceInfoData, [In] ref DEVPROPKEY propertyKey, [In, Out] ref DEVPROPTYPE propertyType, [In, Out] byte[] propertyBuffer, [In] uint propertyBufferSize, [In, Out] ref uint requiredSize, [In] uint flags = 0);

Usage

var dpk = new DEVPROPKEY();
dpk.fmtid = new Guid("60b193cb-5276-4d0f-96fc-f173abad3ec6");
dpk.pid = 2;

var displayDevClass = new Guid(DisplayAdapter.ToString());
var hDevInfo = SetupDiGetClassDevs(ref displayDevClass, null, IntPtr.Zero, DiGetClassFlags.DIGCF_PRESENT | DiGetClassFlags.DIGCF_DEVICEINTERFACE);

if (hDevInfo != INVALID_HANDLE_VALUE)
{
uint i = 0;
while (true)
{
var did = new SP_DEVINFO_DATA();
did.cbSize = (uint)Marshal.SizeOf(did);
if (!SetupDiEnumDeviceInfo(hDevInfo, i, ref did)) break;

uint required = 0;
DEVPROPTYPE dpt = 0;
var temp = new byte[0];
SetupDiGetDeviceProperty(hDevInfo, ref did, ref dpk, ref dpt, temp, 0, ref required);
if (required > 0)
{
var data = new byte[required];
if (SetupDiGetDeviceProperty(hDevInfo, ref did, ref dpk, ref dpt, data, required, ref required))
{
Console.WriteLine(BitConverter.ToString(data));
}
}
}
}

Is there an equivalent to SetupDiGetDeviceProperty which is available in Windows XP?

you can use the Win32_PnPEntity WMI class to get most of the information returned by the SetupDiGetDeviceProperty function.

Try this C++ sample.



Related Topics



Leave a reply



Submit