WndProc to detect device unplug and plug

Posted by Kev Fixx on Stack Overflow See other posts from Stack Overflow or by Kev Fixx
Published on 2012-09-05T14:51:34Z Indexed on 2012/10/01 3:38 UTC
Read the original article Hit count: 106

Filed under:
|
|

How can I know a devices is plug or unplug in WPF?

I am using the code below to detect device changes:

   private void OnSourceInitialized(object sender, EventArgs e)
            {
                IntPtr windowHandle = (new WindowInteropHelper(this)).Handle;
                HwndSource src = HwndSource.FromHwnd(windowHandle);
                src.AddHook(new HwndSourceHook(WndProc));
            }

            private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
            {
                // Handle WM_DEVICECHANGE... 
                if (msg == 0x219)
                {
                    InitHead();
                }

                return IntPtr.Zero;
            }

Thank you.

EDITED:

I did the below, still not working:

if (msg == 0x0219)
            {
                switch (wParam.ToInt32())
                {
                    case 0x8000:
                        {
                            InitHead();
                        }
                        break;
                }
            }

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET