Traverse devices in the Device Manager

Source: Internet
Author: User

The Code originated from codeproject and added the function of reading device IDs. BBS friends need it. Put it here by the way.

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;

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;
}
}
}
//////////////////////////////////////// ///////////// Running result on the sub-host ////////////////// //////////////////////////////////////// ////////

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 = Universal Serial Bus Controller, id = 36fc9e60-c465-11cf-8056-4445535
40000
Classname = pnpprinters, description = IEEE 1394 and SCSI printer, id = 4158ee7e-f050-11d
1-b6bd-00c04fa372a7
Classname = dot4, description = IEEE 1284.4 device, id = 41521b56-6795-11d2-b1a8-0080c72e
74a2
Classname = dot4print, description = IEEE 1284.4 compatible printer, id = 49ce6ac8-6f86-11d2-b1
E5-0080c72e74a2
Classname = CDROM, description = DVD/CD-ROM drive, id = 4d36e965-e325-11ce-bfc1-08002b
E10318
Classname = computer, description = computer, id = 4d36e966-e325-11ce-bfc1-08002be10318
Classname = diskdrive, description = disk drive, id = 4d36e967-e325-11ce-bfc1-08002be10
318
Classname = display, description = display card, id = 4d36e968-e325-11ce-bfc1-08002be10318
Classname = FDC, description = floppy disk controller, id = 4d36e969-e325-11ce-bfc1-08002be10318
Classname = HDC, description = ide ATA/atapi controller, id = 4d36e96a-e325-11ce-bfc1-08002
Be10318
Classname = keyboard, description = keyboard, id = 4d36e96b-e325-11ce-bfc1-08002be10318
Classname = media, description = sound, video, and game controller, id = 4d36e96c-e325-11ce-bfc1-0
8002be10318
Classname = modem, description = modem, id = 4d36e96d-e325-11ce-bfc1-08002be10318
Classname = monitor, description = monitor, id = 4d36e96e-e325-11ce-bfc1-08002be10318
Classname = mouse, description = mouse and other pointer devices, id = 4d36e96f-e325-11ce-bfc1-08002
Be10318
Classname = MTD, description = memory technology driver, id = 4d36e970-e325-11ce-bfc1-0
8002be10318
Classname = multifunction, description = multi-function card, id = 4d36e971-e325-11ce-bfc1-08002be
10318
Classname = net, description = network adapter, id = 4d36e972-e325-11ce-bfc1-08002be10318
Classname = netclient, description = network customer, id = 4d36e973-e325-11ce-bfc1-08002be1031
8
Classname = netservice, description = network service, id = 4d36e974-e325-11ce-bfc1-08002be103
18
Classname = nettrans, description = network protocol, id = 4d36e975-e325-11ce-bfc1-08002be10318

Classname = PCMCIA, description = PCMCIA card, id = 4d36e977-e325-11ce-bfc1-08002be10318
Classname = ports, description = port (COM and LPT), id = 4d36e978-e325-11ce-bfc1-08002b
E10318
Classname = printer, description = printer, id = 4d36e979-e325-11ce-bfc1-08002be10318
Classname = scsiadapter, description = SCSI and RAID Controller, id = 4d36e97b-e325-11ce-bfc
1-08002be10318
Classname = system, description = system device, id = 4d36e97d-e325-11ce-bfc1-08002be10318
Classname = unknown, description = other devices, id = 4d36e97e-e325-11ce-bfc1-08002be10318
Classname = floppydisk, description = floppy disk drive, id = 4d36e980-e325-11ce-bfc1-08002be1
0318
Classname = processor, description = processor, id = 50366dc3-0f36-415e-a6cc-4cb3be910b65
Classname = multiportserial, description = multi-serial port card, id = 50906cb8-ba12-11d1-bf5d-0000f
805f530
Classname = smartcardreader, description = Smart Card Reader, id = 50dd5230-ba8a-11d1-bf5d-0
000f805f530
Classname = volumesnapshot, description = Volume Shadow Copy, 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 bus master controller, id = 6bdd1fc1-810f-11d0-bec7-08
002be2092f
Classname = infrared, description = infrared device, id = 6bdd1fc5-810f-11d0-bec7-08002be209
2f
Classname = image, description = image processing device, id = 6bdd1fc6-810f-11d0-bec7-08002be2092
F
Classname = tapedrive, description = tape drive, id = 6d807884-7d21-11cf-801c-08002be10
318
Classname = volume, description = storage volume, id = 71a27cdd-812a-11d0-bec7-08002be2092f
Classname = battery, description = battery, id = 201731e54-78a4-11d0-bcf7-00aa00b7b32a
Classname = hidclass, description = ergonomic interface device, id = 745a17a0-74d3-11d0-b6fe-00a0c9
0f57da
Classname = 61883, description = 61883 device class, id = 7ebefbc0-3200-11d2-b4c2-00a0c9697d0
7
Classname = legacydriver, description = non-plug-and-play driver, id = 8ecc055d-047f-11d1-a53
7-0000f8753ed1
Classname = AVC, description = AVC equipment class, id = c06ff265-ae09-48f0-812c-16753d7cba83
Classname = enum1394, description = IEEE 1394 IP network counter, id = c459df55-db08-11d1-b0
09-00a0c9081ff6
Classname = mediumchanger, description = media converter, id = ce5939ae-ebde-11d0-b181-0000f
8753ec4
Classname = sbp2, description = sbp2 IEEE 1394 device, id = d48179be-ec20-11d1-b6b8-00c04
Fa372a7
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.