///  <summary>
        ///  Called when a WM_DEVICECHANGE message has arrived,
        ///  indicating that a device has been attached or removed.
        ///  </summary>
        ///
        ///  <param name="m"> a message with information about the device </param>

        public void OnDeviceChange(Message m)
        {
            Tracer.Trace("OnDeviceChange() - m.WParam=" + m.WParam);

            try
            {
                switch (m.WParam.ToInt32())
                {
                case DeviceManagement.DBT_DEVNODES_CHANGED:
                    // this one comes independent of the registration.
                    Tracer.Trace("DBT_DEVNODES_CHANGED");
                    break;

                case DeviceManagement.DBT_DEVICEARRIVAL:
                    // you have to issue MyDeviceManagement.RegisterForDeviceNotifications() first to receive this notification.
                    //  If WParam contains DBT_DEVICEARRIVAL, a device has been attached.

                    Tracer.Trace("A device has been attached.");

                    //  Find out if it's the device we're communicating with.

                    if (MyDeviceManagement.DeviceNameMatch(m, myDevicePathName))
                    {
                        Tracer.Trace("OK: My device attached.");
                    }

                    break;

                case DeviceManagement.DBT_DEVICEREMOVECOMPLETE:
                    // you have to issue MyDeviceManagement.RegisterForDeviceNotifications() first to receive this notification.
                    //  If WParam contains DBT_DEVICEREMOVAL, a device has been removed.

                    Tracer.Trace("Warning: A device has been removed.");

                    //  Find out if it's the device we're communicating with.

                    if (MyDeviceManagement.DeviceNameMatch(m, myDevicePathName))
                    {
                        Tracer.Trace("Warning: My device removed.");

                        //  Set MyDeviceDetected False so on the next data-transfer attempt,
                        //  FindTheHid() will be called to look for the device
                        //  and get a new handle.

                        myDeviceDetected = false;
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex);
                throw;
            }
        }
 /// <summary>
 /// Handler for WndProc WM_DEVICECHANGE message.
 /// </summary>
 /// <param name="m"></param>
 private void OnDeviceChange(Message m)
 {
     if (m.WParam.ToInt32() == DeviceManagement.DBT_DEVICEARRIVAL)
     {
         if (MyDeviceManagement.DeviceNameMatch(m, myDevicePathName))
         {
             Debug.WriteLineIf(WRITE_DEBUG_INFO, "Nintendo controller attached.");
         }
     }
     else if (m.WParam.ToInt32() == DeviceManagement.DBT_DEVICEREMOVECOMPLETE)
     {
         if (MyDeviceManagement.DeviceNameMatch(m, myDevicePathName))
         {
             Debug.WriteLineIf(WRITE_DEBUG_INFO, "Nintendo controller removed.");
             myDeviceDetected = false;
         }
     }
 }