Esempio n. 1
0
        public bool RegisterForDeviceChange(bool Register, IntPtr WindowsHandle)
        {
            var flag = false;

            if (Register)
            {
                var structure = new USBClass.Win32Wrapper.DEV_BROADCAST_DEVICEINTERFACE();
                var cb        = Marshal.SizeOf <USBClass.Win32Wrapper.DEV_BROADCAST_DEVICEINTERFACE>(structure);
                structure.dbcc_size       = cb;
                structure.dbcc_devicetype = 5;
                var    num1 = new IntPtr();
                IntPtr num2 = Marshal.AllocHGlobal(cb);
                Marshal.StructureToPtr <USBClass.Win32Wrapper.DEV_BROADCAST_DEVICEINTERFACE>(structure, num2, true);
                deviceEventHandle = Win32Wrapper.RegisterDeviceNotification(WindowsHandle, num2, 4);
                flag = deviceEventHandle != IntPtr.Zero;
                if (!flag)
                {
                    Marshal.GetLastWin32Error();
                }

                Marshal.FreeHGlobal(num2);
            }
            else
            {
                if (deviceEventHandle != IntPtr.Zero)
                {
                    flag = Win32Wrapper.UnregisterDeviceNotification(deviceEventHandle);
                }

                deviceEventHandle = IntPtr.Zero;
            }
            return(flag);
        }
Esempio n. 2
0
        /// <summary>
        ///     Registers to be notified when devices are added or removed.
        /// </summary>
        /// <param name="register">True to register, False to unregister</param>
        /// <param name="f">Form</param>
        /// <returns>True if successfull, False otherwise</returns>
        public bool RegisterForDeviceChange(bool doRegister, Form f)
        {
            var  Status = false;
            long LastError;

            if (doRegister)
            {
                var deviceInterface = new Win32Wrapper.DEV_BROADCAST_DEVICEINTERFACE();
                int size            = Marshal.SizeOf(deviceInterface);
                deviceInterface.dbcc_size       = size;
                deviceInterface.dbcc_devicetype = (int)Win32Wrapper.DBTDEVTYP.DBT_DEVTYP_DEVICEINTERFACE;
                var buffer = default(IntPtr);
                buffer = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(deviceInterface, buffer, true);
                deviceEventHandle = Win32Wrapper.RegisterDeviceNotification(f.Handle, buffer,
                                                                            (int)(Win32Wrapper.DEVICE_NOTIFY.DEVICE_NOTIFY_WINDOW_HANDLE | Win32Wrapper.DEVICE_NOTIFY.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES));
                Status = deviceEventHandle != IntPtr.Zero;
                if (!Status)
                {
                    LastError = Marshal.GetLastWin32Error();
                }
                Marshal.FreeHGlobal(buffer);
            }
            else
            {
                if (deviceEventHandle != IntPtr.Zero)
                {
                    Status = Win32Wrapper.UnregisterDeviceNotification(deviceEventHandle);
                }
                deviceEventHandle = IntPtr.Zero;
            }

            return(Status);
        }