Exemplo n.º 1
0
        public bool Start(IntPtr windowHandle)
        {
            Log.PrintLine(TAG, LogLevel.Information, $"Start(windowHandle={windowHandle})");
            if (notificationHandle != IntPtr.Zero)
            {
                return(false);
            }

            DEV_BROADCAST_DEVICEINTERFACE dbi = new DEV_BROADCAST_DEVICEINTERFACE
            {
                dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE,
                dbcc_classguid  = GUID_DEVINTERFACE_USB_DEVICE,
            };

            dbi.dbcc_size = Marshal.SizeOf(dbi);
            IntPtr buffer = Marshal.AllocHGlobal(dbi.dbcc_size);

            Marshal.StructureToPtr(dbi, buffer, true);

            notificationHandle = RegisterDeviceNotification(windowHandle, buffer, 0);

            Marshal.FreeHGlobal(buffer);

            return(true);
        }
Exemplo n.º 2
0
        unsafe public UsbAlarm(IntPtr windowHandle)
        {
            DEV_BROADCAST_DEVICEINTERFACE filter = new DEV_BROADCAST_DEVICEINTERFACE();

            filter.dbcc_size       = DEV_BROADCAST_DEVICEINTERFACE.Size;
            filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;

            foreach (Guid guid in GUID_DEVINTERFACE_LIST)
            {
                filter.dbcc_classguid = guid;

                DEV_BROADCAST_DEVICEINTERFACE *ptr = &filter;
                IntPtr ptrStruct  = new IntPtr(ptr);
                IntPtr hDevNotify = RegisterDeviceNotification(windowHandle, ptrStruct, DEVICE_NOTIFY_WINDOW_HANDLE);
                if (hDevNotify == IntPtr.Zero)
                {
                    Debug.WriteLine("Failed to register: " + guid);
                }
                else
                {
                    _notifications.Add(hDevNotify);
                }
            }
        }
Exemplo n.º 3
0
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            //Log.PrintLine(TAG, LogLevel.Information, $"WndProc m={m}");
            switch (m.Msg)
            {
            case WM_DEVICECHANGE:
            {
                var wParam = m.WParam.ToInt32();
                //Log.PrintLine(TAG, LogLevel.Information, $"WndProc WM_DEVICECHANGE wParam=0x{wParam:X8}");

                /*
                 * var serialPorts = SerialPort.GetPortNames().OrderBy(name => name);
                 * foreach (var serialPort in serialPorts)
                 * {
                 *  Log.PrintLine(TAG, LogLevel.Information, $"WndProc WM_DEVICECHANGE serialPort={serialPort}");
                 * }
                 */

                switch (wParam)
                {
                /*
                 * case DBT_DEVNODES_CHANGED:
                 *  {
                 *      Log.PrintLine(TAG, LogLevel.Information, "WndProc DBT_DEVNODES_CHANGED");
                 *      break;
                 *  }
                 */
                case DBT_DEVICEARRIVAL:
                case DBT_DEVICEREMOVECOMPLETE:
                {
                    ChangeType changeType;
                    switch (wParam)
                    {
                    case DBT_DEVICEARRIVAL:
                        changeType = ChangeType.Add;
                        break;

                    case DBT_DEVICEREMOVECOMPLETE:
                        changeType = ChangeType.Remove;
                        break;

                    default:
                        return;
                    }
                    var lParam             = m.LParam;
                    DEV_BROADCAST_HDR pHdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(lParam, typeof(DEV_BROADCAST_HDR));
                    var deviceType         = pHdr.dbch_DeviceType;
                    Log.PrintLine(TAG, LogLevel.Information, $"WndProc {changeType} deviceType={deviceType}");
                    switch (deviceType)
                    {
                    case DBT_DEVTYP_DEVICEINTERFACE:
                    {
                        //Log.PrintLine(TAG, LogLevel.Information, $"WndProc {changeType} devType=DBT_DEVTYP_DEVICEINTERFACE");
                        DEV_BROADCAST_DEVICEINTERFACE pDevice = (DEV_BROADCAST_DEVICEINTERFACE)Marshal.PtrToStructure(lParam, typeof(DEV_BROADCAST_DEVICEINTERFACE));
                        var deviceName = pDevice.dbcc_name;
                        Log.PrintLine(TAG, LogLevel.Information, $"WndProc {changeType} pDevice.dbcc_name={Utils.Quote(deviceName)}");

                        /*
                         * deviceName = CleanUpDeviceInterfaceName(deviceName);
                         * if (DeviceNamesSubscribed.Contains(deviceName))
                         * {
                         *  OnUsbChange?.Invoke(this, new UsbChangeEventArgs(changeType, deviceName));
                         * }
                         */
                        break;
                    }

                    case DBT_DEVTYP_PORT:
                    {
                        //Log.PrintLine(TAG, LogLevel.Information, $"WndProc {changeType} devType=DBT_DEVTYP_PORT");
                        DEV_BROADCAST_PORT pPort = (DEV_BROADCAST_PORT)Marshal.PtrToStructure(lParam, typeof(DEV_BROADCAST_PORT));
                        var portName             = pPort.dbcp_name;
                        Log.PrintLine(TAG, LogLevel.Information, $"WndProc {changeType} pPort.dbcp_name={Utils.Quote(portName)}");

                        /*
                         * if (SerialPortsSubscribed.Contains(portName))
                         * {
                         *  OnSerialPortChange?.Invoke(this, new SerialPortChangeEventArgs(changeType, portName));
                         * }
                         */
                        break;
                    }
                    }
                    break;
                }
                }
                break;
            }
            }
        }