Thursday, March 15, 2012

Diagnose Connection of Devices / Removable Mediums

In the previous post I explained basics about the function WndProc().
In this post I now want to show, how to use this function to find out, if device settings have changed, for example if new hardware like mouses, USB sticks or a CD were plugged in / inserted.
If such a setting changes, the operating system sends our C# application the message WM_DEVICECHANGE (hexadecimal the value 219).

The following code produces a message, if above is the case:

const int WM_DEVICECHANGE = 0x219;
protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_DEVICECHANGE)
        MessageBox.Show("Device settings changed.");
    base.WndProc(ref m);
}

In the next posts I show, how to recognize, if a USB flash drive is plugged in or a CD inserted.

No comments:

Post a Comment