BY
VLADIMIR AFANASYEV
Introduction
In my previous article, I got a lot of examples of system programming. Many years ago, I especially liked to ponder system forms and system tools. Of course, I didn't have a true PC yet, just similar to IBM360, PDP11 or Microvax, but that feels good. I still like to study on COM interfaces, DLL libraries and hard disk configurations. Ah, I always like system programming!
purpose
Many people think that C # is just a "Child Language". In my opinion, this is definitely wrong! In order to prove my point, I am going to prepare three examples of hardware device configuration, showing how to work with C # and Win32 API (or even DDK). It is mainly to be implemented using P / Invoke. At the same time, C # is perfectly solved with different platform data exchanges, as well as through Windows Forms, making us easier usage applications.
Equipment class
All devices in the system join in the device classes. As you can see in the below picture, the class has name and Guid (so it can be found in Registry). The class can also have a description. For example, for class " Ports "The Description IS" Ports (COM & LPT) ". Class Also Has Devices That Are Present in The Configuration.
All device information exists in the device class. This class has names and GUIDs (so it can be found in the registry). This class has some descriptions, such as the description of class "ports" is "Ports (COM & LPT)".
The System Device Manager gives all the device classes present on the PC:
Below, you will see an example of using C # traversing all devices, which uses P / Invoke, DDK, and SDK DLLs.
Using system;
Using system.Runtime.InteropServices; // p / invoke
Using system.text;
Namespace devclasses
{
///
/// Summary Description for Class.
/// summary>
Class DeviceClasses
{
///
/// 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);
// Maximum orowed Access Type to REG.
Public const Int maximum_allowed = (0x02000000);
[Structlayout (layoutkind.sequential] // To ensure data exchange
Public class sp_devinfo_data // struct can also
{
Public int Cbsize;
Public Guid ClassGUID;
Public int devinst; // devinst handle
Public ulong reserved;
}
// Declare function prototype
[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_CLASES (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 deficEinfodata);
[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 machine,
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);
// Traverse
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 respnam = 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 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 (NewDeviceInfoseet);
Classname = name;
DevicePresent = True;
Return 0;
}
[Stathread]
Static void main (string [] args)
{
Stringbuilder classes = new stringbuilder ("");
Stringbuilder classsdesdescriber = 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;
}
}
}
After running this app, you will see all device classes on your machine.
From:
http://www.codeproject.com/csharp/divingsysprog1.asp
The translator's words:
This is just the first chapter in a series of articles. There is not much in-depth explanation of knowledge, just got an example.
Biqiong2004@hotmail.com
PS:
Original text has pictures, you can't see it here, you can see this article on my homepage.
Http://home.ripway.com/2004-6/124912/