. Net-diving into system programming-Part 1 [posting]

Source: Internet
Author: User

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 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;
Return 0 ;}

Intptr keyclass = setupdiopenclassregkeyexa (
Ref classguid, maximum_allowed, diocr_installer, intptr. Zero, 0 );
If (keyclass. toint32 () =-1)
{Devicepresent = false;
Classname = Name;
Return 0 ;}

Uint32 sizeb = 1000;
String ABCD = "";
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;
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;
While (true)
{
Int res = enumerateclasses (I, ref classes, classesdescr, ref devexist );
If (RES =-1) break;
++ I;
If (RES <-1 |! Devexist) continue;
Console. writeline ("classname = {0}, description = {1}", classes, classesdescr );
}
Return;
}
}
}

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.