예제 #1
0
 /// <summary>
 /// Override to handle USB Port notification.
 /// </summary>
 /// <param name="args">Port arguments</param>
 public virtual void OnUsbPort(UsbEventPortArgs args)
 {
 }
예제 #2
0
 /// <summary>
 /// Internal call of CallUsbPort
 /// </summary>
 /// <param name="sender">Sender</param>
 /// <param name="args">Arguments</param>
 public void CallUsbPort(object sender, UsbEventPortArgs args)
 {
     this.UsbPort?.Invoke(sender, args);
 }
예제 #3
0
        /// <summary>
        /// Main windows message handler and dispatcher
        /// </summary>
        /// <param name="monitor">Monitor class to signal notifications</param>
        /// <param name="hwnd">Window handle</param>
        /// <param name="msg">Window message</param>
        /// <param name="wparam">W parameter</param>
        /// <param name="lparam">L parameter</param>
        internal static void HwndHandler(IUsbMonitor monitor, IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam)
        {
            if (msg == WM_DEVICECHANGE)
            {
                UsbDeviceChangeEvent deviceChangeEvent = (UsbDeviceChangeEvent)wparam.ToInt32();
                switch (deviceChangeEvent)
                {
                case UsbDeviceChangeEvent.Arrival:
                case UsbDeviceChangeEvent.QueryRemove:
                case UsbDeviceChangeEvent.QueryRemoveFailed:
                case UsbDeviceChangeEvent.RemovePending:
                case UsbDeviceChangeEvent.RemoveComplete:
                    UsbDeviceType deviceType = (UsbDeviceType)Marshal.ReadInt32(lparam, 4);
                    switch (deviceType)
                    {
                    case UsbDeviceType.OEM:
#if WINDOWS_UWP
                        var oem = Marshal.PtrToStructure <DevBroadcastOEM>(lparam);
#else
                        var oem = (DevBroadcastOEM)Marshal.PtrToStructure(lparam, typeof(DevBroadcastOEM));
#endif
                        Debug.WriteLine($"OEM: Size={oem.Size}, DeviceType={oem.DeviceType}, Reserved={oem.Reserved}, Identifier={oem.Identifier}, SuppFunc={oem.SuppFunc}");
                        var oemArgs = new UsbEventOemArgs(deviceChangeEvent, oem.Identifier, oem.SuppFunc);
                        // fire event
                        (monitor as IUsbMonitorEvents)?.CallUsbOem(monitor, oemArgs);
                        // execute command
                        if ((monitor as IUsbMonitorCommands)?.UsbOemCommand?.CanExecute(oemArgs) ?? false)
                        {
                            (monitor as IUsbMonitorCommands)?.UsbOemCommand?.Execute(oemArgs);
                        }
                        // call virtual method
                        (monitor as IUsbMonitorOverrides)?.OnUsbOem(oemArgs);
                        break;

                    case UsbDeviceType.Volume:
#if WINDOWS_UWP
                        var volume = Marshal.PtrToStructure <DevBroadcastVolume>(lparam);
#else
                        var volume = (DevBroadcastVolume)Marshal.PtrToStructure(lparam, typeof(DevBroadcastVolume));
#endif
                        char[] drives    = DrivesFromMask(volume.UnitMask);
                        string drivesStr = drives.Select(d => $"{d}:").Aggregate((a, b) => $"{a}, {b}");
                        Debug.WriteLine($"Volume: size={volume.Size}, deviceType={volume.DeviceType}, reserved={volume.Reserved}, unitmask={volume.UnitMask}, flags={volume.Flags}, drives={drivesStr}");
                        var volumeArgs = new UsbEventVolumeArgs(deviceChangeEvent, volume.UnitMask, volume.Flags, drives);
                        // fire event
                        (monitor as IUsbMonitorEvents)?.CallUsbVolumem(monitor, volumeArgs);
                        // execute command
                        if ((monitor as IUsbMonitorCommands)?.UsbVolumeCommand?.CanExecute(volumeArgs) ?? false)
                        {
                            (monitor as IUsbMonitorCommands)?.UsbVolumeCommand?.Execute(volumeArgs);
                        }
                        // call virtual method
                        (monitor as IUsbMonitorOverrides)?.OnUsbVolume(volumeArgs);
                        break;

                    case UsbDeviceType.Port:
#if WINDOWS_UWP
                        var port = Marshal.PtrToStructure <DevBroadcastPort>(lparam);
#else
                        var port = (DevBroadcastPort)Marshal.PtrToStructure(lparam, typeof(DevBroadcastPort));
#endif
                        Debug.WriteLine($"Port: Size={port.Size}, DeviceType={port.DeviceType}, Reserved={port.Reserved}, Name={port.Name}");
                        var portArgs = new UsbEventPortArgs(deviceChangeEvent, port.Name);
                        // fire event
                        (monitor as IUsbMonitorEvents)?.CallUsbPort(monitor, portArgs);
                        // execute command
                        if ((monitor as IUsbMonitorCommands)?.UsbPortCommand?.CanExecute(portArgs) ?? false)
                        {
                            (monitor as IUsbMonitorCommands)?.UsbPortCommand?.Execute(portArgs);
                        }
                        // call virtual method
                        (monitor as IUsbMonitorOverrides)?.OnUsbPort(portArgs);
                        break;

                    case UsbDeviceType.DeviceInterface:
#if WINDOWS_UWP
                        var device = Marshal.PtrToStructure <DevBroadcastDeviceInterface>(lparam);
#else
                        var device = (DevBroadcastDeviceInterface)Marshal.PtrToStructure(lparam, typeof(DevBroadcastDeviceInterface));
#endif
                        Debug.WriteLine($"DeviceInterface: Size={device.Size}, DeviceType={device.DeviceType}, Reserved={device.Reserved}, ClassGuid={device.ClassGuid}, Name={device.Name}");
                        var interfaceArgs = new UsbEventDeviceInterfaceArgs(deviceChangeEvent, device.ClassGuid, GuidToEnum <UsbDeviceInterface>(device.ClassGuid), device.Name);
                        // fire event
                        (monitor as IUsbMonitorEvents)?.CallUsbDeviceInterface(monitor, interfaceArgs);
                        // execute command
                        if ((monitor as IUsbMonitorCommands)?.UsbDeviceInterfaceCommand?.CanExecute(interfaceArgs) ?? false)
                        {
                            (monitor as IUsbMonitorCommands)?.UsbDeviceInterfaceCommand?.Execute(interfaceArgs);
                        }
                        // call virtual method
                        (monitor as IUsbMonitorOverrides)?.OnUsbInterface(interfaceArgs);
                        break;

                    case UsbDeviceType.Handle:
#if WINDOWS_UWP
                        var handle = Marshal.PtrToStructure <DevBroadcastHandle>(lparam);
#else
                        var handle = (DevBroadcastHandle)Marshal.PtrToStructure(lparam, typeof(DevBroadcastHandle));
#endif
                        Debug.WriteLine($"DeviceInterface: Size={handle.Size}, DeviceType={handle.DeviceType}, Reserved={handle.Reserved}, Handle={handle.Handle}, DevNotify={handle.DevNotify}, EventGuid={handle.EventGuid}, NameOffset={handle.NameOffset}, Data={handle.Data}");
                        var handleArgs = new UsbEventHandleArgs(deviceChangeEvent, handle.Handle, handle.DevNotify, handle.EventGuid, handle.NameOffset, handle.Data);
                        // fire event
                        (monitor as IUsbMonitorEvents)?.CallUsbHandle(monitor, handleArgs);
                        // execute command
                        if ((monitor as IUsbMonitorCommands)?.UsbHandleCommand?.CanExecute(handleArgs) ?? false)
                        {
                            (monitor as IUsbMonitorCommands)?.UsbHandleCommand?.Execute(handleArgs);
                        }
                        // call virtual method
                        (monitor as IUsbMonitorOverrides)?.OnUsbHandle(handleArgs);
                        break;

                    default:
                        break;
                    }
                    break;

                case UsbDeviceChangeEvent.Changed:
                    var changedArgs = new UsbEventArgs(deviceChangeEvent);
                    // fire event
                    (monitor as IUsbMonitorEvents)?.CallUsbChanged(monitor, changedArgs);
                    // execute command
                    if ((monitor as IUsbMonitorCommands)?.UsbChangedCommand?.CanExecute(changedArgs) ?? false)
                    {
                        (monitor as IUsbMonitorCommands)?.UsbChangedCommand?.Execute(changedArgs);
                    }
                    // call virtual method
                    (monitor as IUsbMonitorOverrides)?.OnUsbChanged(changedArgs);
                    break;
                }
            }
        }