原文: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. VC++编程中为程序加入启动画面功能

     如何为自己的程序加入启动画面 观察我们平常使用的软件,当我们双击软件的时候,会在主界面出现前,先行出现一个启动画面,由于前一阵子写了一个基于对话框的程序,亲自实验了下,今天就为大家简单的介绍下,在我 ...

  2. 微软职位内部推荐-Software Engineer II-SDP

    微软近期Open的职位: Position: SDE II The R&D of Shared Data Platform at Application and Services Group ...

  3. My97DatePicker控制开始时间和结束时间区间

    开始时间: <input type="text" placeholder=" -请选择- " id="kssj" name=" ...

  4. poj 1386 Play on Words 有向欧拉回路

    题目链接:http://poj.org/problem?id=1386 Some of the secret doors contain a very interesting word puzzle. ...

  5. 【POJ】【1160】Post Office

    DP/四边形不等式 邮局,经典的四边形不等式例题! 关于四边形不等式的学习请看 赵爽论文<动态规划加速原理之四边形不等式> 题目总结&题解:http://blog.csdn.net ...

  6. Leetcode#129 Sum Root to Leaf Numbers

    原题地址 二叉树的遍历 代码: vector<int> path; int sumNumbers(TreeNode *root) { if (!root) ; ; path.push_ba ...

  7. 二分图匹配 分类: ACM TYPE 2014-10-01 19:57 94人阅读 评论(0) 收藏

    #include<cstdio> #include<cstring> using namespace std; bool map[505][505]; int n, k; bo ...

  8. ios 判断空字符串

    - (BOOL) isBlankString:(NSString *)string { if (string == nil || string == NULL) { return YES; } if ...

  9. ASP.NET - 演练:创建网页以显示 XML 数据

    数据通常是以 XML 格式提供给 Web 应用程序的.但是,XML 数据本质上是分层的,因此您可能希望能够在基于列表的控件中使用 XML 数据,如 GridView 或 DropDownList 控件 ...

  10. Combination Sum III

    https://leetcode.com/problems/combination-sum-iii/ Find all possible combinations of k numbers that ...