当前位置:首页>>ASP.NET教程>>Asp.NET综合技巧>>遍历设备管理器的设备
遍历设备管理器的设备
2009/11/8 13:19:18
代码源于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("");
DevicePresent=false;
//incorrect device class:
if(result == CR_INVALID_DATA)
{
return -2;
}
//device class is absent
if(result == CR_NO_SUCH_VALUE)
{
return -1;
}
//bad param. - fatal error
if(result != CR_SUCCESS)
{
return -3;
}


name.Capacity=0;
resNam=SetupDiClassNameFromGuidA(ref ClassGuid,name,RequiredSize,ref RequiredSize);
if(RequiredSize > 0)
{
name.Capacity=(int)RequiredSize;
resNam=SetupDiClassNameFromGuidA(ref ClassGuid,name,RequiredSize,ref RequiredSize);

}

NewDeviceInfoSet=SetupDiGetClassDevsA(ref ClassGuid,0,IntPtr.Zero,DIGCF_PRESENT);

if(NewDeviceInfoSet.ToInt32() == -1)
{
DevicePresent=false;
ClassName=name;
id=ClassGuid.ToString();
return 0;}

IntPtr KeyClass=SetupDiOpenClassRegKeyExA(
ref ClassGuid, MAXIMUM_ALLOWED, DIOCR_INSTALLER,IntPtr.Zero,0);
if(KeyClass.ToInt32() == -1)
{
DevicePresent=false;
ClassName=name;
id=ClassGuid.ToString();
return 0;}


UInt32 sizeB=1000;

StringBuilder CD=new StringBuilder("");
ClassDescription.Capacity=1000;

UInt32 res=RegQueryValueA(KeyClass,0,ClassDescription,ref sizeB);


if(res != 0)ClassDescription=new StringBuilder("");
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
ClassName=name;
id=ClassGuid.ToString();
DevicePresent=true;

return 0;

}

[STAThread]
static void Main(string[] args)
{
StringBuilder classes=new StringBuilder("");
StringBuilder classesDescr=new StringBuilder("");

StringBuilder classesDescrOEM=new StringBuilder("");
classesDescrOEM.Capacity=1000;
Boolean DevExist=false;
UInt32 i=0;
string id="";
while(true)
{

int res=EnumerateClasses(i,ref classes,classesDescr,ref id ,ref DevExist);
if(res == -1)break;
++i;
if(res < -1 || !DevExist)continue;
Console.WriteLine("ClassName={0}, Description={1},id={2}",classes,classesDescr,id);
}
return;
}
}
}
////////////////////////////////////////////////////////在我机子上运行结果//////////////////////////////////////////////////////////////////

ClassName=WCEUSBS, Description=Windows CE USB Devices,id=25dbce51-6c8f-4a72-8a6d
-b54c2b4fc835
ClassName=Security Accelerator, Description=Security Accelerators,id=268c95a1-ed
fe-11d3-95c3-0010dc4050a5
ClassName=USB, Description=通用串行总线控制器,id=36fc9e60-c465-11cf-8056-4445535
40000
ClassName=PnpPrinters, Description=IEEE 1394 和 SCSI 打印机,id=4658ee7e-f050-11d
1-b6bd-00c04fa372a7
ClassName=Dot4, Description=IEEE 1284.4 设备,id=48721b56-6795-11d2-b1a8-0080c72e
74a2
ClassName=Dot4Print, Description=IEEE 1284.4 兼容打印机,id=49ce6ac8-6f86-11d2-b1
e5-0080c72e74a2
ClassName=CDROM, Description=DVD/CD-ROM 驱动器,id=4d36e965-e325-11ce-bfc1-08002b
e10318
ClassName=Computer, Description=计算机,id=4d36e966-e325-11ce-bfc1-08002be10318
ClassName=DiskDrive, Description=磁盘驱动器,id=4d36e967-e325-11ce-bfc1-08002be10
318
ClassName=Display, Description=显示卡,id=4d36e968-e325-11ce-bfc1-08002be10318
ClassName=fdc, Description=软盘控制器,id=4d36e969-e325-11ce-bfc1-08002be10318
ClassName=hdc, Description=IDE ATA/ATAPI 控制器,id=4d36e96a-e325-11ce-bfc1-08002
be10318
ClassName=Keyboard, Description=键盘,id=4d36e96b-e325-11ce-bfc1-08002be10318
ClassName=MEDIA, Description=声音、视频和游戏控制器,i