庆祝老婆生日,发布《C#枚举硬件设备(升级版)》源码

甴曱 2009-12-03 10:07:16
加精
先取设备类型:

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

http://download.csdn.net/source/1871029
...全文
5050 508 打赏 收藏 转发到动态 举报
写回复
用AI写文章
508 条回复
切换为时间正序
请发表友善的回复…
发表回复
JohnsonShu 2012-03-29
  • 打赏
  • 举报
回复
64bitwindows,vs2010里出现空指针异常, 改完之后,左边也出不来树状结构了
guojinshangb 2012-03-07
  • 打赏
  • 举报
回复
请问楼主,如何判断机器中是否带有蓝牙设备.如何去获得这个蓝牙设备的"兼容ID","硬件ID"之类的..
hzy111 2011-06-03
  • 打赏
  • 举报
回复
标记一下
zq710860677 2011-05-25
  • 打赏
  • 举报
回复
预祝今年生日快乐!
wb0504 2011-05-20
  • 打赏
  • 举报
回复
收藏……学习……
wuwaiwai 2011-05-19
  • 打赏
  • 举报
回复
太牛逼了
a5680737 2011-05-14
  • 打赏
  • 举报
回复
LZ强大!!!
hnelfh 2010-10-06
  • 打赏
  • 举报
回复
不错,转用。
ls3aslp 2010-08-27
  • 打赏
  • 举报
回复
生日快了,顺便接分。。。。。
wbd6572789 2010-08-26
  • 打赏
  • 举报
回复
保留下来
createTim 2010-07-21
  • 打赏
  • 举报
回复
mark
bluesky4812 2010-07-20
  • 打赏
  • 举报
回复
谢谢楼主。。。。。。
audryhepburn 2010-05-30
  • 打赏
  • 举报
回复
顶!
mark
phinex93 2010-05-26
  • 打赏
  • 举报
回复
lz很强大!
jeamizhang2000 2010-05-22
  • 打赏
  • 举报
回复
非常好,楼主强大
jxndwl 2010-03-24
  • 打赏
  • 举报
回复
真是巨牛啊 正需要呢
收藏了
liumeibiao 2010-03-18
  • 打赏
  • 举报
回复
祝福!!!
  • 打赏
  • 举报
回复
不能不承认这是一篇好帖子
up是必须的
拿分是顺便的
crazyxingjc 2010-03-15
  • 打赏
  • 举报
回复





路过!学习下!···
fly4free 2010-03-11
  • 打赏
  • 举报
回复
可能有用, 标记一下~~
加载更多回复(470)

110,530

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧