.NET - DIVING INTO SYSTEM Programming - Part 1 [Repost]

zhaozj2021-02-16  55

Using system; using system.Text; namespace devclasses {///////

/// summary description for class.///

Class DeviceClasses

{

?

///? /// The main entry point for the application.? ///

?

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);

// Maximum orowed 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 DeviceInfoseet, 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 NewDeviceInfoseet;

?? sp_devinfo_data deficEinfodata;

?? 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,

????????

RequiredSize;

??

IF (RequiredSize>

0)

??? {

??? name.capacity =

Int) RequiredSize;

??? RESNAM = setupdiclassnamefromguida (

Ref classguid, name,

?????????? RequiredSize,

RequiredSize;

???}

?? NewDeviceInfoset = setupdigetClassdevsa (

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

Ref classguid,

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

0,

??? INTPTR.ZERO,

??? Digcf_present;

??

IF (newDeviceInfoset.toint32 () == -

1)

??? {? DevicePresent =

False;

????? classname = name;

?????

Return

}

?? INTPTR KeyClass = setupdiopenclassregkeyexa (

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

Ref classguid, maximum_allowed, dioCr_installer, INTPTR.ZERO,

0);

??

IF (KeyClass.Toint32 () == -

1)

??? {? DevicePresent =

False;

????? classname = name;

?????

Return

}

?? 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 classs =

New StringBuilder

"" "

?? StringBuilder classsDescriber =

New StringBuilder

"" "

?? Stringbuilder ClassesDescroem = New StringBuilder

"" "

?? classesdescribem.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;

?

}

}

转载请注明原文地址:https://www.9cbs.com/read-21525.html

New Post(0)