本文主要是介绍WinAPI(C++)获取设备管理器中的设备,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
- HID设备
- USB设备
HID设备
获取windows设备管理器上所有通过usb接口接入的hid设备(设备管理器: 人体学输入设备)中显示的设备:
#include <windows.h>
#include <setupapi.h>
#include <hidsdi.h>
#include <iostream>
#include <locale>
#include <codecvt>#pragma comment (lib, "setupapi.lib")
#pragma comment (lib, "hid.lib")
int get_all_hid_devices()
{GUID InterfaceClassGuid = { 0x4D1E55B2, 0xF16F, 0x11CF,{0x88,0xCB,0x00,0x11,0x11,0x00,0x00,0x30} };HDEVINFO hDevInfo;SP_DEVICE_INTERFACE_DATA deviceInterfaceData;PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData = NULL;ULONG requiredSize = 0;hDevInfo = SetupDiGetClassDevs(&InterfaceClassGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);if (hDevInfo == INVALID_HANDLE_VALUE) return 1;deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);for (DWORD i = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &InterfaceClassGuid, i, &deviceInterfaceData); ++i) {SetupDiGetDeviceInterfaceDetail(hDevInfo, &deviceInterfaceData, NULL, 0, &requiredSize, NULL);deviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiredSize);deviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);if (SetupDiGetDeviceInterfaceDetail(hDevInfo, &deviceInterfaceData, deviceInterfaceDetailData, requiredSize, &requiredSize, NULL)) {HANDLE hDevice = CreateFile(deviceInterfaceDetailData->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);if (hDevice != INVALID_HANDLE_VALUE) {HIDD_ATTRIBUTES attributes;HidD_GetAttributes(hDevice, &attributes);printf("VendorID: %04x ProductID: %04x Version: %02x \n", attributes.VendorID, attributes.ProductID, attributes.VersionNumber);CloseHandle(hDevice);}}free(deviceInterfaceDetailData);}SetupDiDestroyDeviceInfoList(hDevInfo);return 0;
}int main()
{get_all_hid_devices();return 0;
}
打印:
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 045e ProductID: 0000 Version: 00
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 045e ProductID: 0000 Version: 00
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 026d ProductID: 0002 Version: 101
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 06cb ProductID: 000f Version: 101
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 06cb ProductID: 000f Version: 101
VendorID: 8087 ProductID: 0a1e Version: 200
VendorID: 046d ProductID: c542 Version: 302
VendorID: 8087 ProductID: 0a1e Version: 200
USB设备
获取USB接入的USB设备(设备管理器: USB连接器管理器):
#include <windows.h>
#include <setupapi.h>
#include <hidsdi.h>
#include <iostream>
#include <locale>
#include <codecvt>#pragma comment (lib, "setupapi.lib")
void PrintDeviceDetails(const TCHAR* hardwareID)
{// 不同的设备有不同的信息,不能通用处理,具体看设备信息取其中的字段if (hardwareID) {printf("\nHardware ID: %S", hardwareID);}
}void EnumerateDevices()
{HDEVINFO deviceInfoSet = SetupDiGetClassDevs((GUID*)&GUID_DEVCLASS_USB, NULL, NULL, DIGCF_PRESENT);if (deviceInfoSet == INVALID_HANDLE_VALUE)return;SP_DEVINFO_DATA deviceInfoData;deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);for (DWORD i = 0; SetupDiEnumDeviceInfo(deviceInfoSet, i, &deviceInfoData); i++){DWORD dataSize = 0;SetupDiGetDeviceRegistryProperty(deviceInfoSet, &deviceInfoData,SPDRP_HARDWAREID, NULL, NULL, 0, &dataSize);TCHAR* hardwareID = (TCHAR*)alloca(dataSize);if (SetupDiGetDeviceRegistryProperty(deviceInfoSet, &deviceInfoData,SPDRP_HARDWAREID, NULL, (PBYTE)hardwareID, dataSize, &dataSize)){PrintDeviceDetails(hardwareID);}}SetupDiDestroyDeviceInfoList(deviceInfoSet);
}
输出打印:
Hardware ID: USB\ROOT_HUB30&VID8086&PID43ED&REV0011
Hardware ID: USB\VID_026D&PID_0002&REV_0101
Hardware ID: PCI\VEN_8086&DEV_9A17&SUBSYS_22D717AA&REV_05
Hardware ID: USB\ROOT_HUB30&VID8086&PID9A17&REV0005
Hardware ID: USB\VID_04F2&PID_B6BE&REV_6118
Hardware ID: hid\vid_1ff7&pid_0f11
Hardware ID: PCI\VEN_8086&DEV_43ED&SUBSYS_22D717AA&REV_11
这篇关于WinAPI(C++)获取设备管理器中的设备的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!