To obtain the list of hardware devices installed in Windows 2000, the application can call the setupdi class API function.
Back to Top
More information
The following code snippet shows how to display a list Of all installed hardware devices:
# Include <stdio. h>
# Include <windows. h>
# Include <setupapi. h>
# Include <devguid. h>
# Include <regstr. h>
Int main (INT argc, char * argv [], char * envp [])
{
Hdevinfo;
Sp_devinfo_data deviceinfodata;
Dword I;
// Create a hdevinfo with all present devices.
Hdevinfo = setupdigetclassdevs (null,
0, // enumerator
0,
Digcf_present | 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 datat;
Lptstr buffer = NULL;
DWORD buffersize = 0;
//
// Call function with null to begin,
// Then use the returned buffer size
// To alloc the buffer. keep calling
// Success or an unknown failure.
//
While (! Setupdigetdeviceregistryproperty (
Hdevinfo,
& Deviceinfodata,
Spdrp_devicedesc,
& Datat,
(Pbyte) buffer,
Buffersize,
& Buffersize ))
{
If (getlasterror () =
Error_insufficient_buffer)
{
// Change the buffer size.
If (buffer) localfree (buffer );
Buffer = localalloc (lptr, buffersize );
}
Else
{
// Insert error handling here.
Break;
}
}
Printf ("Result: [% s] \ n", buffer );
If (buffer) localfree (buffer );
}
If (getlasterror ()! = No_error &&
Getlasterror ()! = Error_no_more_items)
{
// Insert error handling here.
Return 1;
}
// Cleanup
Setupdidestroydeviceinfolist (hdevinfo );
Return 0;
}
The following code snippet shows how to retrieve all display adapter groups on the system:
Hdevinfo = setupdigetclassdevs (
(Lpguid) & guid_devclass_display,
0,
0,
Digcf_present );
The following code snippet shows how to retrieve all device groups on the Peripheral Component Interconnect (PCI) bus:
Hdevinfo = setupdigetclassdevs (null,
Regstr_key_pcienum, // enumerator
0,
Digcf_present | digcf_allclasses );
The Windows API function that requires the device instance handle (such as the config manager group of the API function) to use
The value of devinst in the sp_devinfo_data structure returned by the setupdienumdeviceinfo function.