分享
 
 
 

遍历设备管理器的设备

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

代码源于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=声音、视频和游戏控制器,id=4d36e96c-e325-11ce-bfc1-0

8002be10318

ClassName=Modem, Description=调制解调器,id=4d36e96d-e325-11ce-bfc1-08002be10318

ClassName=Monitor, Description=监视器,id=4d36e96e-e325-11ce-bfc1-08002be10318

ClassName=Mouse, Description=鼠标和其它指针设备,id=4d36e96f-e325-11ce-bfc1-08002

be10318

ClassName=MTD, Description=Memory technology driver,id=4d36e970-e325-11ce-bfc1-0

8002be10318

ClassName=MultiFunction, Description=多功能卡,id=4d36e971-e325-11ce-bfc1-08002be

10318

ClassName=Net, Description=网络适配器,id=4d36e972-e325-11ce-bfc1-08002be10318

ClassName=NetClient, Description=网络客户,id=4d36e973-e325-11ce-bfc1-08002be1031

8

ClassName=NetService, Description=网络服务,id=4d36e974-e325-11ce-bfc1-08002be103

18

ClassName=NetTrans, Description=网络协议,id=4d36e975-e325-11ce-bfc1-08002be10318

ClassName=PCMCIA, Description=PCMCIA 卡,id=4d36e977-e325-11ce-bfc1-08002be10318

ClassName=Ports, Description=端口 (COM 和 LPT),id=4d36e978-e325-11ce-bfc1-08002b

e10318

ClassName=Printer, Description=打印机,id=4d36e979-e325-11ce-bfc1-08002be10318

ClassName=SCSIAdapter, Description=SCSI 和 RAID 控制器,id=4d36e97b-e325-11ce-bfc

1-08002be10318

ClassName=System, Description=系统设备,id=4d36e97d-e325-11ce-bfc1-08002be10318

ClassName=Unknown, Description=其它设备,id=4d36e97e-e325-11ce-bfc1-08002be10318

ClassName=FloppyDisk, Description=软盘驱动器,id=4d36e980-e325-11ce-bfc1-08002be1

0318

ClassName=Processor, Description=处理器,id=50127dc3-0f36-415e-a6cc-4cb3be910b65

ClassName=MultiPortSerial, Description=多串口卡,id=50906cb8-ba12-11d1-bf5d-0000f

805f530

ClassName=SmartCardReader, Description=智能卡阅读器,id=50dd5230-ba8a-11d1-bf5d-0

000f805f530

ClassName=VolumeSnapshot, Description=存储卷影副本,id=533c5b84-ec70-11d2-9505-00

c04f79deaf

ClassName=Biometric, Description=Personal identification devices,id=53d29ef7-377

c-4d14-864b-eb3a85769359

ClassName=1394, Description=IEEE 1394 总线主控制器,id=6bdd1fc1-810f-11d0-bec7-08

002be2092f

ClassName=Infrared, Description=红外线设备,id=6bdd1fc5-810f-11d0-bec7-08002be209

2f

ClassName=Image, Description=图像处理设备,id=6bdd1fc6-810f-11d0-bec7-08002be2092

f

ClassName=TapeDrive, Description=磁带驱动器,id=6d807884-7d21-11cf-801c-08002be10

318

ClassName=Volume, Description=存储卷,id=71a27cdd-812a-11d0-bec7-08002be2092f

ClassName=Battery, Description=电池,id=72631e54-78a4-11d0-bcf7-00aa00b7b32a

ClassName=HIDClass, Description=人体学接口设备,id=745a17a0-74d3-11d0-b6fe-00a0c9

0f57da

ClassName=61883, Description=61883 设备类,id=7ebefbc0-3200-11d2-b4c2-00a0c9697d0

7

ClassName=LegacyDriver, Description=非即插即用驱动程序,id=8ecc055d-047f-11d1-a53

7-0000f8753ed1

ClassName=Avc, Description=AVC 设备类,id=c06ff265-ae09-48f0-812c-16753d7cba83

ClassName=Enum1394, Description=IEEE 1394 IP 网络计数器,id=c459df55-db08-11d1-b0

09-00a0c9081ff6

ClassName=MediumChanger, Description=介质变换器,id=ce5939ae-ebde-11d0-b181-0000f

8753ec4

ClassName=SBP2, Description=SBP2 IEEE 1394 设备,id=d48179be-ec20-11d1-b6b8-00c04

fa372a7

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有