Tutorials to .com

Tutorials to .com » Dotnet » Oop » . NET - depth System Programming - Part 1

. NET - depth System Programming - Part 1

Print View , by: iSee ,Total views: 2 ,Word Count: 1251 ,Date: Sat, 18 Apr 2009 Time: 4:35 AM

. NET - depth system Programming - Part 1
By Vladimir Afanasyev

In my previous article inside, I cited many examples of system programming. Many years ago, I especially liked the refined form and system tools system. Of course, then I do not have a real PC, just like IBM360, PDP11 or microVAX, but feeling good. Over the years I still like the COM interface, DLL library and hard disk configurations for more research. Aha, I have always liked System Programming!


Many people think that c # is just a "child language". In my opinion, this is absolutely wrong! To prove my point of view, I am ready to prepare the three on the hardware configuration examples show how to use C++ # and Win32 API (or DDK) to work together. Mainly use P / Invoke to achieve. At the same time, C # is the perfect solution to the exchange of data on different platforms, as well as through the Windows Forms, makes us more easy-to-use applications.

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 the equipment information exists in the equipment category. This class has a name and Guid (so it can be found in the registry). There are also described in this category, such as the category "Ports" the description is "Ports (COM & LPT)".
System Device Manager in the PC given the existence of all the equipment information:
Next, you will see that we can use C # traverse all examples of equipment, it uses P / Invoke, DDK and SDK DLLs.

using System;
using System.Runtime.InteropServices; / / P / Invoke necessary
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)] / / In order to ensure the smooth exchange of data

public class SP_DEVINFO_DATA / / struct can also be
public int cbSize;
public Guid ClassGuid;
public int DevInst; / / DEVINST handle
public ulong Reserved;

/ / Function prototype statement
[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);

/ / Traversal
public static int EnumerateClasses (UInt32 ClassIndex,
ref StringBuilder ClassName, StringBuilder ClassDescription,
ref bool DevicePresent)
Guid ClassGuid = Guid.Empty;
IntPtr NewDeviceInfoSet;
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,
ref RequiredSize);
if (RequiredSize> 0)
name.Capacity = (int) RequiredSize;
resNam = SetupDiClassNameFromGuidA (ref ClassGuid, name,
RequiredSize, ref RequiredSize);

NewDeviceInfoSet = SetupDiGetClassDevsA (
ref ClassGuid,

if (NewDeviceInfoSet.ToInt32 () == -1)
(DevicePresent = false;
ClassName = name;
return 0;)

IntPtr KeyClass = SetupDiOpenClassRegKeyExA (
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;


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

After running this application, you will see your machines all equipment.


The translator as saying:
This is only author of a series of articles in the chapter. Do not have much depth on knowledge, only one example cited.
The original pictures, this may not see, you can view my home page article.

.Net Object Oriented Programming Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.