有些时候需要先获取设备管理器里面的硬件信息,才能执行对应的操作,简单的鼓捣了一下,贴出来共享。
具体有两个方法,大致来说差不多,流程一样 ,懒得整理了,直接贴出来原始体。
// PrintDeviceInfo.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <Windows.h> #include <setupapi.h> #pragma comment(lib, "setupapi.lib") void PrintDevicesInfo1() { HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES); if (hDevInfo == INVALID_HANDLE_VALUE) { printf("SetupDiGetClassDevs Err:%d", GetLastError()); return ; }; SP_CLASSIMAGELIST_DATA _spImageData = {0}; _spImageData.cbSize = sizeof(SP_CLASSIMAGELIST_DATA); SetupDiGetClassImageList(&_spImageData); short wIndex = 0; SP_DEVINFO_DATA spDevInfoData = {0}; spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA); while (1) { if (SetupDiEnumDeviceInfo(hDevInfo, wIndex, &spDevInfoData)) { char szBuf[MAX_PATH] = {0}; int wImageIdx = 0; short wItem = 0; if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &spDevInfoData, SPDRP_CLASS, NULL, (PBYTE)szBuf, MAX_PATH, 0)) { wIndex++; continue; }; if (SetupDiGetClassImageIndex(&_spImageData, &spDevInfoData.ClassGuid, &wImageIdx)) { char szName[MAX_PATH] = {0}; DWORD dwRequireSize; // if (!SetupDiGetClassDescription(&spDevInfoData.ClassGuid, szBuf, MAX_PATH, &dwRequireSize)) { wIndex++; continue; }; printf("Class:%s\r\n", szBuf); if (SetupDiGetDeviceRegistryProperty(hDevInfo,&spDevInfoData, SPDRP_FRIENDLYNAME, NULL, (PBYTE)szName, MAX_PATH - 1, 0)) { printf("Device:%s\r\n\r\n", szName); } else if (SetupDiGetDeviceRegistryProperty(hDevInfo,&spDevInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szName, MAX_PATH - 1, 0)) { printf("Device:%s\r\n\r\n", szName); }; }; } else break; wIndex++; }; SetupDiDestroyClassImageList(&_spImageData); } int _tmain(int argc, _TCHAR* argv[]) { HDEVINFO hDevInfo; SP_DEVINFO_DATA DeviceInfoData; DWORD i; // 得到所有设备 HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, 0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES ); if (hDevInfo == INVALID_HANDLE_VALUE) return 0; // 循环列举 DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) { char szClassBuf[MAX_PATH] = { 0 }; char szDescBuf[MAX_PATH] = { 0 }; // 获取类名 if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_CLASS, NULL, (PBYTE)szClassBuf, MAX_PATH - 1, NULL)) continue; //获取设备描述信息 if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szDescBuf, MAX_PATH - 1, NULL)) continue; printf("Class:%s\r\nDesc:%s\r\n\r\n", szClassBuf, szDescBuf); } // 释放 SetupDiDestroyDeviceInfoList(hDevInfo); getchar(); return 0; }
if (SetupDiGetDeviceInstanceId(hDevInfo, &DeviceInfoData, DeviceInstanceId, DeviceInstanceIdSize, NULL))
{
// 从设备实例ID中提取VID和PID
TCHAR* pVidIndex = _tcsstr(DeviceInstanceId, TEXT("VID_"));
if (pVidIndex == NULL) continue;
TCHAR* pPidIndex = _tcsstr(pVidIndex + 4, TEXT("PID_"));
if (pPidIndex == NULL) continue;
USHORT VendorID = _tcstoul(pVidIndex + 4, NULL, 16);
USHORT ProductID = _tcstoul(pPidIndex + 4, NULL, 16);
// 剔除重复的VID和PID
if (!WDK_isExistVidPid( VendorID, ProductID, pVidPid, iTotal ))
{
pVidPid[iTotal].VendorID = VendorID;
pVidPid[iTotal].ProductID = ProductID;
if (++iTotal >= iCapacity) break;
}
}