예제 #1
0
        internal string CheckDeviceChangeMessageSenderIsTargetDeviceOrNot(IntPtr LParam)
        {
            try
            {
                NativeMethods.DEV_BROADCAST_DEVICEINTERFACE devBroadcastDeviceInterface = new NativeMethods.DEV_BROADCAST_DEVICEINTERFACE();
                NativeMethods.DEV_BROADCAST_HDR             devBroadcastHdr             = (NativeMethods.DEV_BROADCAST_HDR)Marshal.PtrToStructure(LParam, typeof(NativeMethods.DEV_BROADCAST_HDR));

                if (devBroadcastHdr.dbch_devicetype == NativeMethods.DeviceType.DBT_DEVTYP_DEVICEINTERFACE)
                {
                    // The dbch_devicetype parameter indicates that the event applies to a device interface.
                    // So the structure in LParam is actually a DEV_BROADCAST_INTERFACE structure,
                    // which begins with a DEV_BROADCAST_HDR.

                    // Obtain the number of characters in dbch_name by subtracting the 32 bytes
                    // in the structure that are not part of dbch_name and dividing by 2 because there are
                    // 2 bytes per character.
                    int stringSize = System.Convert.ToInt32((devBroadcastHdr.dbch_size - 32) / 2);

                    // The dbcc_name parameter of broadcastDeviceInterface contains the device name.
                    // Trim dbcc_name to match the size of the string.
                    devBroadcastDeviceInterface.dbcc_name = new string(new char[stringSize + 1]);

                    // Marshal data from the unmanaged block pointed to by m.LParam
                    // to the managed object broadcastDeviceInterface.
                    devBroadcastDeviceInterface = (NativeMethods.DEV_BROADCAST_DEVICEINTERFACE)Marshal.PtrToStructure(LParam, typeof(NativeMethods.DEV_BROADCAST_DEVICEINTERFACE));

                    return(devBroadcastDeviceInterface.dbcc_name);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
            return(string.Empty);
        }
        /// <summary>
        /// Worker function for WM_DEVICECHANGE messages. Invokes DeviceChangedMsg event.
        /// </summary>
        /// <param name="msg">Native Windows message - WM_DEVICECHANGE</param>
        private void OnDeviceChange(Message msg)
        {
            DeviceChangeEvent devEvent   = DeviceChangeEvent.Unknown;
            String            devDetails = String.Empty;

            if (msg.LParam != IntPtr.Zero)
            {
                NativeMethods.DEV_BROADCAST_HDR db = (NativeMethods.DEV_BROADCAST_HDR)Marshal.PtrToStructure(msg.LParam, typeof(NativeMethods.DEV_BROADCAST_HDR));

                switch (msg.WParam.ToInt32())
                {
                case NativeMethods.DBT_DEVICEARRIVAL:
                    if (db.dbch_devicetype == NativeMethods.DBT_DEVTYP.DEVICEINTERFACE)
                    {
                        NativeMethods.DEV_BROADCAST_DEVICEINTERFACE dbdi = (NativeMethods.DEV_BROADCAST_DEVICEINTERFACE)Marshal.PtrToStructure(msg.LParam, typeof(NativeMethods.DEV_BROADCAST_DEVICEINTERFACE));
                        if (dbdi.dbcc_classguid == NativeMethods.GUID_DEVINTERFACE_USB_DEVICE)
                        {
                            devEvent   = DeviceChangeEvent.DeviceArrival;
                            devDetails = dbdi.dbcc_name;
                        }
                        else if (dbdi.dbcc_classguid == NativeMethods.GUID_DEVINTERFACE_USB_HUB)
                        {
                            devEvent   = DeviceChangeEvent.HubArrival;
                            devDetails = dbdi.dbcc_name;
                        }
                    }
                    else if (db.dbch_devicetype == NativeMethods.DBT_DEVTYP.VOLUME)
                    {
                        NativeMethods.DEV_BROADCAST_VOLUME dbv = (NativeMethods.DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(msg.LParam, typeof(NativeMethods.DEV_BROADCAST_VOLUME));
                        devEvent   = DeviceChangeEvent.VolumeArrival;
                        devDetails = DrivesFromMask(dbv.dbcv_unitmask);
                    }
                    else if (db.dbch_devicetype == NativeMethods.DBT_DEVTYP.PORT)
                    {
                        NativeMethods.DEV_BROADCAST_PORT dbp = (NativeMethods.DEV_BROADCAST_PORT)Marshal.PtrToStructure(msg.LParam, typeof(NativeMethods.DEV_BROADCAST_PORT));
                        devEvent   = DeviceChangeEvent.PortArrival;
                        devDetails = dbp.dbcp_name;
                    }
                    break;

                case NativeMethods.DBT_DEVICEREMOVECOMPLETE:
                    if (db.dbch_devicetype == NativeMethods.DBT_DEVTYP.DEVICEINTERFACE)
                    {
                        NativeMethods.DEV_BROADCAST_DEVICEINTERFACE dbdi = (NativeMethods.DEV_BROADCAST_DEVICEINTERFACE)Marshal.PtrToStructure(msg.LParam, typeof(NativeMethods.DEV_BROADCAST_DEVICEINTERFACE));

                        if (dbdi.dbcc_classguid == NativeMethods.GUID_DEVINTERFACE_USB_DEVICE)
                        {
                            devEvent   = DeviceChangeEvent.DeviceRemoval;
                            devDetails = dbdi.dbcc_name;
                        }
                        else if (dbdi.dbcc_classguid == NativeMethods.GUID_DEVINTERFACE_USB_HUB)
                        {
                            devEvent   = DeviceChangeEvent.HubRemoval;
                            devDetails = dbdi.dbcc_name;
                        }
                    }
                    else if (db.dbch_devicetype == NativeMethods.DBT_DEVTYP.VOLUME)
                    {
                        NativeMethods.DEV_BROADCAST_VOLUME dbv = (NativeMethods.DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(msg.LParam, typeof(NativeMethods.DEV_BROADCAST_VOLUME));
                        devEvent   = DeviceChangeEvent.VolumeRemoval;
                        devDetails = DrivesFromMask(dbv.dbcv_unitmask);
                    }
                    else if (db.dbch_devicetype == NativeMethods.DBT_DEVTYP.PORT)
                    {
                        NativeMethods.DEV_BROADCAST_PORT dbp = (NativeMethods.DEV_BROADCAST_PORT)Marshal.PtrToStructure(msg.LParam, typeof(NativeMethods.DEV_BROADCAST_PORT));
                        devEvent   = DeviceChangeEvent.PortRemoval;
                        devDetails = dbp.dbcp_name;
                    }
                    break;

                default:
                    Trace.Assert(false, "Invalid msg.WParam.");
                    break;
                } // end switch (nEventType)

                Trace.WriteLine(String.Format("*** DeviceChangeWindow.OnDeviceChange(), {0}, {1}, {2}({3})", devEvent, devDetails, Thread.CurrentThread.Name, Thread.CurrentThread.GetHashCode()));

                // let's figure out what to do with the WM_DEVICECHANGE message
                // after we get out of this loop so we don't miss any messages.
                if (DeviceChangedMsg != null)
                {
                    DeviceChangedMsg.BeginInvoke(devEvent, devDetails, null, null);
                }
            } // end if (lpdb)

            msg.Result = new IntPtr(1); // true
            return;
        }