C#枚举硬件设备(升级版)
先取设备类型:
/// <summary>
/// 设备类型
/// </summary>
class DeviceClasses
{
public static Guid ClassesGuid;
public const int MAX_SIZE_DEVICE_DESCRIPTION = ;
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 = ;
result = CM_Enumerate_Classes(ClassIndex, ref ClassGuid, );
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 > )
{
ClassName.Capacity = (int)RequiredSize;
resNam = SetupDiClassNameFromGuidA(ref ClassGuid, ClassName, RequiredSize, ref RequiredSize);
}
NewDeviceInfoSet = SetupDiGetClassDevsA(ref ClassGuid, , IntPtr.Zero, DIGCF_PRESENT);
if (NewDeviceInfoSet.ToInt32() == -)
{
DevicePresent = false;
return ;
}
UInt32 numD = ;
DeviceInfoData.cbSize = ;
DeviceInfoData.DevInst = ;
DeviceInfoData.ClassGuid = System.Guid.Empty;
DeviceInfoData.Reserved = ;
Boolean res1 = SetupDiEnumDeviceInfo(
NewDeviceInfoSet,
numD,
DeviceInfoData);
if (!res1)
{
DevicePresent = false;
return ;
}
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
IntPtr KeyClass = SetupDiOpenClassRegKeyExA(
ref ClassGuid, MAXIMUM_ALLOWED, DIOCR_INSTALLER, IntPtr.Zero, );
if (KeyClass.ToInt32() == -)
{
DevicePresent = false;
return ;
}
UInt32 sizeB = MAX_SIZE_DEVICE_DESCRIPTION;
ClassDescription.Capacity = MAX_SIZE_DEVICE_DESCRIPTION;
UInt32 res = RegQueryValueA(KeyClass, , ClassDescription, ref sizeB);
if (res != ) ClassDescription = new StringBuilder(""); //No device description
DevicePresent = true;
ClassesGuid = DeviceInfoData.ClassGuid;
return ;
}
}
再取设备信息:
class DeviceInfo
{
private const int DIGCF_PRESENT = (0x00000002);
private const int MAX_DEV_LEN = ;
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 = ;
Guid guid = Guid.Empty;
Guid[] guids = new Guid[];
IntPtr NewDeviceInfoSet;
SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA();
bool res = SetupDiClassGuidsFromNameA(ClassName, ref guids[], RequiredSize, ref RequiredSize);
if (RequiredSize == )
{
//类型不正确
DeviceName = new StringBuilder("");
return -;
}
if (!res)
{
guids = new Guid[RequiredSize];
res = SetupDiClassGuidsFromNameA(ClassName, ref guids[], RequiredSize, ref RequiredSize);
if (!res || RequiredSize == )
{
//类型不正确
DeviceName = new StringBuilder("");
return -;
}
}
//通过类型获取设备信息
NewDeviceInfoSet = SetupDiGetClassDevsA(ref guids[], , IntPtr.Zero, DIGCF_PRESENT);
if (NewDeviceInfoSet.ToInt32() == -)
{
//设备不可用
DeviceName = new StringBuilder("");
return -;
}
DeviceInfoData.cbSize = ;
//正常状态
DeviceInfoData.DevInst = ;
DeviceInfoData.ClassGuid = System.Guid.Empty;
DeviceInfoData.Reserved = ;
res = SetupDiEnumDeviceInfo(NewDeviceInfoSet,
DeviceIndex, DeviceInfoData);
if (!res)
{
//没有设备
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
DeviceName = new StringBuilder("");
return -;
}
DeviceName.Capacity = MAX_DEV_LEN;
if (!SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet, DeviceInfoData,
SPDRP_FRIENDLYNAME, , DeviceName, MAX_DEV_LEN, IntPtr.Zero))
{
res = SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet,
DeviceInfoData, SPDRP_DEVICEDESC, , DeviceName, MAX_DEV_LEN, IntPtr.Zero);
if (!res)
{
//类型不正确
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
DeviceName = new StringBuilder("");
return -;
}
}
return ;
}
}
C#枚举硬件设备(升级版)的更多相关文章
- usb由于其配置信息(注册表中的)不完整或已损坏,Windows 无法启动这个硬件设备
在设备管理器的usb设备的属性中,显示提示“由于其配置信息(注册表中的)不完整或已损坏,Windows 无法启动这个硬件设备”.注册表坏了.经过查询,解决方法如下: 方法:打开注册表编辑器(开始--& ...
- 使用RSA非对称密钥算法实现硬件设备授权
一.硬件设备授权 即用户在硬件设备输入一个序列号(或一个包含授权信息的文件),然后硬件设备便可正常使用. 二.授权方案 构思授权方案时,参考了下面网址的思路: http://bbs.csdn.n ...
- USB设备不能用。提示Windows 无法启动这个硬件设备。 (代码 19)
USB,由于其配置信息(注册表中的)不完整或已损坏, Windows 无法启动这个硬件设备. (代码 19) 原因:提示Windows 无法启动这个硬件设备. (代码 19) 处理解决方法: 1) r ...
- MFC枚举USB设备碰到的一个疑难,还没解决
代码如下: 打开USB Hub设备之后,返回句柄hHubDevice,然后使用EnumerateHubPorts来枚举Hub的端 口.疑问在代码的中文注释中. bool CUsbEnumHub::En ...
- Android系统修改硬件设备访问权限
Android系统修改硬件设备访问权限 在硬件抽象层模块文件(so)文件中,提供的函数调用open函数来打开设备文件,比如/dev/gpio,如果不修改设备文件/dev/gpio的访问权限,那么应用程 ...
- Studio 5000编程:如何判断AB PLC系统中的硬件设备是否在正常工作
前言:PLC控制系统,主要由CPU.本机架I/O模块,分布式I/O模块,通信模块,或其他设备(如:伺服驱动器.交换机.第三方设备)等组成,如何判断这些设备是否工作正常?或是一旦出现故障,能在第一时间判 ...
- linux 系统下有sda和hda的硬件设备分别代表什么意思
linux 系统下有sda和hda的硬件设备分别代表什么意思/dev/sda1 # SCSI设备,sda,sdb,sdc,三块盘,1,2,3代表分区(PV)/dev/sda2/dev/sdb1/dev ...
- linux 挂载硬件设备
mount命令用于挂载文件系统,格式为:“mount 文件系统 挂载目录”. 挂载是在使用硬件设备前的最后操作的一步,只需要用mount命令把硬件设备与一个目录做关联,然后就能在这个目录中看到硬件设备 ...
- lspci通过系统总线查看硬件设备信息
lspci - 列出所有PCI设备 PCI 的科普: PCI(Peripheral Component Interconnect),是一种连接电子计算机主板和外部设备的总线标准. 常见的PCI卡包括网 ...
随机推荐
- JSP动作学习一
创建一个简单的模拟登陆功能的网页,没有用数据库技术,只是简单的学习jsp语法 首先创建一个登陆页面 <%@ page language = "java" import=&qu ...
- 微软职位内部推荐-Senior Software Engineer
微软近期Open的职位: Are you looking for a big challenge? Do you know why Big Data is the next frontier for ...
- ORACLE 检查数据库表中是否存在不规范字 段的语句参考.sql
--查看是否有除number,char,date,varchar2,clob/blob之外的类型,比如:NVARCHAR2,TIMESTAMP(6),FLOATSELECT DISTINCT a.DA ...
- 双系统修改启动项顺序&&&修改开机启动等待时间
1. 双系统修改启动项顺序 更改/etc/grub.d目录 下的文件名是可行的 默认情况下Windows 7对应的文件名是30_os-prober,第一个linux系统对应的是10- ...
- camera render texture 游戏里的监控视角
Camera里: 新建render texture并拖入到target texture里 新建材质球 拖入render texture camera里的视角会在材质球上出现 新建一个pla ...
- linux内核分析之fork()
从一个比较有意思的题开始说起,最近要找工作无意间看到一个关于unix/linux中fork()的面试题: #include<sys/types.h> #include<stdio.h ...
- IOC框架的认识
转:http://blog.csdn.net/wanghao72214/article/details/3969594 1 IoC理论的背景 我们都知道,在采用面向对象方法设计的软件系统中,它的底层实 ...
- websphere OSGi应用环境下服务调用saaj包加载问题分析报告
websphere OSGi应用环境下服务调用saaj包加载问题分析报告 作者:bingjava 版权声明:本文为博主原创文章,转载请说明出处:http://www.cnblogs.com/bingj ...
- 【POJ】【2699】The Maximum Number of Strong Kings
网络流/最大流/二分or贪心 题目大意:有n个队伍,两两之间有一场比赛,胜者得分+1,负者得分+0,问最多有几只队伍打败了所有得分比他高的队伍? 可以想到如果存在这样的“strong king”那么一 ...
- jQuery - AJAX (keep for myself)
1. 简介:AJAX工作原理图 AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术.(如google地图) 目的:在不重载整个网页的情况下,AJAX 通 ...