原文: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#枚举硬件设备(升级版)的更多相关文章

  1. usb由于其配置信息(注册表中的)不完整或已损坏,Windows 无法启动这个硬件设备

    在设备管理器的usb设备的属性中,显示提示“由于其配置信息(注册表中的)不完整或已损坏,Windows 无法启动这个硬件设备”.注册表坏了.经过查询,解决方法如下: 方法:打开注册表编辑器(开始--& ...

  2. 使用RSA非对称密钥算法实现硬件设备授权

    一.硬件设备授权 即用户在硬件设备输入一个序列号(或一个包含授权信息的文件),然后硬件设备便可正常使用.    二.授权方案 构思授权方案时,参考了下面网址的思路: http://bbs.csdn.n ...

  3. USB设备不能用。提示Windows 无法启动这个硬件设备。 (代码 19)

    USB,由于其配置信息(注册表中的)不完整或已损坏, Windows 无法启动这个硬件设备. (代码 19) 原因:提示Windows 无法启动这个硬件设备. (代码 19) 处理解决方法: 1) r ...

  4. MFC枚举USB设备碰到的一个疑难,还没解决

    代码如下: 打开USB Hub设备之后,返回句柄hHubDevice,然后使用EnumerateHubPorts来枚举Hub的端 口.疑问在代码的中文注释中. bool CUsbEnumHub::En ...

  5. Android系统修改硬件设备访问权限

    Android系统修改硬件设备访问权限 在硬件抽象层模块文件(so)文件中,提供的函数调用open函数来打开设备文件,比如/dev/gpio,如果不修改设备文件/dev/gpio的访问权限,那么应用程 ...

  6. Studio 5000编程:如何判断AB PLC系统中的硬件设备是否在正常工作

    前言:PLC控制系统,主要由CPU.本机架I/O模块,分布式I/O模块,通信模块,或其他设备(如:伺服驱动器.交换机.第三方设备)等组成,如何判断这些设备是否工作正常?或是一旦出现故障,能在第一时间判 ...

  7. linux 系统下有sda和hda的硬件设备分别代表什么意思

    linux 系统下有sda和hda的硬件设备分别代表什么意思/dev/sda1 # SCSI设备,sda,sdb,sdc,三块盘,1,2,3代表分区(PV)/dev/sda2/dev/sdb1/dev ...

  8. linux 挂载硬件设备

    mount命令用于挂载文件系统,格式为:“mount 文件系统 挂载目录”. 挂载是在使用硬件设备前的最后操作的一步,只需要用mount命令把硬件设备与一个目录做关联,然后就能在这个目录中看到硬件设备 ...

  9. lspci通过系统总线查看硬件设备信息

    lspci - 列出所有PCI设备 PCI 的科普: PCI(Peripheral Component Interconnect),是一种连接电子计算机主板和外部设备的总线标准. 常见的PCI卡包括网 ...

随机推荐

  1. mysql安装/启动报错汇总

    2016/9/6补充 初始化报错: # /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysq ...

  2. 四则运算2+psp0级表格

    四则运算2 一.题目和要求 题目:写一个能自动生成小学四则运算题目的程序,要求一次输出不少于30道,只能是整数100以内的四则运算(四则运算1升级版) 要求: 1.题目避免重复 2.可定制(数量/打印 ...

  3. Careercup - Google面试题 - 5727310284062720

    2014-05-06 14:04 题目链接 原题: given an 2D matrix M, is filled either using X or O, you need to find the ...

  4. [转载]115个Java面试题和答案

    不知道大家有没有这样的体会,就是找工作的时候不得不准备大量面试题,而工作的时间长了面试题里的精髓却忘的差不多了... 转载几篇Java面试的bolg,温故而知新,最重要的是常来看看. 1. http: ...

  5. 二分图匹配(KM算法)n^3 分类: ACM TYPE 2014-10-01 21:46 98人阅读 评论(0) 收藏

    #include <iostream> #include<cstring> #include<cstdio> #include<cmath> const ...

  6. IIS网站打不开错误解决方案集锦(一):编译器错误消息: 编译器失败,错误代码为 -1073741502。

    [2015-05-12解决记录] 问题:服务器上的文件一直都是好的,但是运行了很长一段时间以后,发现网站打不开,或者上传不了图片了,怎么办? 错误信息:c:\windows\system32\inet ...

  7. .Net 执行 Oracle SQL语句时, 中文变问号

      带中文的Sql语句在.Net调用时, 中文变问号(可使用 SQL Tracker工具跟踪)   问题:       服务器的字符集与客户端的字符集不一致. 解决方法: 1.  查看服务端的字符集: ...

  8. KMP_Best Reward

    大意:把一个字符串分成两串,假如一个字符串是回文串就可以加上它的VALUE,否则它的VALUE为0: KMP的特点是可以求出前缀与后面的字符串是否匹配, 注意回文串的特点,所以当我们把回文串反转的时候 ...

  9. 偏序集的Dilworth定理

    定理1 令(X,≤)是一个有限偏序集,并令r是其最大链的大小.则X可以被划分成r个但不能再少的反链.其对偶定理称为Dilworth定理:定理2 令(X,≤)是一个有限偏序集,并令m是反链的最大的大小. ...

  10. 图解JavaScript执行环境结构

    JavaScript引擎在开始编译代码的时候,会对JavaScript代码进行一次预编译,生成一个执行环境,比如如下代码: window.onload=function(){ function sub ...