您的位置:首页 > 编程语言 > C#

C#枚举硬件设备

2009-12-07 15:32 218 查看
 
/// <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;
}
}


/// <summary>
/// 设备资源(查询MSDN)
/// </summary>
public class DeviceResources
{
private const int DIGCF_PRESENT = (0x00000002);
private const int MAX_DEV_LEN = 1000;
private const int SPDRP_FRIENDLYNAME = (0x0000000C);  // FriendlyName (R/W)
private const int SPDRP_DEVICEDESC = (0x00000000);    // DeviceDesc (R/W)

public const int sizIO_HEADER = 28;
public const int sizDMA_HEADER = 16;
public const int sizIRQ_HEADER = 20;
public const int sizMEM_HEADER = 32;

public const int ResType_Mem = 0x00000001; //Physical address resource
public const int ResType_IO = 0x00000002; //Physical I/O address resource
public const int ResType_DMA = 0x00000003; //DMA channels resource
public const int ResType_IRQ = 0x00000004; //IRQ resource

public const int CR_SUCCESS = 0;
public const int CR_NO_MORE_LOG_CONF = 0x0000000E;
public const int CR_BUFFER_SMALL = 0x0000001A;

public const int CR_NO_SUCH_VALUE = (0x00000025);
public const int CR_INVALID_DATA = (0x0000001F);

//--------------------------------------------------------------
// 内存资源
//--------------------------------------------------------------
public const int fMD_MemoryType = (0x1); //位掩码,无论是否是可写的存储器
public const int fMD_ROM = (0x0); // 内存范围为只读
public const int fMD_RAM = (0x1); // 内存范围可能会被写入

public const int fMD_32_24 = (0x2); // 掩码,内存是24位或32位
public const int fMD_24 = (0x0); // 内存范围是24位
public const int fMD_32 = (0x2); // 内存范围是32位

public const int fMD_Prefetchable = (0x4); // 掩码,内存是否能预取
public const int fMD_PrefetchDisallowed = (0x0); // 不支持
public const int fMD_PrefetchAllowed = (0x4); // 支持

public const int fMD_Readable = (0x8); // 掩码,是否可读内存
public const int fMD_ReadAllowed = (0x0); // 可以
public const int fMD_ReadDisallowed = (0x8); // 只写

public const int fMD_CombinedWrite = (0x10); // 掩码,支持后写
public const int fMD_CombinedWriteDisallowed = (0x0);  //   不支持 combined-write caching
public const int fMD_CombinedWriteAllowed = (0x10); // 支持 combined-write caching

//
// 内存范围结构
//
[StructLayout(LayoutKind.Sequential)]
public class MEM_RANGE
{
public UInt64 MR_Align;     // 基址对齐
public UInt32 MR_nBytes;    // 指定所需的字节数
public UInt64 MR_Min;       // 指定范围内的最低地址
public UInt64 MR_Max;       // 指定范围内的最高地址
public UInt32 MR_Flags;     // 指定标志描述范围
public UInt32 MR_Reserved;
};

//
// 内存描述结构
//
[StructLayout(LayoutKind.Sequential)]
public class MEM_DES
{
public UInt32 MD_Count;         // 数量
public UInt32 MD_Type;          // 大小
public UInt64 MD_Alloc_Base;    // 内存地址分配基址范围
public UInt64 MD_Alloc_End;     // 内存地址分配终址范围
public UInt32 MD_Flags;         // 指定标志描述范围
public UInt32 MD_Reserved;
};

//--------------------------------------------------------------
// I/O端口资源
//--------------------------------------------------------------

public const int fIOD_PortType = (0x1); // 掩码,无论端口是IO或内存
public const int fIOD_Memory = (0x0); // 内存使用的端口
public const int fIOD_IO = (0x1); // IO使用的端口

//
// I/O端口结构
//
[StructLayout(LayoutKind.Sequential)]
public class IO_RANGE
{
public UInt64 IOR_Align;
public UInt32 IOR_nPorts;
public UInt64 IOR_Min;
public UInt64 IOR_Max;
public UInt32 IOR_RangeFlags;
public UInt64 IOR_Alias;
};

[StructLayout(LayoutKind.Sequential)]
public class IO_DES
{
public UInt32 IOD_Count;          // 数量
public UInt32 IOD_Type;           // 大小
public UInt64 IOD_Alloc_Base;     // 端口分配基址
public UInt64 IOD_Alloc_End;      // 端口分配终址
public UInt32 IOD_DesFlags;       //
};

public const int IOA_Local = 0xff;

//--------------------------------------------------------------
// DMA 资源
//--------------------------------------------------------------
public const int mDD_Width = (0x3);    // 位掩码
public const int fDD_BYTE = (0x0);    //   8-bit
public const int fDD_WORD = (0x1);    //   16-bit
public const int fDD_DWORD = (0x2);    //   32-bit

[StructLayout(LayoutKind.Sequential)]
public class DMA_RANGE
{
public UInt32 DR_Min;
public UInt32 DR_Max;
public UInt32 DR_Flags;
};

[StructLayout(LayoutKind.Sequential)]
public class DMA_DES
{
public UInt32 DD_Count;       // 数量
public UInt32 DD_Type;        // 大小
public UInt32 DD_Flags;       // 标志位
public UInt32 DD_Alloc_Chan;  // 指定的DMA已经被分配
};

//--------------------------------------------------------------
// 中断资源
//--------------------------------------------------------------

//
//定义为一个中断资源的范围属性标志。每个位标志识别恒定的位掩码。继位掩码的定义是可能的值。
//
public const int mIRQD_Share = (0x1); // 位掩码,是否可以共享的IRQ
public const int fIRQD_Exclusive = (0x0); //   可能不能共享的IRQ
public const int fIRQD_Share = (0x1); //   能共享的IRQ

public const int mIRQD_Edge_Level = (0x2);
public const int fIRQD_Level = (0x0);
public const int fIRQD_Edge = (0x2);

//
// 中断范围
//
[StructLayout(LayoutKind.Sequential)]
public class IRQ_RANGE
{
public UInt32 IRQR_Min;      // 最小中断
public UInt32 IRQR_Max;      // 最大
public UInt32 IRQR_Flags;
};

//
// 中断描述
//
[StructLayout(LayoutKind.Sequential)]
public class IRQ_DES
{
public UInt32 IRQD_Count;       // 数量
public UInt32 IRQD_Type;        // 类型
public UInt32 IRQD_Flags;       // 标志位
public UInt32 IRQD_Alloc_Num;   // 指定了被分配的IRQ
public UInt32 IRQD_Affinity;
};

[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("cfgmgr32.dll")]
private static extern UInt32 CM_Get_First_Log_Conf(ref UInt32 Conf, UInt32 DevInst, UInt32 Flag);

[DllImport("cfgmgr32.dll")]
private static extern UInt32 CM_Get_Next_Res_Des(ref UInt32 ResHandle, UInt32 CurrConf, UInt32 TypResource, UInt32 ptr, UInt32 Flag);

[DllImport("cfgmgr32.dll")]
private static extern UInt32 CM_Free_Res_Des_Handle(UInt32 handle);

[DllImport("cfgmgr32.dll")]
private static extern UInt32 CM_Get_Res_Des_Data_Size(ref UInt32 handle, UInt32 ResHandle, UInt32 Flag);

[DllImport("cfgmgr32.dll")]
private static extern UInt32 CM_Get_Res_Des_Data(UInt32 ResHandle, IntPtr buf, UInt32 size, UInt32 Flag);

static bool FResourceOpen = false;
public static IntPtr FBufForRes;
static UInt32 FResourceType;
static UInt32 CurrConf;
static IntPtr FResDeviceInfoSet;  //设备信息
static UInt32 ResHandle;
static UInt32 PrevResHandle;
static bool FNoResourses;
static UInt32 sizeResFu;


public DeviceResources()
{
}

//打开资源
public static bool OpenResources(UInt32 TypR, string ClassName, int DeviceIndex)
{

if (FResourceOpen)
return false;

FBufForRes = IntPtr.Zero;

//测试资源类型
if ((TypR != ResType_Mem) && (TypR != ResType_IO) &&
(TypR != ResType_DMA) && (TypR != ResType_IRQ))
{
//类型不正确则返回
FResourceType = TypR;
FResourceOpen = true;
CurrConf = 0;
FResDeviceInfoSet = IntPtr.Zero;
ResHandle = 0;
PrevResHandle = 0;
FNoResourses = true;
return false;
}

Guid[] guids = new Guid[1];
UInt32 RequiredSize = 0;
SP_DEVINFO_DATA FDeviceInfoData = new SP_DEVINFO_DATA();
//通过类型名称返回设备信息
bool res = SetupDiClassGuidsFromNameA(ClassName, ref guids[0], RequiredSize, ref RequiredSize);
if (RequiredSize == 0)
{
//类型不正确
return false;
}

//通过类型名称返回设备信息
if (!res)
{
guids = new Guid[RequiredSize];
res = SetupDiClassGuidsFromNameA(ClassName, ref guids[0], RequiredSize, ref RequiredSize);

if (!res || RequiredSize == 0)
{
//类型不正确
return false;
}
}

//通过类型名称返回设备信息
FResDeviceInfoSet = SetupDiGetClassDevsA(ref guids[0], 0, IntPtr.Zero, DIGCF_PRESENT);
if (FResDeviceInfoSet.ToInt32() == -1)
{
//设备不可用
FResourceType = TypR;
FResourceOpen = true;
CurrConf = 0;
FResDeviceInfoSet = IntPtr.Zero;
ResHandle = 0;
PrevResHandle = 0;
FNoResourses = true;
guids = null;
return false;
}

FDeviceInfoData.cbSize = 28;
//正常
FDeviceInfoData.DevInst = 0;
FDeviceInfoData.ClassGuid = System.Guid.Empty;
FDeviceInfoData.Reserved = 0;

res = SetupDiEnumDeviceInfo(FResDeviceInfoSet,
(UInt32)DeviceIndex, FDeviceInfoData);
if (!res)
{
//没有该类型的设备
SetupDiDestroyDeviceInfoList(FResDeviceInfoSet);
FResourceType = TypR;
FResourceOpen = true;
CurrConf = 0;
FResDeviceInfoSet = IntPtr.Zero;
ResHandle = 0;
PrevResHandle = 0;
FNoResourses = true;
guids = null;
return false;
}

CurrConf = 0;
ResHandle = 0;

//打开配置
UInt32 cmRes = CM_Get_First_Log_Conf(ref CurrConf, (UInt32)FDeviceInfoData.DevInst, 0x00000002);
if (cmRes == CR_NO_MORE_LOG_CONF)
{
cmRes = CM_Get_First_Log_Conf(ref CurrConf, (UInt32)FDeviceInfoData.DevInst, 0x00000003);
}
if (cmRes != CR_SUCCESS)
{
//错误
SetupDiDestroyDeviceInfoList(FResDeviceInfoSet);
FResourceType = TypR;
FResourceOpen = true;
CurrConf = 0;
FResDeviceInfoSet = IntPtr.Zero;
ResHandle = 0;
PrevResHandle = 0;
FNoResourses = true;
guids = null;
return false;
}

//获取设备资源描述
cmRes = CM_Get_Next_Res_Des(ref ResHandle, CurrConf, TypR, 0, 0);
if (cmRes == CR_SUCCESS)
{
//OK
FResourceType = TypR;
FResourceOpen = true;
PrevResHandle = 0;
FNoResourses = false;
guids = null;
return true;
}
else
{
//error
SetupDiDestroyDeviceInfoList(FResDeviceInfoSet);
FResourceType = TypR;
FResourceOpen = true;
CurrConf = 0;
FResDeviceInfoSet = IntPtr.Zero;
ResHandle = 0;
PrevResHandle = 0;
FNoResourses = true;
guids = null;
return false;
}
}

//关闭资源
public static bool CloseResources()
{

//
if (FBufForRes != IntPtr.Zero) Marshal.FreeHGlobal(FBufForRes);
if (!FResourceOpen)
return false;
FResourceOpen = false;
FResourceType = 0;

if (ResHandle != 0)
{
CM_Free_Res_Des_Handle(ResHandle);
ResHandle = 0;
}
return true;
}

//提取资源
public static int ExtractResourceInfo(ref bool NextResExist)
{
UInt32 sizeRes = 0;

FBufForRes = IntPtr.Zero;

if (FNoResourses)
{
NextResExist = false;
return -1;
}
if (ResHandle == 0)
{
NextResExist = false;
return -2;
}

//获取资源的大小
UInt32 cmRes = CM_Get_Res_Des_Data_Size(ref sizeRes, ResHandle, 0);
if (cmRes != CR_SUCCESS)
{
NextResExist = false;
return -3;
}

sizeResFu = sizeRes;

//分配资源数据到缓冲区
FBufForRes = Marshal.AllocHGlobal(1000);   //必须要有足够的缓冲区大小
//获取资源数据
cmRes = CM_Get_Res_Des_Data(ResHandle, FBufForRes, sizeResFu, 0);
if (cmRes == CR_BUFFER_SMALL)
return -4;
if (cmRes != CR_SUCCESS)
{
NextResExist = false;
return -5;
}

PrevResHandle = 0;
//获得下一个资源描述(如果存在)
cmRes = CM_Get_Next_Res_Des(ref PrevResHandle, ResHandle, FResourceType, 0, 0);
if (cmRes == CR_SUCCESS)
{
NextResExist = true;
CM_Free_Res_Des_Handle(ResHandle);
ResHandle = PrevResHandle;
PrevResHandle = 0;
}
else
{
NextResExist = false;
CM_Free_Res_Des_Handle(ResHandle);
ResHandle = 0;
PrevResHandle = 0;
}

return (int)sizeResFu;    //返回资源数据的大小
}
}


#region 加载设备类型
al = new ArrayList();
bool DevExist = false;
UInt32 i = 0;
treeView1.Nodes.Clear();
treeView1.Nodes.Add(SystemInformation.ComputerName);
while (true)
{
StringBuilder classes = new StringBuilder("");
StringBuilder classDescriptions = new StringBuilder("");
int res = DeviceClasses.EnumerateClasses(i, classes, classDescriptions, ref DevExist);
if (res == DeviceClasses.CR_NO_SUCH_VALUE) break;
++i;
if ((res != DeviceClasses.CR_NO_SUCH_VALUE && res != DeviceClasses.CR_SUCCESS) || !DevExist) continue;
#region 获取图标
IntPtr hIcon;
int ix;
Guid guid = DeviceClasses.ClassesGuid;
if (DeviceClasses.SetupDiLoadClassIcon(ref guid, out hIcon, out ix) != 0)
{
Icon devIcon = Icon.FromHandle(hIcon);
imgListTreeSmall.Images.Add(devIcon);
imgListTreeBig.Images.Add(devIcon);
}
int iImageIndex = imgListTreeSmall.Images.Count - 1;
#endregion
TreeNode tn = new TreeNode(classDescriptions.ToString(), iImageIndex, iImageIndex);
treeView1.Nodes[0].Nodes.Add(tn);
#region 加载设备
StringBuilder devices = new StringBuilder("");
int result = 0;
UInt32 Index = 0;
while (true)
{
result = DeviceInfo.EnumerateDevices(Index, classes.ToString(), devices);
Index++;
if (result == -2)
{
break;
}
if (result == -1)
break;
if (result == 0)
{
TreeNode tnClient = new TreeNode(devices.ToString(), iImageIndex, iImageIndex);
tn.Nodes.Add(tnClient);
}
}
#endregion
}
#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息