Enumerating System Devices on Windows
August 11, 2006 at 5:09 PM
—
Amer Gerzic
Occasionally an application execution depends on installed devices. For example, USB memory card might be needed for storing/reading application specific files. But how do we know that such device is inserted/installed on the system?
Below is the code that enumerates all installed devices on a windows system.
HDEVINFO hInfoList = ::SetupDiGetClassDevs(NULL, NULL, NULL,
DIGCF_PRESENT | DIGCF_ALLCLASSES | DIGCF_PROFILE);
if(hInfoList != INVALID_HANDLE_VALUE)
{
SP_DEVINFO_DATA spdid;
spdid.cbSize = sizeof(SP_DEVINFO_DATA);
DWORD dwIndex = 0;
while(::SetupDiEnumDeviceInfo(hInfoList, dwIndex, &spdid))
{
CString strEntry = _T("");
TCHAR szName[4096] = {0};
if(::SetupDiGetClassDescription(&spdid.ClassGuid,
szName,
4096,
NULL))
strEntry += szName;
strEntry += " | ";
if(::SetupDiGetDeviceRegistryProperty(hInfoList, &spdid, SPDRP_DEVICEDESC, 0,
(PBYTE)szName, 4096, 0))
{
strEntry += szName;
}
strEntry += " | ";
if(::SetupDiGetDeviceRegistryProperty(hInfoList,
&spdid, SPDRP_FRIENDLYNAME, 0,
(PBYTE)szName, 4096, 0))
{
strEntry += szName;
}
m_DevList.AddString(strEntry);
++dwIndex;
}
}
::SetupDiDestroyDeviceInfoList(hInfoList);