日期:2009-08-17  浏览次数:20491 次

代码源于codeproject,修改增加了读取设备标识的功能。BBS的朋友需要,顺便放到这里来

using System;
using System.Runtime.InteropServices;

using System.Text;


namespace DevClasses
{
/// <summary>
/// Summary description for Class.
/// </summary>
class DeviceClasses
{
/// <summary>
/// The main entry point for the application.
/// </summary>
public const int MAX_NAME_PORTS=7;
public const int RegDisposition_OpenExisting=(0x00000001);
// open key only if exists
public const int CM_REGISTRY_HARDWARE=(0x00000000);

public const int CR_SUCCESS = (0x00000000);
public const int CR_NO_SUCH_VALUE = (0x00000025);
public const int CR_INVALID_DATA = (0x0000001F);
public const int DIGCF_PRESENT = (0x00000002);
public const int DIOCR_INSTALLER = (0x00000001);
// MaximumAllowed access type to Reg.
public const int MAXIMUM_ALLOWED = (0x02000000);
[StructLayout(LayoutKind.Sequential)]

public class SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public int DevInst; // DEVINST handle
public ulong Reserved;
};


[DllImport("cfgmgr32.dll")]
public static extern UInt32
CM_Open_DevNode_Key(IntPtr dnDevNode, UInt32 samDesired,
UInt32 ulHardwareProfile,
UInt32 Disposition,IntPtr phkDevice, UInt32 ulFlags);

[DllImport("cfgmgr32.dll")]
public static extern UInt32
CM_Enumerate_Classes(UInt32 ClassIndex,ref Guid ClassGuid, UInt32 Params);

[DllImport("setupapi.dll")]//
public static extern Boolean
SetupDiClassNameFromGuidA(ref Guid ClassGuid,
StringBuilder ClassName, //char * ?
UInt32 ClassNameSize, ref UInt32 RequiredSize);

[DllImport("setupapi.dll")]
public static extern IntPtr
SetupDiGetClassDevsA(ref Guid ClassGuid, UInt32 Enumerator,
IntPtr hwndParent, UInt32 Flags);

[DllImport("setupapi.dll")]
public static extern Boolean
SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, UInt32 MemberIndex,
ref SP_DEVINFO_DATA DeviceInfoData);

[DllImport("setupapi.dll")]
public static extern Boolean
SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);

[DllImport("setupapi.dll")]
public static extern IntPtr
SetupDiGetClassDevsA(ref Guid ClassGuid, UInt32 samDesired,
UInt32 Flags, ref string hwndParent, IntPtr Reserved);

[DllImport("setupapi.dll")]
public static extern IntPtr
SetupDiOpenClassRegKeyExA(
ref Guid ClassGuid, UInt32 samDesired, int Flags, IntPtr MachineName,
UInt32 Reserved);

[DllImport("advapi32.dll")]
public static extern UInt32
RegQueryValueA(IntPtr KeyClass,UInt32 SubKey,
StringBuilder ClassDescription,ref UInt32 sizeB);


[DllImport("user32.dll")]
public static extern Boolean
CharToOem(String lpszSrc, StringBuilder lpszDst);

public static int EnumerateClasses(UInt32 ClassIndex,
ref StringBuilder ClassName, StringBuilder ClassDescription, ref string id,
ref bool DevicePresent)
{
Guid ClassGuid=Guid.Empty;

IntPtr NewDeviceInfoSet;
SP_DEVINFO_DATA DeviceInfoData;
UInt32 result;
StringBuilder name=new StringBuilder("");
bool resNam=false;
UInt32 RequiredSize=0;

IntPtr ptr;

result = CM_Enumerate_Classes(ClassIndex, ref ClassGuid,0);


ClassName=new StringBuilder(""