C#枚举硬件设备(升级版)

简介: 原文:C#枚举硬件设备(升级版)先取设备类型: /// /// 设备类型/// class DeviceClasses {public static Guid ClassesGuid;public const int MAX_SIZE_DEVICE_DESCRIPTION = 100...
原文: C#枚举硬件设备(升级版)

先取设备类型: 
/// <summary>
/// 设备类型
/// </summary>
class DeviceClasses
{
public static Guid ClassesGuid;
public const int MAX_SIZE_DEVICE_DESCRIPTION = 1000;
public const int CR_SUCCESS = 0x00000000;
public const int CR_NO_SUCH_VALUE = 0x00000025;
public const int CR_INVALID_DATA = 0x0000001F;
private const int DIGCF_PRESENT = 0x00000002;
private const int DIOCR_INSTALLER = 0x00000001;
private const int MAXIMUM_ALLOWED = 0x02000000;
public const int DMI_MASK = 0x00000001;
public const int DMI_BKCOLOR = 0x00000002;
public const int DMI_USERECT = 0x00000004;

[StructLayout(LayoutKind.Sequential)]
class SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public int DevInst;
public ulong Reserved;
}

[DllImport(
"cfgmgr32.dll")]
private static extern UInt32 CM_Enumerate_Classes(UInt32 ClassIndex, ref Guid ClassGuid, UInt32 Params);

[DllImport(
"setupapi.dll")]
private static extern Boolean SetupDiClassNameFromGuidA(ref Guid ClassGuid, StringBuilder ClassName, UInt32 ClassNameSize, ref UInt32 RequiredSize);

[DllImport(
"setupapi.dll")]
private static extern IntPtr SetupDiGetClassDevsA(ref Guid ClassGuid, UInt32 Enumerator, IntPtr hwndParent, UInt32 Flags);

[DllImport(
"setupapi.dll")]
private static extern Boolean SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);

[DllImport(
"setupapi.dll")]
private static extern IntPtr SetupDiOpenClassRegKeyExA(ref Guid ClassGuid, UInt32 samDesired, int Flags, IntPtr MachineName, UInt32 Reserved);

[DllImport(
"setupapi.dll")]
private static extern Boolean SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, UInt32 MemberIndex, SP_DEVINFO_DATA DeviceInfoData);

[DllImport(
"advapi32.dll")]
private static extern UInt32 RegQueryValueA(IntPtr KeyClass, UInt32 SubKey, StringBuilder ClassDescription, ref UInt32 sizeB);

/// <summary>
/// 设备类型图标信息
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public class SP_CLASSIMAGELIST_DATA
{
public int cbSize;
public ImageList ImageList;
public ulong Reserved;
}
public struct RECT
{
long left;
long top;
long right;
long bottom;
}

/// <summary>
/// 载入图片
/// </summary>
/// <param name="hInstance"></param>
/// <param name="Reserved"></param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern int LoadBitmapW(int hInstance, ulong Reserved);

/// <summary>
/// 获取图标
/// </summary>
/// <param name="ClassImageListData"></param>
/// <returns></returns>
[DllImport("setupapi.dll")]
public static extern Boolean SetupDiGetClassImageList(out SP_CLASSIMAGELIST_DATA ClassImageListData);
[DllImport(
"setupapi.dll")]
public static extern int SetupDiDrawMiniIcon(Graphics hdc, RECT rc, int MiniIconIndex, int Flags);
[DllImport(
"setupapi.dll")]
public static extern bool SetupDiGetClassBitmapIndex(Guid ClassGuid, out int MiniIconIndex);
[DllImport(
"setupapi.dll")]
public static extern int SetupDiLoadClassIcon(ref Guid classGuid, out IntPtr hIcon, out int index);

/// <summary>
/// 枚举设备类型
/// </summary>
/// <param name="ClassIndex"></param>
/// <param name="ClassName"></param>
/// <param name="ClassDescription"></param>
/// <param name="DevicePresent"></param>
/// <returns></returns>
public static int EnumerateClasses(UInt32 ClassIndex, StringBuilder ClassName, StringBuilder ClassDescription, ref bool DevicePresent)
{
Guid ClassGuid
= Guid.Empty;
IntPtr NewDeviceInfoSet;
UInt32 result;
SP_DEVINFO_DATA DeviceInfoData
= new SP_DEVINFO_DATA();
bool resNam = false;
UInt32 RequiredSize
= 0;
result
= CM_Enumerate_Classes(ClassIndex, ref ClassGuid, 0);
DevicePresent
= false;
SP_CLASSIMAGELIST_DATA imagelist
= new SP_CLASSIMAGELIST_DATA();
if (result != CR_SUCCESS)
{
return (int)result;
}
resNam
= SetupDiClassNameFromGuidA(ref ClassGuid, ClassName, RequiredSize, ref RequiredSize);
if (RequiredSize > 0)
{
ClassName.Capacity
= (int)RequiredSize;
resNam
= SetupDiClassNameFromGuidA(ref ClassGuid, ClassName, RequiredSize, ref RequiredSize);
}
NewDeviceInfoSet
= SetupDiGetClassDevsA(ref ClassGuid, 0, IntPtr.Zero, DIGCF_PRESENT);
if (NewDeviceInfoSet.ToInt32() == -1)
{
DevicePresent
= false;
return 0;
}

UInt32 numD
= 0;
DeviceInfoData.cbSize
= 28;
DeviceInfoData.DevInst
= 0;
DeviceInfoData.ClassGuid
= System.Guid.Empty;
DeviceInfoData.Reserved
= 0;

Boolean res1
= SetupDiEnumDeviceInfo(
NewDeviceInfoSet,
numD,
DeviceInfoData);

if (!res1)
{
DevicePresent
= false;
return 0;
}
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
IntPtr KeyClass
= SetupDiOpenClassRegKeyExA(
ref ClassGuid, MAXIMUM_ALLOWED, DIOCR_INSTALLER, IntPtr.Zero, 0);
if (KeyClass.ToInt32() == -1)
{
DevicePresent
= false;
return 0;
}
UInt32 sizeB
= MAX_SIZE_DEVICE_DESCRIPTION;
ClassDescription.Capacity
= MAX_SIZE_DEVICE_DESCRIPTION;
UInt32 res
= RegQueryValueA(KeyClass, 0, ClassDescription, ref sizeB);
if (res != 0) ClassDescription = new StringBuilder(""); //No device description
DevicePresent = true;
ClassesGuid
= DeviceInfoData.ClassGuid;
return 0;
}
}

再取设备信息:

 

 class DeviceInfo
{
private const int DIGCF_PRESENT = (0x00000002);
private const int MAX_DEV_LEN = 1000;
private const int SPDRP_FRIENDLYNAME = (0x0000000C);
private const int SPDRP_DEVICEDESC = (0x00000000);

[StructLayout(LayoutKind.Sequential)]
private class SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public int DevInst;
public ulong Reserved;
};
[DllImport(
"setupapi.dll")]
private static extern Boolean SetupDiClassGuidsFromNameA(string ClassN, ref Guid guids, UInt32 ClassNameSize, ref UInt32 ReqSize);

[DllImport(
"setupapi.dll")]
private static extern IntPtr SetupDiGetClassDevsA(ref Guid ClassGuid, UInt32 Enumerator, IntPtr hwndParent, UInt32 Flags);

[DllImport(
"setupapi.dll")]
private static extern Boolean SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, UInt32 MemberIndex, SP_DEVINFO_DATA DeviceInfoData);

[DllImport(
"setupapi.dll")]
private static extern Boolean SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);

[DllImport(
"setupapi.dll")]
private static extern Boolean SetupDiGetDeviceRegistryPropertyA(IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, UInt32 Property, UInt32 PropertyRegDataType, StringBuilder PropertyBuffer, UInt32 PropertyBufferSize, IntPtr RequiredSize);

/// <summary>
/// 通过设备类型枚举设备信息
/// </summary>
/// <param name="DeviceIndex"></param>
/// <param name="ClassName"></param>
/// <param name="DeviceName"></param>
/// <returns></returns>
public static int EnumerateDevices(UInt32 DeviceIndex, string ClassName, StringBuilder DeviceName)
{
UInt32 RequiredSize
= 0;
Guid guid
= Guid.Empty;
Guid[] guids
= new Guid[1];
IntPtr NewDeviceInfoSet;
SP_DEVINFO_DATA DeviceInfoData
= new SP_DEVINFO_DATA();


bool res = SetupDiClassGuidsFromNameA(ClassName, ref guids[0], RequiredSize, ref RequiredSize);
if (RequiredSize == 0)
{
//类型不正确
DeviceName = new StringBuilder("");
return -2;
}

if (!res)
{
guids
= new Guid[RequiredSize];
res
= SetupDiClassGuidsFromNameA(ClassName, ref guids[0], RequiredSize, ref RequiredSize);

if (!res || RequiredSize == 0)
{
//类型不正确
DeviceName = new StringBuilder("");
return -2;
}
}

//通过类型获取设备信息
NewDeviceInfoSet = SetupDiGetClassDevsA(ref guids[0], 0, IntPtr.Zero, DIGCF_PRESENT);
if (NewDeviceInfoSet.ToInt32() == -1)
{
//设备不可用
DeviceName = new StringBuilder("");
return -3;
}

DeviceInfoData.cbSize
= 28;
//正常状态
DeviceInfoData.DevInst = 0;
DeviceInfoData.ClassGuid
= System.Guid.Empty;
DeviceInfoData.Reserved
= 0;

res
= SetupDiEnumDeviceInfo(NewDeviceInfoSet,
DeviceIndex, DeviceInfoData);
if (!res)
{
//没有设备
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
DeviceName
= new StringBuilder("");
return -1;
}



DeviceName.Capacity
= MAX_DEV_LEN;
if (!SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet, DeviceInfoData,
SPDRP_FRIENDLYNAME,
0, DeviceName, MAX_DEV_LEN, IntPtr.Zero))
{
res
= SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet,
DeviceInfoData, SPDRP_DEVICEDESC,
0, DeviceName, MAX_DEV_LEN, IntPtr.Zero);
if (!res)
{
//类型不正确
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
DeviceName
= new StringBuilder("");
return -4;
}
}
return 0;
}
}
 

 

目录
相关文章
|
6月前
|
程序员 C#
深入理解 C# 编程:枚举、文件处理、异常处理和数字相加
枚举是一个特殊的“类”,表示一组常量(不可更改/只读变量)。 要创建枚举,请使用 enum 关键字(而不是 class 或 interface),并用逗号分隔枚举项:
66 0
|
传感器 物联网 数据管理
.NETCore/C#开发IOT嵌入式设备的个人见解
.NETCore/C#开发IOT嵌入式设备的个人见解
1146 0
.NETCore/C#开发IOT嵌入式设备的个人见解
|
1月前
|
C#
C# 枚举(Enum)
枚举是一组命名整型常量。枚举类型是使用 enum 关键字声明的。 C# 枚举是值类型。换句话说,枚举包含自己的值,且不能继承或传递继承。
41 9
|
2月前
|
存储 C# 开发者
枚举与结构体的应用:C#中的数据组织艺术
在C#编程中,枚举(`enum`)和结构体(`struct`)是非常重要的数据类型。枚举用于定义命名常量集合,提高代码可读性;结构体则封装相关数据字段,适合小型数据集。本文从基本概念入手,探讨它们的使用技巧、常见问题及解决方案,帮助开发者更好地利用这些特性构建健壮的应用程序。
41 8
|
3月前
|
开发框架 .NET C#
C#语言进阶(四) 枚举器和迭代器
C#语言进阶(四) 枚举器和迭代器
34 0
|
6月前
|
安全 API C#
C#.Net筑基-类型系统②常见类型--枚举Enum
枚举(enum)是C#中的一种值类型,用于创建一组命名的整数常量。它们基于整数类型(如int、byte等),默认为int。枚举成员可指定值,未指定则从0开始自动递增。默认值为0。枚举可以与整数类型互相转换,并可通过`[Flags]`特性表示位域,支持位操作,用于多选场景。`System.Enum`类提供了如`HasFlag`、`GetName`等方法进行枚举操作。
|
6月前
|
数据采集 存储 安全
C#手术麻醉系统源码 可对接HIS LIS PACS 医疗系统各类设备
术麻醉信息管理系统主要还是为了手术室开发提供全面帮助的系统,其主要是由监护设备数据采集子系统和麻醉临床系统两个子部分组成。包括从手术申请到手术分配,再到术前访视、术中记录及术后恢复的全过程中都可以得到全方位的保障。
48 4
C#手术麻醉系统源码 可对接HIS  LIS   PACS 医疗系统各类设备
|
存储 C#
C#基础⑥.1——枚举、结构体
枚举是一组命名整型常量。枚举类型是使用 enum 关键字声明的。也就是一些固定范围的值。
|
监控 安全 开发工具
Baumer工业相机堡盟工业相机如何通过BGAPI SDK使用DeviceEvents“设备事件”记录相机事件的各类时间(C#)
Baumer工业相机堡盟工业相机如何通过BGAPI SDK使用DeviceEvents“设备事件”记录相机事件的各类时间(C#)
96 0
【C#视频】常量、枚举、结构体、数组
【C#视频】常量、枚举、结构体、数组