C#枚举硬件设备(升级版)-阿里云开发者社区

开发者社区> 杰克.陈> 正文

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;
}
}
 

 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
怎么设置阿里云服务器安全组?阿里云安全组规则详细解说
阿里云服务器安全组设置规则分享,阿里云服务器安全组如何放行端口设置教程
6473 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
4016 0
使用OpenApi弹性释放和设置云服务器ECS释放
云服务器ECS的一个重要特性就是按需创建资源。您可以在业务高峰期按需弹性的自定义规则进行资源创建,在完成业务计算的时候释放资源。本篇将提供几个Tips帮助您更加容易和自动化的完成云服务器的释放和弹性设置。
7645 0
windows server 2008阿里云ECS服务器安全设置
最近我们Sinesafe安全公司在为客户使用阿里云ecs服务器做安全的过程中,发现服务器基础安全性都没有做。为了为站长们提供更加有效的安全基础解决方案,我们Sinesafe将对阿里云服务器win2008 系统进行基础安全部署实战过程! 比较重要的几部分 1.
5045 0
阿里云服务器安全组设置内网互通的方法
虽然0.0.0.0/0使用非常方便,但是发现很多同学使用它来做内网互通,这是有安全风险的,实例有可能会在经典网络被内网IP访问到。下面介绍一下四种安全的内网互联设置方法。 购买前请先:领取阿里云幸运券,有很多优惠,可到下文中领取。
9347 0
腾讯云服务器 设置ngxin + fastdfs +tomcat 开机自启动
在tomcat中新建一个可以启动的 .sh 脚本文件 /usr/local/tomcat7/bin/ export JAVA_HOME=/usr/local/java/jdk7 export PATH=$JAVA_HOME/bin/:$PATH export CLASSPATH=.
2051 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,云吞铺子总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系统盘、创建快照、配置安全组等操作如何登录ECS云服务器控制台? 1、先登录到阿里云ECS服务器控制台 2、点击顶部的“控制台” 3、通过左侧栏,切换到“云服务器ECS”即可,如下图所示 通过ECS控制台的远程连接来登录到云服务器 阿里云ECS云服务器自带远程连接功能,使用该功能可以登录到云服务器,简单且方便,如下图:点击“远程连接”,第一次连接会自动生成6位数字密码,输入密码即可登录到云服务器上。
16362 0
+关注
杰克.陈
一个安静的程序猿~
9798
文章
2
问答
文章排行榜
最热
最新
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载