#include <stdio.h>
#include <Windows.h>
#include <setupapi.h>
#include <devguid.h>
#pragma comment(lib, "setupapi.lib")
int enum_usb_device_info()
{
int i = 0, res = 0;
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData = { sizeof(DeviceInfoData) };
// get device class information handle
hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_USB, 0, 0, DIGCF_PRESENT);
//hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_USB, 0, 0, DIGCF_ALLCLASSES);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
res = GetLastError();
return res;
}
// enumerute device information
for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
{
printf("\n-------------------[%d]------------------\n", i);
DWORD DataT, buffersize = 2048, req_bufsize = 0;
// 返回设备描述
char dev_desc[2048] = { 0 };
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, &DataT, (LPBYTE)dev_desc, buffersize, &req_bufsize))
{
printf("[err]DESC code:%d\n", GetLastError());
}
else
{
printf("USB device DESC : %s\n", dev_desc);
}
// 返回设备设置类
char dev_class[2048] = { 0 };
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_CLASS, &DataT, (LPBYTE)dev_class, buffersize, &req_bufsize))
{
printf("[err]Class code:%d\n", GetLastError());
}
else
{
printf("USB device Class : %s\n", dev_class);
}
// 返回设备设置类GUID
char dev_class_guid[2048] = { 0 };
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_CLASSGUID, &DataT, (LPBYTE)dev_class_guid, buffersize, &req_bufsize))
{
printf("[err]Class GUID code:%d\n", GetLastError());
}
else
{
printf("USB device Class GUID : %s\n", dev_class_guid);
}
// 返回设备友好名称
char dev_friendly_name[2048] = { 0 };
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, &DataT, (LPBYTE)dev_friendly_name, buffersize, &req_bufsize))
{
printf("[err]Friendly Name code:%d\n", GetLastError());
}
else
{
printf("USB device Friendly Name : %s\n", dev_friendly_name);
}
// 返回用户是否可以获得设备的独占使用权的情况( 如果允许独占使用,则返回值为 1,否则为 0 )
char dev_exclusive[2048] = { 0 };
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_EXCLUSIVE, &DataT, (LPBYTE)dev_exclusive, buffersize, &req_bufsize))
{
printf("[err]Exclusive code:%d\n", GetLastError());
}
else
{
printf("USB device Exclusive : %s\n", dev_exclusive);
}
// 返回硬件 ID 列表
char dev_hardware_id[2048] = { 0 };
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_HARDWAREID, &DataT, (LPBYTE)dev_hardware_id, buffersize, &req_bufsize))
{
printf("[err]Exclusive code:%d\n", GetLastError());
}
else
{
printf("USB device Hardware Id : %s\n", dev_hardware_id);
}
}
return 0;
}
int main() {
enum_usb_device_info();
getchar();
return 0;
}
枚举Windows设备的信息
最新推荐文章于 2023-11-13 13:56:21 发布