USB RegisterDeviceNotification

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bfoo75
    New Member
    • Feb 2008
    • 12

    USB RegisterDeviceNotification

    I've been doing lots of research on controlling USB devices in C# and ran across a few tutorials that seemed good until I started trying to code them. It seems like everyone seems to know how to use the function RegisterDeviceN otification to register the form to recieve WM_DEVICECHANGE messages.

    I've gotten as far as importing the function from user32.dll and setting up the device filtering, but I don't really understand the concept of using RegisterDeviceN otification. Can anyone clarify what and where to get the parameters that its asking for?

    Here is my code so far:

    [code=text]
    using System;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows. Forms;
    using System.Windows;
    using System.Runtime. InteropServices ;
    using DeviceManagemen tDeclarations;


    namespace USBDevice
    {

    public partial class Form1 : Form
    {
    public const int WM_DEVICECHANGE = 0x219;

    public Form1()
    {
    InitializeCompo nent();
    RegisterDeviceN otification(thi s.Handle, ?NotificationFi lter?, ?Flags?);

    }
    protected override void WndProc(ref Message m)
    {
    switch (m.Msg)
    {
    case WM_DEVICECHANGE :
    {
    MessageBox.Show (m.ToString());
    break;
    }

    }

    base.WndProc(re f m);
    }
    [DllImport("user 32.dll", CharSet = CharSet.Auto)]
    static public extern IntPtr RegisterDeviceN otification(Int Ptr hRecipient, IntPtr NotificationFil ter, int Flags);

    }

    }
    [/code]
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Hmm, I was pretty sure I got that notification just fine without doing any registering.

    I will check though.
    EDIT:
    Just checked, yup I get that message regardless of focus or anything and I didn't need to register for it.

    Comment

    • bfoo75
      New Member
      • Feb 2008
      • 12

      #3
      Huh... well I feel stupid. How come all these tutorials make out that you have to register the device notification?

      I think last time I had it compilable I was using a different filter for the message. Thanks for the clear-up tho!

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        I should alert you that DeviceNotify isn't as usefull as you would hope. I only found it really usefull for when a USB->SerialPort was added/removed, or some kind of mass storage usb device was added/removed (i.e. thumbdrive, external harddrive, etc)

        Comment

        • nuno1707
          New Member
          • Mar 2009
          • 1

          #5
          I've been doing the same (some research on monitoring added/removed of mass storage device in C# ) but sa new in C# I don't know what to do!

          Could you clarify it or post you Code?

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Have a look here:
            Notifies an application of a change to the hardware configuration of a device or the computer.

            Comment

            Working...