Ejemplo n.º 1
0
        private IntPtr HwndHandler(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
        {
            // Only checking changed event since it gets called when devices are added and removed
            // while remove notifications don't always get called.
            if (msg == WmDevicechange)
            {
                if ((int)wparam == DbtDeviceArrival || (int)wparam == DbtDeviceRemoveComplete)
                {
                    if (lparam != IntPtr.Zero)
                    {
                        //Get Device name
                        DevBroadcastDeviceInterface deviceInterface = (DevBroadcastDeviceInterface)Marshal.PtrToStructure(lparam, typeof(DevBroadcastDeviceInterface));
                        string deviceName = "";
                        deviceName = System.Text.Encoding.Unicode.GetString(deviceInterface.Name); //convert byte array into unicode
                        deviceName = deviceName.Replace("\0", string.Empty);                       //removes all unicode null "\0" charecters

                        if (deviceName.Contains("057e"))                                           // nintendo VID = 057e
                        {
                            OnDevicesUpdated?.Invoke();
                        }
                    }
                }
            }

            handled = false;
            return(IntPtr.Zero);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Register windows for notification
        /// </summary>
        /// <param name="windowHandle">Windows handle of the main window</param>
        public void Register(IntPtr windowHandle)
        {
#if WINDOWS_UWP
            int size = Marshal.SizeOf <DevBroadcastDeviceInterface>();
#else
            int size = Marshal.SizeOf(typeof(DevBroadcastDeviceInterface));
#endif
            var deviceInterface = new DevBroadcastDeviceInterface();
            deviceInterface.Size       = (uint)size;
            deviceInterface.DeviceType = (uint)UsbDeviceType.DeviceInterface; // DBT_DEVTYP_DEVICEINTERFACE;
            deviceInterface.Reserved   = 0;
            deviceInterface.ClassGuid  = InterfaceClassGuid;

            IntPtr buffer = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(deviceInterface, buffer, true);

            try
            {
                this.deviceEventHandle = NativeMethods.RegisterDeviceNotification(windowHandle, buffer, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
            }
            catch (Exception)
            {
            }


            if (this.deviceEventHandle == IntPtr.Zero)
            {
                int error = Marshal.GetLastWin32Error();
            }
            Marshal.FreeHGlobal(buffer);
        }
 /// <summary>
 /// Get the DevBroadcastDeviceInterface
 /// </summary>
 /// <param name="devBroadcastDeviceInterface">out DevBroadcastDeviceInterface</param>
 /// <returns>bool true the value could be converted</returns>
 public bool TryGetDevBroadcastDeviceInterface(out DevBroadcastDeviceInterface devBroadcastDeviceInterface)
 {
     if (_devBroadcastHeader.DeviceType != DeviceBroadcastDeviceType.DeviceInterface)
     {
         devBroadcastDeviceInterface = default;
         return(false);
     }
     devBroadcastDeviceInterface = Marshal.PtrToStructure <DevBroadcastDeviceInterface>(_deviceBroadcastPtr);
     return(true);
 }
Ejemplo n.º 4
0
        public void TestParse()
        {
            const string iPhoneDeviceName            = @"\?\USB#VID_05AC&PID_1294&MI_00#0#{6bdd1fc6-810f-11d0-bec7-08002be2092f}";
            var          devBroadcastDeviceInterface = DevBroadcastDeviceInterface.Test(iPhoneDeviceName, DeviceInterfaceClass.StillImage);

            Assert.Equal("1294", devBroadcastDeviceInterface.ProductId);
            Assert.Equal("05AC", devBroadcastDeviceInterface.VendorId);
            Assert.Equal("6bdd1fc6-810f-11d0-bec7-08002be2092f", devBroadcastDeviceInterface.DeviceClassGuid.ToString(), StringComparer.OrdinalIgnoreCase);
            Assert.True(devBroadcastDeviceInterface.IsUsb);
            Log.Info().WriteLine("More information: {0}", devBroadcastDeviceInterface.UsbDeviceInfoUri);
        }
Ejemplo n.º 5
0
        public void TestParse_2()
        {
            const string graphicsCard =
                @"\?\PCI#VEN_10DE&DEV_1FB8&SUBSYS_09061028&REV_A1#4&32af3f68&0&0008#{1ca05180-a699-450a-9a0c-de4fbe3ddd89}";
            var devBroadcastDeviceInterface = DevBroadcastDeviceInterface.Test(graphicsCard, DeviceInterfaceClass.DisplayDeviceArrival);

            Assert.Equal("10DE", devBroadcastDeviceInterface.VendorId);
            Assert.Equal("1ca05180-a699-450a-9a0c-de4fbe3ddd89", devBroadcastDeviceInterface.DeviceClassGuid.ToString(), StringComparer.OrdinalIgnoreCase);
            Assert.Equal(@"PCI\VEN_10DE&DEV_1FB8&SUBSYS_09061028&REV_A1\4&32af3f68&0&0008", devBroadcastDeviceInterface.DisplayName);
            Assert.True(devBroadcastDeviceInterface.IsPci);
            Log.Info().WriteLine("More information: {0}", devBroadcastDeviceInterface.UsbDeviceInfoUri);
        }
Ejemplo n.º 6
0
        internal override IntPtr WindowProc(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            if (msg == WM_DEVICECHANGE)
            {
                var theEvent = wParam.ToInt64();
                if (theEvent == DBT_DEVICEARRIVAL || theEvent == DBT_DEVICEREMOVECOMPLETE)
                {
                    var devBroadcastMsg = new DevBroadcastDeviceInterface();
                    Marshal.PtrToStructure(lParam, devBroadcastMsg);
                    if (devBroadcastMsg.DeviceType != 0x05)
                    {
                        return(base.WindowProc(hwnd, msg, wParam, lParam));
                    }
                    if (theEvent == DBT_DEVICEARRIVAL)
                    {
                        Task.Run(() =>
                        {
                            if (currentContext == null)
                            {
                                Service.add(devBroadcastMsg.DevicePath);
                            }
                            else
                            {
                                currentContext.Post(delegate { Service.add(devBroadcastMsg.DevicePath); }, null);
                            }
                        });
                    }
                    else
                    {
                        var board = Service.Boards
                                    .Where(x => x.Connection.DevicePath.Substring(3).ToLower() ==
                                           devBroadcastMsg.DevicePath.Substring(3).ToLower())
                                    .FirstOrDefault();
                        if (board != null)
                        {
                            Debug.WriteLine("Removing: " + board);
                            board.Connection.Dispose(); // kill the connection first
                            board.Dispose();
                            if (currentContext == null)
                            {
                                Service.Boards.Remove(board);
                            }
                            else
                            {
                                currentContext.Post(delegate { Service.Boards.Remove(board); }, null);
                            }
                        }
                    }
                }
            }

            return(base.WindowProc(hwnd, msg, wParam, lParam));
        }
Ejemplo n.º 7
0
        public static void RegisterForDeviceNotification(IntPtr window)
        {
            var filter = new DevBroadcastDeviceInterface
            {
                size       = (uint)Marshal.SizeOf(typeof(DevBroadcastDeviceInterface)),
                deviceType = DBT_DEVTYP_DEVICEINTERFACE,
            };
            var result = RegisterDeviceNotification(window, ref filter, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);

            if (result == IntPtr.Zero)
            {
                throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
            }
        }
        private static DevBroadcastDeviceInterface CreateBroadcastDeviceInterface(Guid classGuid)
        {
            var dbi = new DevBroadcastDeviceInterface
            {
                DeviceType = DbtDeviceTypeDeviceInterface,
                Reserved   = 0,
                ClassGuid  = classGuid,
                Name       = 0
            };

            dbi.Size = Marshal.SizeOf(dbi);

            return(dbi);
        }
        private static IntPtr RegisterDeviceNotification(DevBroadcastDeviceInterface dbi, IntPtr windowHandle)
        {
            var    buffer = Marshal.AllocHGlobal(dbi.Size);
            IntPtr handle;

            try
            {
                Marshal.StructureToPtr(dbi, buffer, true);

                handle = RegisterDeviceNotification(windowHandle, buffer, 0);
            }
            finally
            {
                // Free buffer
                Marshal.FreeHGlobal(buffer);
            }

            return(handle);
        }
Ejemplo n.º 10
0
        public void RegisterDeviceNotification(
            IntPtr windowHandle, Guid deviceInterfaceClass)
        {
            // Specifies which type of device to send notifications for. Here
            // DeviceType is 5 which means notifications will be sent for all devices
            // that are part of a specific device interface class (GUID).
            // See NotificationFilter parameter of RegisterDeviceNotification docs
            DevBroadcastDeviceInterface dbdi = new DevBroadcastDeviceInterface
            {
                DeviceType = 5,
                Reserved   = 0,
                ClassGuid  = deviceInterfaceClass,
                Name       = ""
            };

            dbdi.Size = Marshal.SizeOf(dbdi);
            IntPtr filter = Marshal.AllocHGlobal(dbdi.Size);

            Marshal.StructureToPtr(dbdi, filter, true);

            NotificationHandle = RegisterDeviceNotification(windowHandle, filter, 0);
        }
Ejemplo n.º 11
0
        public void RegisterDeviceNotification(Window window)
        {
            //get main window handle and hook its message events
            var    source       = HwndSource.FromHwnd(new WindowInteropHelper(window).Handle);
            IntPtr windowHandle = source.Handle;

            source.AddHook(HwndHandler);

            //create filter to find bluetooth WmDevicechange messages only
            DevBroadcastDeviceInterface deviceInterface = new DevBroadcastDeviceInterface
            {
                ClassGuid  = GuidInterfaceHID,
                DeviceType = DbtDevtypDeviceinterface,
            };

            deviceInterface.Size = Marshal.SizeOf(deviceInterface);
            IntPtr buffer = Marshal.AllocHGlobal(deviceInterface.Size);

            Marshal.StructureToPtr(deviceInterface, buffer, false);

            //send messages of these filtered events to main window
            notificationHandle = RegisterDeviceNotification(windowHandle, buffer, DEVICE_NOTIFY.WINDOWS_HANDLE);
        }
Ejemplo n.º 12
0
        public void RegisterDeviceNotification(Window window, Guid deviceClass, bool usbOnly = false)
        {
            var source = HwndSource.FromHwnd(new WindowInteropHelper(window).Handle);

            source.AddHook(HwndHandler);

            IntPtr windowHandle = source.Handle;

            var deviceInterface = new DevBroadcastDeviceInterface
            {
                DeviceType = DbtDevtypDeviceinterface,
                Reserved   = 0,
                ClassGuid  = deviceClass,
                Name       = 0
            };

            deviceInterface.Size = Marshal.SizeOf(deviceInterface);
            IntPtr buffer = Marshal.AllocHGlobal(deviceInterface.Size);

            Marshal.StructureToPtr(deviceInterface, buffer, true);

            notificationHandle = RegisterDeviceNotification(windowHandle, buffer, usbOnly ? 0 : DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
        }
Ejemplo n.º 13
0
 private static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, ref DevBroadcastDeviceInterface notificationFilter, uint flags);