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

zhaozj2021-02-16  51

Using system;

Using system.text;

Using system.Runtime.InteropServices;

Namespace Devinfo

{

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

Class DeviceInfo

??? {

???????

public

Const

INT Digcf_present ??? =

0x00000002);

???????

public

Const

INT MAX_DEV_LEN =

1000;

???????

public

Const

INT SPDRP_FRIENDLYNAME =

0x0000000c);?

??????????

// FriendlyName (R / W)

???????

public

Const

INT SPDRP_DEVICEDESC =

0x00000000);???

??????????

// devicesDesc (r / w)

??????? [structLayout (layoutkind.sequential)]

???????????

public

Class SP_DEVINFO_DATA

??????????????? {

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

public

Int cbsize;

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

PUBLIC GUID? ClassGuid;

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

public

Int devinst; ???

// devinst handle

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

public

Ulong reserved;

???????????????};

??????? [DLLIMPORT

"setupapi.dll"]]]

//

???????

public

Static

Extern Boolean

????????? setupdiclassguidsfromnamea (

String Classn,

REF GUID GUIDS,

??????????? uint32 ClassNameSize,

Ref uint32 reqsize);

??????? [DLLIMPORT

"setupapi.dll"]]]

???????

public

Static

EXTERN INTPTR ???????????????

// Result HDevinfo

????????? setupdigetClassdevsa (

Ref guid classguid, uint32 enumerator,

??????????? INTPTR ???? hwndparent ,? uint32 flags);

??????? [DLLIMPORT

"setupapi.dll"]]]

???????

public

Static

Extern Boolean

????????? setUpdienumDeviceInfo (INTPTR DeviceInfoseet, uint32 memberindex,

??????????? SP_DEVINFO_DATA ???? deviceInfodata);

??????? [DLLIMPORT

"setupapi.dll"]]]

???????

public

Static

Extern Boolean

????????? setupdideStroyDeviceInfolist (INTPTR DeviceInfoseet);

??????? [DLLIMPORT

"setupapi.dll"]]]

???????

public

Static

Extern Boolean

????????? setupdiGetDeviceRegistryPropertya (INTPTR DEVICEINFOSET, ????????? sp_devinfo_data ???? DeviceInfodata, uint32 Property,

????????? uint32 ?? PropertyRegDataType, STRINGBUILDER? PropertyBuffer,

????????? uint32 propertybuffersize, intptr requiredsize;

???????

public

Static

Int EnumerateDevices (uint32 deviceindex,

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

String ClassName,

??????????????? StringBuilder DeviceName)

??????? {

???????? uint32 requiredsize =

0;

???????? guid guid = guid.empty

???????? guid [] guids =

New guid [

1];

???????? INTPTR NEWDEVICEINFOSet

???????? sp_devinfo_data deficEinfodata =

NEW sp_devinfo_data ();

????????

Bool res = setupdiclassguidsfromnamea (classname,

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

Ref guids [

0], RequiredSize,

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

RequiredSize;

????????

IF (RequiredSize ==

0)

?????????????? {

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

// IncorRect Class Name:

??????????????? deviceName =

New StringBuilder

"" "

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

Return -

2;

??????????????}

????????

IF (! rs)

????????? {

?????????? Guids =

New Guid [RequiredSize];

?????????? res = setupdiclassguidsfromnamea (classname,

Ref guids [

0], RequiredSize,

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

RequiredSize;

??????????

IF (! res || RequiredSize ==

0)

?????????????? {

??????????

// IncorRect Class Name:

??????????????? deviceName =

New StringBuilder

"" "

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

Return -

2;

??????????????}

?????????}

????????

// Get Device Info Set for Our Device Class

???????? NewdeviceInfoseet = setupdigetClassdevsa (

Ref guids [

0],

0, INTPTR.ZERO,

???????????????????? Digcf_Present);

????????

IF (newDeviceInfoset.toint32 () == -

1 )

????????

IF (! rs)

?????????????? {

??????????

// Device Information is unavailable: ??????????????? deviceName =

New StringBuilder

"" "

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

Return -

3;

??????????????}

??????????? deviceInfodata.cbsize =

28;

???????????

// is Devices EXIST for Class

??????????? deviceInfodata.devinst =

0;

??????????? deviceInfodata.classguid = system.guid.empty;

??????????? deviceInfodata.reserved =

0;

??????????? res = setupdienumDeviceInfo (NewDeviceInfoseet,

?????????????????? deviceIndex, deviceinfodata);

???????????

IF (! res) {

????????

// no Such device:

??????????????? setUpdideStroyDeviceInfolist (NewDeviceInfoseet);

??????????????? deviceName =

New StringBuilder

"" "

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

Return -

1;

???????????}

??????? deviceName.capacity = max_dev_len;

???????

IF (! setupdiGetDeviceRegistryPropertya (NewDeviceInfoseet,

????????? deviceInfodata,

??????? SPDRP_FRIENDLYNAME,

0, DeviceName, Max_Dev_len, INTPTR.ZERO))

??????? {

???????? res = setupdiGetDeviceRegistryProperty (NewDeviceInfoseet,

????????? deviceInfodata, SPDRP_DEVICEDESC,

0, DeviceName, Max_Dev_len,

??????????? INTPTR.ZERO);

????????

IF (! res) {

????????

// IncorRect Device Name:

??????????????? setUpdideStroyDeviceInfolist (NewDeviceInfoseet);

??????????????? deviceName =

New StringBuilder

"" "

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

Return -

4;

???????????}

???????}

????????

Return

0;

???????}

??????? [stathread]

???????

Static

Void main

String [] ARGS)

??????? {

???????? StringBuilder Devices =

New StringBuilder

"" "

???????? uint32 index =

0;

????????

int result =

0;

????????

IF (args.length! =

1)

????????? {

??????????? console.writeline

"Command Line Format:");

??????????? console.writeline ("Devinfo

"

);

???????????

Return;

?????????}

????????

WHILE

True)

????????? {

??????????? result = enumeratedeVices (Index, Args [

0], Devices);

??????????? INDEX ;

???????????

IF (result == -

2)

??????????????????? {

???????????????????? console.writeline

"IncorRect Name of class = {0}",

?????????????????????? ARGS [

0]);

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

Break;

???????????????????}

???????????

IF (result == -

1)

Break;

???????????

IF (result ==

0) Console.WriteLine

Device {0} is {1} ",

????????????? index, defices;

???????}

???????}

???}

}

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

New Post(0)