Enumerate all hardware devices installed in the system

zhaozj2021-02-16  49

Main part of the implementation:

#include #include #include #pragma Comment (lib, "setupapi.lib")

Clistbox m_strlist;

........

Enumerate the main implementation code of all hardware devices:

m_strList.ResetContent (); HDEVINFO 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;.} // 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 with, // then use the returned buffer size // to Alloc the buffer Keep calling until // success or an unknown failure //.. While (! SetupdiGetDeviceRegistryProperty (HDevinfo, & DeviceInfodata, SPDRP_DEVICEESC, & DATAT, (PBYTE) buffer, buffersize, & buffersize)) {if (GetLastError () == ERROR_INSUFFICIENT_BUFFER) {// Change the buffer size if (buffer) LocalFree (buffer);. Buffer = (char *) LocalAlloc (LPTR, buffersize); } Else {// INSERT ERROR HANDLING Here. Break;}}

m_strList.AddString (buffer); if (buffer) LocalFree (buffer);} if (! GetLastError () = NO_ERROR && GetLastError () = ERROR_NO_MORE_ITEMS!) {// Insert error handling here return;.} // Cleanup SetupDiDestroyDeviceInfoList (hDevInfo );test environment:

WinXP, Win2000 or above operating system!

转载请注明原文地址:https://www.9cbs.com/read-25871.html

New Post(0)