Esempio n. 1
0
        private void OnDeviceChange(ref Message m)
        {
            if (!mEnabled)
            {
                return;
            }
            if (m.LParam.ToInt64() != 0)     // ToInt32() only works on non-x64 platforms.
            {
                EventHandler <DeviceNotifyEventArgs> temp = OnDeviceNotify;
                if (!ReferenceEquals(temp, null))
                {
                    DeviceNotifyEventArgs args;
                    DevBroadcastHdr       hdr = new DevBroadcastHdr();
                    Marshal.PtrToStructure(m.LParam, hdr);
                    switch (hdr.DeviceType)
                    {
                    case DeviceType.Port:
                    case DeviceType.Volume:
                    case DeviceType.DeviceInterface:
                        args = new WindowsDeviceNotifyEventArgs(hdr, m.LParam, (EventType)m.WParam.ToInt32());
                        break;

                    default:
                        args = null;
                        break;
                    }
                    if (!ReferenceEquals(args, null))
                    {
                        temp(this, args);
                    }
                }
            }
        }
Esempio n. 2
0
        public static DeviceData GetDevice(IntPtr lParam)
        {
            DeviceData itemData = new DeviceData();

            if ((int)lParam == 0)
            {
                return(itemData);
            }

            DevBroadcastHdr hdr = new DevBroadcastHdr();

            Marshal.PtrToStructure(lParam, hdr);
            itemData.DeviceType = ((DbtDeviceType)hdr.dbch_devicetype);
            switch ((DbtDeviceType)hdr.dbch_devicetype)
            {
            case DbtDeviceType.DeviceInterface:
                DevBroadcastDeviceinterfaceVariable devIf = new DevBroadcastDeviceinterfaceVariable();

                // Convert lparam to DEV_BROADCAST_DEVICEINTERFACE structure
                Marshal.PtrToStructure(lParam, devIf);

                // Get the device path from the broadcast message
                itemData.Data = new string(devIf.dbcc_name);

                // Remove null-terminated data from the string
                int pos = itemData.Data.IndexOf((char)0);
                if (pos != -1)
                {
                    itemData.Data = itemData.Data.Substring(0, pos);
                }
                break;

            case DbtDeviceType.Port:
                DevBroadcastPortVariable devPort = new DevBroadcastPortVariable();

                // Convert lparam to DEV_BROADCAST_DEVICEINTERFACE structure
                Marshal.PtrToStructure(lParam, devPort);

                // Get the device path from the broadcast message
                itemData.Data = new string(devPort.dbcc_name);

                // Remove null-terminated data from the string
                pos = itemData.Data.IndexOf((char)0);
                if (pos != -1)
                {
                    itemData.Data = itemData.Data.Substring(0, pos);
                }
                break;

            case DbtDeviceType.Volume:
                DevBroadcastVolume volume = new DevBroadcastVolume();
                Marshal.PtrToStructure(lParam, volume);
                itemData.Data = $"{((volume.dbcv_flags & (int) Dbtf.Media) == 1 ? "Media" : "Net")} {FirstDriveFromMask(volume.dbcv_unitmask)}";
                break;
            }
            return(itemData);
        }
Esempio n. 3
0
        public static bool IsNotificationForTargetDevice(Message m)
        {
            if (string.IsNullOrEmpty(devicepath))
            {
                return(false);
            }

            try
            {
                var devBroadcastDeviceInterface = new DevBroadcastDeviceinterface1();
                var devBroadcastHeader          = new DevBroadcastHdr();

                try
                {
                    Marshal.PtrToStructure(m.LParam, devBroadcastHeader);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    return(false);
                }


                // Is the notification event concerning a device interface?
                if ((devBroadcastHeader.dbch_devicetype == Constants.DbtDevtypDeviceinterface))
                {
                    // Get the device path name of the affected device
                    var stringSize = Convert.ToInt32((devBroadcastHeader.dbch_size - 32) / 2);
                    devBroadcastDeviceInterface.dbcc_name = new Char[stringSize + 1];
                    Marshal.PtrToStructure(m.LParam, devBroadcastDeviceInterface);
                    var deviceNameString = new string(devBroadcastDeviceInterface.dbcc_name, 0, stringSize);
                    // Compare the device name with our target device's pathname (strings are moved to lower case
                    return(string.Compare(deviceNameString.ToLower(), devicepath.ToLower(), StringComparison.OrdinalIgnoreCase) == 0);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("usbGenericHidCommunication:isNotificationForTargetDevice() -> EXCEPTION: An unknown exception has occured!");
                Debug.WriteLine(ex.Message);
                return(false);
            }
            return(false);
        }
Esempio n. 4
0
    public static DevBroadcastDeviceinterface GetDevBroadcastDeviceinterface(Message m)
    {
        DevBroadcastHdr hdr = GetDevBroadcastHdr(m);

        if (hdr.DeviceType == DBT_DEVTYP_DEVICEINTERFACE)
        {
            DevBroadcastDeviceinterface0 dbi0 = (DevBroadcastDeviceinterface0)m.GetLParam(typeof(DevBroadcastDeviceinterface0));
            DevBroadcastDeviceinterface  dbi  = new DevBroadcastDeviceinterface()
            {
                DeviceType = dbi0.DeviceType, ClassGuid = dbi0.ClassGuid
            };
            Byte[] bytes = new Byte[hdr.Size];
            Marshal.Copy((IntPtr)m.LParam, bytes, 0, hdr.Size);
            int offset = Marshal.SizeOf(dbi0) - 2;
            dbi.Name = System.Text.Encoding.Unicode.GetString(bytes, offset, bytes.Length - offset);
            return(dbi);
        }
        return(new DevBroadcastDeviceinterface());
    }
        private string GetDeviceName(Message m)
        {
            try
            {
                if (m.LParam == IntPtr.Zero)
                {
                    return(null);
                }
                var devBroadcastDeviceInterface = new DevBroadcastDeviceinterface1();
                var devBroadcastHeader          = new DevBroadcastHdr();

                try
                {
                    Marshal.PtrToStructure(m.LParam, devBroadcastHeader);
                }
                catch (Exception ex)
                {
                    LogHelper.Log.Error(ex.Message);
                }


                // Is the notification event concerning a device interface?
                if ((devBroadcastHeader.dbch_devicetype == Constants.DbtDevtypDeviceinterface))
                {
                    // Get the device path name of the affected device
                    var stringSize = Convert.ToInt32((devBroadcastHeader.dbch_size - 32) / 2);
                    devBroadcastDeviceInterface.dbcc_name = new Char[stringSize + 1];
                    Marshal.PtrToStructure(m.LParam, devBroadcastDeviceInterface);
                    var deviceNameString = new string(devBroadcastDeviceInterface.dbcc_name, 0, stringSize);
                    return(deviceNameString);
                    // Compare the device name with our target device's pathname (strings are moved to lower case
                    //return (string.Compare(deviceNameString.ToLower(), devicepath.ToLower(), StringComparison.OrdinalIgnoreCase) == 0);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Log.Error("GetDeviceName(Message m) -> 发生异常:", ex);
            }
            return(null);
        }
        ///  <summary>
        ///  Compares two device path names. Used to find out if the device name
        ///  of a recently attached or removed device matches the name of a
        ///  device the application is communicating with.
        ///  </summary>
        ///
        ///  <param name="m"> a WM_DEVICECHANGE message. A call to RegisterDeviceNotification
        ///  causes WM_DEVICECHANGE messages to be passed to an OnDeviceChange routine.. </param>
        ///  <param name="mydevicePathName"> a device pathname returned by
        ///  SetupDiGetDeviceInterfaceDetail in an SP_DEVICE_INTERFACE_DETAIL_DATA structure. </param>
        ///
        ///  <returns>
        ///  True if the names match, False if not.
        ///  </returns>
        ///
        internal Boolean DeviceNameMatch(Message m, String mydevicePathName)
        {
            var devBroadcastDeviceInterface = new DevBroadcastDeviceinterface1();
            var devBroadcastHeader          = new DevBroadcastHdr();

            // The LParam parameter of Message is a pointer to a DEV_BROADCAST_HDR structure.

            Marshal.PtrToStructure(m.LParam, devBroadcastHeader);

            if ((devBroadcastHeader.dbch_devicetype == DbtDevtypDeviceinterface))
            {
                // 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 strucutre that are not part of dbch_name and dividing by 2 because there are
                // 2 bytes per character.
                var stringSize = Convert.ToInt32((devBroadcastHeader.dbch_size - 32) / 2);

                // The dbcc_name parameter of devBroadcastDeviceInterface contains the device name.
                // Trim dbcc_name to match the size of the String.
                devBroadcastDeviceInterface.dbcc_name = new Char[stringSize + 1];

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

                // Store the device name in a String.
                var deviceNameString = new String(devBroadcastDeviceInterface.dbcc_name, 0, stringSize);

                // Compare the name of the newly attached device with the name of the device
                // the application is accessing (mydevicePathName).
                // Set ignorecase True.
                return(String.Compare(deviceNameString, mydevicePathName, StringComparison.OrdinalIgnoreCase) == 0);
            }
            return(false);
        }
Esempio n. 7
0
        ///  <summary>
        ///  Compares two device path names. Used to find out if the device name 
        ///  of a recently attached or removed device matches the name of a 
        ///  device the application is communicating with.
        ///  </summary>
        ///  
        ///  <param name="m"> a WM_DEVICECHANGE message. A call to RegisterDeviceNotification
        ///  causes WM_DEVICECHANGE messages to be passed to an OnDeviceChange routine.. </param>
        ///  <param name="mydevicePathName"> a device pathname returned by 
        ///  SetupDiGetDeviceInterfaceDetail in an SP_DEVICE_INTERFACE_DETAIL_DATA structure. </param>
        ///  
        ///  <returns>
        ///  True if the names match, False if not.
        ///  </returns>
        ///  
        internal Boolean DeviceNameMatch(Message m, String mydevicePathName)
        {
            var devBroadcastDeviceInterface = new DevBroadcastDeviceinterface1();
            var devBroadcastHeader = new DevBroadcastHdr();

            // The LParam parameter of Message is a pointer to a DEV_BROADCAST_HDR structure.

            Marshal.PtrToStructure(m.LParam, devBroadcastHeader);

            if ((devBroadcastHeader.dbch_devicetype == DbtDevtypDeviceinterface))
            {
                // 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 strucutre that are not part of dbch_name and dividing by 2 because there are
                // 2 bytes per character.
                var stringSize = Convert.ToInt32((devBroadcastHeader.dbch_size - 32) / 2);

                // The dbcc_name parameter of devBroadcastDeviceInterface contains the device name.
                // Trim dbcc_name to match the size of the String.
                devBroadcastDeviceInterface.dbcc_name = new Char[stringSize + 1];

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

                // Store the device name in a String.
                var deviceNameString = new String(devBroadcastDeviceInterface.dbcc_name, 0, stringSize);

                // Compare the name of the newly attached device with the name of the device
                // the application is accessing (mydevicePathName).
                // Set ignorecase True.
                return (String.Compare(deviceNameString, mydevicePathName, StringComparison.OrdinalIgnoreCase) == 0);
            }
            return false;
        }
        internal WindowsDeviceNotifyEventArgs(DevBroadcastHdr hdr, IntPtr ptrHdr, EventType eventType)
        {
            mBaseHdr    = hdr;
            mEventType  = eventType;
            mDeviceType = mBaseHdr.DeviceType;
            switch (mDeviceType)
            {
            case DeviceType.Volume:
                mVolume = new VolumeNotifyInfo(ptrHdr);
                mObject = mVolume;
                break;

            case DeviceType.Port:
                mPort   = new PortNotifyInfo(ptrHdr);
                mObject = mPort;
                break;

            case DeviceType.DeviceInterface:
                mDevice = new UsbDeviceNotifyInfo(ptrHdr);
                mObject = mDevice;
                break;
            }
        }