Click here to Skip to main content
15,881,856 members
Articles / Programming Languages / C#
Article

.NET - Diving into System Programming - Part 3

Rate me:
Please Sign up or sign in to vote.
4.84/5 (28 votes)
10 Mar 2004CPOL3 min read 181.4K   1.9K   61   55
Device configuration using C# (Part 3). Device resources - IRQ, DMA, etc.

Introduction

In this last part, you will see a Windows Forms application. This application can enumerate device classes, enumerate devices from device classes and show information about device resources. You can download source code for Borland C# Builder and Visual Studio C#.

Device resources

Every device can use some hardware resources for its work. There are four standard types of resources:

  • Memory Resource
  • I/O Port Resource
  • DMA Resource
  • Interrupt Resource

The type of resources used depends on the device. For example, serial port uses I/O Port Resource and Interrupt Resource. Display (Video adapter), as a rule, uses I/O Port Resource, Interrupt Resource and Memory Resource. Device Manager can show information about resources for every device from configuration.

Image 1

So how to get information about resources.

  • Get class Guid using device class name (SetupDiClassGuidsFromNameA function)
  • Get device info set for device class (SetupDiGetClassDevsA function)
  • Get device info data for every device (SetupDiGetClassDevsA function, second parameter for this function is sequential device index in the device class, so call this function in circle with device index = 0, 1, etc.).
  • Get information about current configuration (CM_Get_First_Log_Conf function)
  • Get resource descriptor from current configuration (CM_Get_Next_Res_Des function, do this and follow steps for every resource till they exist)
  • Get information about size of resource data (CM_Get_Res_Des_Data_Size function)
  • Get resource data (CM_Get_Res_Des_Data function)
  • Marshal resource data in suitable structures (class Marshal, methods PtrToStructure; structure IntPtr, method ToInt32 and vice versa to IntPtr)

You can find details in the source code for the application that you see on the next picture:

Image 2

Configuration and Device Manager

Configuration Manager includes functions from cfgmgr32.dll and Device Management functions include most functions from setupapi.dll. Using these functions, you can recreate system Device Manager (if you really need it) and a lot other things. You can use it for Windows 98/Me/NT4/2000/XP with unmanaged code as well as managed. You can't use this way for early Windows versions. In Windows 95, Configuration Manager is not a DLL but a VXD driver. And I don't know about using it for Windows Server 2003.

What about WMI

WMI is really a very useful and suitable thing. It was developed for such tasks that I examine in these articles also. But WMI is native beginning from Windows 2000. If you want to use WMI for Windows 98/NT4, you need to install WMI Core. Here there is a problem, at least I had a problem. WMI Core is localized but there is no WMI Core for Russian Windows98, for example. Beside this, WMI is not magus, WMI also uses Win32 API to access device configuration.

Registry, Registry and Registry again

Really, Windows Registry is a Central Windows database for all system information. So even if we use cfgmgr32.dll or/and setupapi.dll, anyway in most cases we work with Registry data. It means if you know where to find configuration data in Registry and how to collect and marshal them, you can access data via Win32 Registry API. Of course, you can find even computer name using this API directly but I suppose that GetComputerName Win32 API function is more suitable.

Summary

As you see, some ways exist to get information about device configuration. I examined only one of these. And it is the end of my story about "child language" C#, P/Invoke and device configuration.

References

Information about Configuration and Device Managers are on Microsoft Library site.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Ukraine Ukraine
I am C++ Builder developer.
I am interesting in WMI, Shell, some deep
COM interface. Beside these I am Brainbench
Win32 API Master.
Now I very like Microsoft .NET and C#. I made some firsts OPOS drivers for Ukrainian fiscal printers.

Comments and Discussions

 
AnswerRe: Device enable/disable - Parameters wrong Pin
HiAle3-Dec-05 12:12
HiAle3-Dec-05 12:12 
GeneralRe: Device enable/disable - Parameters wrong Pin
gumigyuri6-Mar-06 22:26
gumigyuri6-Mar-06 22:26 
GeneralRe: Device enable/disable - Parameters wrong Pin
Balagopal Ambalakkat7-Jan-07 17:47
Balagopal Ambalakkat7-Jan-07 17:47 
GeneralRe: Device enable/disable - Parameters wrong Pin
Joe Henrich13-Feb-07 22:38
Joe Henrich13-Feb-07 22:38 
GeneralRe: Device anable/disable Pin
CPfx3000se31-May-06 6:27
CPfx3000se31-May-06 6:27 
GeneralRe: Device anable/disable Pin
Ian_00112-Jul-06 2:36
Ian_00112-Jul-06 2:36 
GeneralRe: Device anable/disable Pin
-=[aDy]=-16-Feb-07 0:30
-=[aDy]=-16-Feb-07 0:30 
GeneralRe: Device anable/disable Pin
DUMITRU Guraliuc9-Jul-07 20:45
DUMITRU Guraliuc9-Jul-07 20:45 
Hi,

I managed to enable/disable a device from C#. I must say that I did not find any articles explaining how to do it so I found the solution by trial and error. However it works for me.

I have a SerialPort Multiplexer that creates 3 virtual serial ports that will send/receive data to/from a single physical COM port.

Steps:

0. using System.Runtime.InteropServices;

1. Just above the class definition define the following type:

using DEVINSTID_A = System.String; // Device ID ANSI name.<br />
public class SerialDevice<br />
{<br />
....


2. Inside the class definition import "cfgmgr32.dll" and "CM_Set_HW_Prof_FlagsA" method as below:

[DllImport("cfgmgr32.dll", EntryPoint = "CM_Set_HW_Prof_FlagsA")]<br />
public static extern void CM_Set_HW_Prof_FlagsA(<br />
                     DEVINSTID_A szDevInstName,<br />
                     uint ulConfig,<br />
                     uint ulValue,<br />
                     uint ulFlags<br />
                     );


3. Call the method to enable disable:

- DISABLE
TG.Devices.Enquirer.SerialEnquirer.CM_Set_HW_Prof_FlagsA(@"ROOT\WINMUX\0000", 0, 0x00000001, 0);

- ENABLE
TG.Devices.Enquirer.SerialEnquirer.CM_Set_HW_Prof_FlagsA(@"ROOT\WINMUX\0000", 0, 0x00000000, 0);

The Device Instance ID can be found by goind to Device Manager -> Properties (0f the COM port) -> Details (and select from the combo the Device Instance ID string). It will display the value that you must use when calling the method.
GeneralImage problem Pin
p daddy12-Mar-04 0:33
p daddy12-Mar-04 0:33 
GeneralRe: Image problem Pin
p daddy12-Mar-04 0:58
p daddy12-Mar-04 0:58 
GeneralRe: Image problem Pin
Vladimir Afanasyev12-Mar-04 1:02
Vladimir Afanasyev12-Mar-04 1:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.