Exemplo n.º 1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="action"></param>
 /// <param name="handle"></param>
 /// <param name="devNotify"></param>
 /// <param name="eventGuid"></param>
 /// <param name="nameOffset"></param>
 /// <param name="data"></param>
 internal UsbEventHandleArgs(UsbDeviceChangeEvent action, IntPtr handle, IntPtr devNotify, Guid eventGuid, ulong nameOffset, byte[] data) : base(action)
 {
     this.Handle     = handle;
     this.DevNotify  = devNotify;
     this.EventGuid  = eventGuid;
     this.NameOffset = nameOffset;
     this.Data       = data;
 }
Exemplo n.º 2
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;
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="action"></param>
 /// <param name="name"></param>
 internal UsbEventPortArgs(UsbDeviceChangeEvent action, string name) : base(action)
 {
     this.Name = name;
 }
Exemplo n.º 4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="action"></param>
 /// <param name="unitmask"></param>
 /// <param name="flags"></param>
 /// <param name="drives"></param>
 internal UsbEventVolumeArgs(UsbDeviceChangeEvent action, uint unitmask, ushort flags, char[] drives) : base(action)
 {
     this.UnitMask = unitmask;
     this.Flags    = (UsbVolumeFlags)flags;
     this.Drives   = drives;
 }
Exemplo n.º 5
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="action"></param>
 /// <param name="identifier"></param>
 /// <param name="suppFunc"></param>
 internal UsbEventOemArgs(UsbDeviceChangeEvent action, uint identifier, uint suppFunc) : base(action)
 {
     this.Identifier = identifier;
     this.SuppFunc   = suppFunc;
 }
Exemplo n.º 6
0
 internal UsbEventArgs(UsbDeviceChangeEvent action)
 {
     this.Action = action;
 }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="action"></param>
 /// <param name="classGuid"></param>
 /// <param name="deviceInterface"></param>
 /// <param name="name"></param>
 internal UsbEventDeviceInterfaceArgs(UsbDeviceChangeEvent action, Guid classGuid, UsbDeviceInterface deviceInterface, string name) : base(action)
 {
     this.ClassGuid       = classGuid;
     this.DeviceInterface = deviceInterface;
     this.Name            = name;
 }