枚举Windows设备的信息

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值