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

Calling Unmanaged Code from .NET and Device Detection with C#.NET

Rate me:
Please Sign up or sign in to vote.
4.12/5 (11 votes)
23 Apr 2007CPOL3 min read 86.5K   1.1K   45   23
An article that explores hardware insertion or removal

Introduction

Receiving "Hardware insertion or removal" is an interesting thing to consider while programming in Windows. In fact, we may have already done this in our MFC or SDK applications. As some of readers of my previous article suggested, I am trying to submit an article here on Device Detection in C#.NET. The intention of the article is to share the concepts which I found from other sources on the web. I hope this article satisfies my intentions.

Calling Unmanaged Code from .NET

As we already know the .NET Framework is a development and execution environment that allows different programming languages and libraries to work together seamlessly to create Windows-based applications.

Despite the breadth and depth of the .NET Framework, it cannot do everything. There are times when you need to do something that is not covered by the Framework.

This situation may occur in many scenarios, but the two most common contexts are:

  1. When there is some capability that you know is part of the Windows API but is not yet in the .NET Framework.
  2. When you are programming an external device whose drivers are written for the non-.NET programmer.

In both scenarios, our program will need to call unmanaged code. More specifically, it needs to call code that is provided as part of a dynamic link library or DLL; the Windows DLL in the first case, and a manufacturer-provided DLL in the second.

Basically the code which is executing under the control of the runtime is called managed code. Conversely, code that runs outside the runtime is called unmanaged code. COM components, ActiveX interfaces, and Win32 API functions are examples of unmanaged code.

How do we call functions in an unmanaged code DLL from your managed code .NET program? This is achieved in .NET by a concept called "Interoperability".

The Interop Service

This namespace provides a wide variety of members that support COM interop and platform invoke services. This is our key to achieving device detection in C#.NET.

The DllImport keyword present in this namespace is used to import the unmanaged DLL of context to our .NET application.

If we know the function name and constants which are present in the unmanaged DLL, we can import all of them in our .NET application.

For an example, I will use the Windows Sleep() function, which has the following declaration (in C):

C#
BOOL Sleep(
        DWORD dwDuration    
//Duration in milliseconds
};

To make this function accessible in your managed code, you would write the DllImport statement like this:

C#
[DllImport("Kernel32.dll")]
    static extern Boolean Sleep( 
       UInt32 duration);

With this DllImport statement in our managed code, we can call the Sleep function like any other function:

C#
Sleep(1000);

Device detection in C#

Whenever a device arrival/removal happens, Windows sends a message called WM_DEVICECHANGE to all the applications running currently in the system. But to receive this message our application should handle the "Windows Process function". C# applications will not have default support for this function; we need to explicitly override this method in our Form class.

If we want to find the type of the device inserted (specifically a USB mass storage) then we need to include the structure which is defined in the unmanaged area.

For that we need to do marshalling. Marshalling is a separate topic and there are lots of articles on this topic available on the net. So I am simply going to show a sample.

C#
[StructLayout(LayoutKind.Sequential)] 
public struct DEV_BROADCAST_VOLUME 
{ 
    public int dbcv_size; 
    public int dbcv_devicetype; 
    public int dbcv_reserved; 
    public int dbcv_unitmask; 
} 
//Use the Interopservices to make the Unmanaged call.

using System.Runtime.InteropServices;

protected override void WndProc(ref Message m) 
{ 
    //you may find these definitions in dbt.h and winuser.h 
    const int WM_DEVICECHANGE = 0x0219; 
    // system detected a new device 
    const int DBT_DEVICEARRIVAL = 0x8000;
    // system detected a new device 
    const int DBT_DEVICEREMOVECOMPLETE = 0x8001;    
    // logical volume 
    const int DBT_DEVTYP_VOLUME = 0x00000002;  
    switch(m.Msg)
    {
    case WM_DEVICECHANGE:
        switch(m.WParam.ToInt32())
        {
        case DBT_DEVICEARRIVAL:
            { 
                int devType = Marshal.ReadInt32(m.LParam,4); 
                if(devType == DBT_DEVTYP_VOLUME) 
                { 
                    DEV_BROADCAST_VOLUME vol; 
                    vol = (DEV_BROADCAST_VOLUME)
                        Marshal.PtrToStructure(
                        m.LParam,typeof(DEV_BROADCAST_VOLUME)); 

                    MessageBox.Show(
                        vol.dbcv_unitmask.ToString("x"));
                } 
            } 
            break;
        case DBT_DEVICEREMOVECOMPLETE:
            MessageBox.Show("Removal");
            break;
        }
        break;
    }
    //we detect the media arrival event 
    base.WndProc (ref m); 
}

Building the Sample Code

It is a simple C# Windows form application. Building the sample code requires the Visual Studio.NET 2003 compiler. Execute the application and insert/remove the USB Mass storage. The message box for device arrival and removal will appear.

License

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionRemoval message does not appear Pin
TAN TH23-Apr-12 17:15
TAN TH23-Apr-12 17:15 
when I remove the USB drive, I do not see any Removal message, why?

When I insert a USB drive, I do see a ID number.

If I insert a USB scanner, I want to see the ID number too, why code should I modify?

thanks.
GeneralCode does not detect USB scanners Pin
codeguruj16-Jun-08 5:08
codeguruj16-Jun-08 5:08 
GeneralRe: Code does not detect USB scanners Pin
svsundar (Vairavan)22-Jul-08 10:58
svsundar (Vairavan)22-Jul-08 10:58 
GeneralRe: Code does not detect USB scanners Pin
Vimalsoft(Pty) Ltd12-Feb-09 19:48
professionalVimalsoft(Pty) Ltd12-Feb-09 19:48 
GeneralRe: Code does not detect USB scanners Pin
svsundar (Vairavan)13-Feb-09 20:07
svsundar (Vairavan)13-Feb-09 20:07 
GeneralRe: Code does not detect USB scanners Pin
Vimalsoft(Pty) Ltd16-Feb-09 0:42
professionalVimalsoft(Pty) Ltd16-Feb-09 0:42 
QuestionModify code for any device? Pin
AgentM0075-Dec-07 14:58
AgentM0075-Dec-07 14:58 
AnswerRe: Modify code for any device? Pin
svsundar (Vairavan)22-Jul-08 10:59
svsundar (Vairavan)22-Jul-08 10:59 
GeneralRe: Modify code for any device? Pin
B.V.Papadopoulos15-Dec-09 22:35
professionalB.V.Papadopoulos15-Dec-09 22:35 
GeneralAutoplay option for USB Pin
Anand Kingmaker26-Sep-07 21:31
Anand Kingmaker26-Sep-07 21:31 
GeneralRe: Autoplay option for USB Pin
svsundar (Vairavan)4-Oct-07 0:28
svsundar (Vairavan)4-Oct-07 0:28 
GeneralRe: Autoplay option for USB Pin
Anand Kingmaker4-Oct-07 21:18
Anand Kingmaker4-Oct-07 21:18 
GeneralSample code Pin
Raymond Verbruggen19-Apr-07 0:51
Raymond Verbruggen19-Apr-07 0:51 
GeneralRe: Sample code Pin
svsundar (Vairavan)24-Apr-07 4:36
svsundar (Vairavan)24-Apr-07 4:36 
Generalsleep() problem.. Pin
rahmanAi16-Apr-07 20:27
rahmanAi16-Apr-07 20:27 
GeneralRe: sleep() problem.. Pin
svsundar (Vairavan)16-Apr-07 21:05
svsundar (Vairavan)16-Apr-07 21:05 
GeneralRe: sleep() problem.. Pin
svsundar (Vairavan)17-Apr-07 4:47
svsundar (Vairavan)17-Apr-07 4:47 
GeneralRe: sleep() problem.. Pin
rahmanAi17-Apr-07 19:55
rahmanAi17-Apr-07 19:55 
GeneralReally nice article Pin
Venu S12-Apr-07 2:13
Venu S12-Apr-07 2:13 
GeneralGreat article! Pin
thebeekeeper11-Apr-07 9:11
thebeekeeper11-Apr-07 9:11 
GeneralRe: Great article! Pin
svsundar (Vairavan)12-Apr-07 1:54
svsundar (Vairavan)12-Apr-07 1:54 
GeneralRe: Great article! Pin
Member 37550634-Jun-08 16:49
professionalMember 37550634-Jun-08 16:49 
GeneralRe: Great article! Pin
svsundar (Vairavan)22-Jul-08 11:03
svsundar (Vairavan)22-Jul-08 11:03 

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.