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 );
// Devicedesc (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 deviceinfoset, uint32 memberindex,
Sp_devinfo_data deviceinfodata );
[Dllimport ("setupapi. dll")]
Public static extern Boolean
Setupdidestroydeviceinfolist (intptr deviceinfoset );
[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. empty;
Guid [] guids = new guid [1];
Intptr newdeviceinfoset;
Sp_devinfo_data deviceinfodata = new sp_devinfo_data ();
Bool res = setupdiclassguidsfromnamea (classname,
Ref guids [0], requiredsize,
Ref requiredsize );
If (requiredsize = 0)
{
// Incorrect Class Name:
Devicename = new stringbuilder ("");
Return-2;
}
If (! Res)
{
Guids = new guid [requiredsize];
Res = setupdiclassguidsfromnamea (classname, ref guids [0], requiredsize,
Ref requiredsize );
If (! Res | requiredsize = 0)
{
// Incorrect Class Name:
Devicename = new stringbuilder ("");
Return-2;
}
}
// Get device info set for our device class
Newdeviceinfoset = setupdigetclassdevsa (ref guids [0], 0, intptr. Zero,
Digcf_present );
If (newdeviceinfoset. toint32 () =-1)
If (! Res)
{
// 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 (newdeviceinfoset,
Deviceindex, deviceinfodata );
If (! Res ){
// No such device:
Setupdidestroydeviceinfolist (newdeviceinfoset );
Devicename = new stringbuilder ("");
Return-1;
}
Devicename. capacity = max_dev_len;
If (! Setupdigetdeviceregistrypropertya (newdeviceinfoset,
Deviceinfodata,
Spdrp_friendlyname, 0, devicename, max_dev_len, intptr. Zero ))
{
Res = setupdigetdeviceregistrypropertya (newdeviceinfoset,
Deviceinfodata, spdrp_devicedesc, 0, devicename, max_dev_len,
Intptr. Zero );
If (! Res ){
// Incorrect device name:
Setupdidestroydeviceinfolist (newdeviceinfoset );
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 <classname> ");
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, devices );
}
}
}
}