USB Detection

Mar 15, 2011 at 10:53am
Hi All,

I have an application in C#.net and C++.

I am listing all the USB devices connected to the system in the GUI which in C#.
I am calling function in C++ DLL to list all the USB's.
Now If any device get removed or added to the system then I have to reflect those changes on to the GUI.
How should I get such notification and again call my ListUSB function to refresh GUI part.

Thanks for your valuable advice.
Last edited on Mar 15, 2011 at 10:54am
Mar 15, 2011 at 11:21am
closed account (z05DSL3A)
In native code you would call RegisterDeviceNotification() and handle WM_DEVICECHANGE messages.

MSDN sample: http://msdn.microsoft.com/en-us/library/aa363432%28VS.85%29.aspx

I'm not sure how you would do it in managed code but it is a starting point.
Mar 15, 2011 at 1:15pm
Thanks for your reply.

But I am confusing how can I write a program which will notify any change has happened and as a result go for refreshing my GUI with currently attached USB's.

I am confusing that how we can achieve this. Please suggest the way

Please suggest any solution.
Mar 15, 2011 at 2:06pm
closed account (z05DSL3A)
Basically you will need to override the WndProc() of your form and intercept the WM_DEVICECHANGE message, somthing like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public partial class Form1 : Form
    {
        // Constant value was found in the "windows.h" header file.
        private const int WM_DEVICECHANGE = 0x0219;

        public Form1()
        {
            InitializeComponent();
        }
        protected override void WndProc(ref System.Windows.Forms.Message theMessage)
        {
            switch (theMessage.Msg)
            {
                case WM_DEVICECHANGE:
                    MessageBox.Show("Device changed", "WM_DEVICECHANGE");
                    break;

            }
            base.WndProc(ref theMessage);
        }
    }
code: c#

Instead of MessageBox.Show() you would do whatever it is you need to refresh the form.
Last edited on Mar 15, 2011 at 2:07pm
Mar 16, 2011 at 5:19am
Yes I found this solution earlier. But when I want to do this in C++ and send the notification to the
GUI that change has happened. Then what would be the solution.

I think GUI will call one callback Function which will check for any change happen, once the change is happened the GUI get notified and perform the refreshing task.

I am trying in this way but I don't know, is this solution is feasible or not.
Topic archived. No new replies allowed.