Win32 programming-use setupdi to call a function to enumerate hardware devices

Source: Internet
Author: User
To obtain the list of hardware devices installed in Windows 2000 Program Yes Setupdi Class API functions.

BelowCodeSection shows how to display the list of all installed hardware devices:

# Include <stdio. h>
# Include <windows. h>
# Include <setupapi. h>
# Include <devguid. h>
# Include <regstr. h>

# Pragma comment (Lib, "setupapi. lib ")

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 = (lptstr) 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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.