views:

382

answers:

2

I am trying to write a class library that can catch the windows messages to notify me if a device has been attached or removed. Normally, in a windows forms app I would just override the WndProc method but there is not WndProc method in this case. Is there another way I can get the messages?

A: 

Have you checked out the NativeWindow class?

Mitch Wheat
It is a wrapper for a window, you can't actually create a window with it.
Hans Passant
I tried this and couldn't get it to work...
Jordan S
+3  A: 

You'll need a window, there's no way around that. Here's a sample implementation. Implement an event handler for the DeviceChangeNotifier.DeviceNotify event to get notifications. Call the DeviceChangeNotifier.Start() method at the start of your program. Call DeviceChangeNotifier.Stop() at the end of your program. Beware that the DeviceNotify event is raised on a background thread, be sure to lock as needed to keep your code thread-safe.

using System;
using System.Windows.Forms;
using System.Threading;

class DeviceChangeNotifier : Form {
  public delegate void DeviceNotifyDelegate(Message msg);
  public static event DeviceNotifyDelegate DeviceNotify;
  private static DeviceChangeNotifier mInstance;

  public static void Start() {
    Thread t = new Thread(runForm);
    t.SetApartmentState(ApartmentState.STA);
    t.IsBackground = true;
    t.Start();
  }
  public static void Stop() {
    if (mInstance == null) throw new InvalidOperationException("Notifier not started");
    DeviceNotify = null;
    mInstance.Invoke(new MethodInvoker(mInstance.endForm));
  }
  private static void runForm() {
    Application.Run(new DeviceChangeNotifier());
  }

  private void endForm() {
    this.Close();
  }
  protected override void SetVisibleCore(bool value) {
    // Prevent window getting visible
    if (mInstance == null) CreateHandle();
    mInstance = this;
    value = false;
    base.SetVisibleCore(value);
  }
  protected override void WndProc(ref Message m) {
    // Trap WM_DEVICECHANGE
    if (m.Msg == 0x219) {
      DeviceNotifyDelegate handler = DeviceNotify;
      if (handler != null) handler(m);
    }
    base.WndProc(ref m);
  }
}
Hans Passant
+1 Great example, and nobugz is right, you need a window.
SwDevMan81
+1 . Nice example.
Mitch Wheat
I see you use .Invoke. Does this work if the delegate is not in a UI?
Padu Merloti
I don't. The event is raised on the background thread.
Hans Passant
@nobugz, I am using this and it works but for some reason I am receiving two device attached messages every time I plug in my device. Any idea why?
Jordan S
@jordan - pay attention to the wparam value of the message.
Hans Passant
I'm an idiot and was calling the EventHandler twice... Sorry for wasting your time
Jordan S