コード例 #1
0
        /// <summary>
        /// AssociatedObject_Loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
        {
            // WindProcを設定
            IntPtr     handle = new WindowInteropHelper(this.AssociatedObject).Handle;
            HwndSource source = HwndSource.FromHwnd(handle);

            source.AddHook(new HwndSourceHook(WndProc));

            // USBデバイスの抜き差しを取得する設定
            DevBroadcastDeviceinterface dbi = new DevBroadcastDeviceinterface
            {
                DeviceType = DbtDevtypDeviceinterface,
                Reserved   = 0,
                ClassGuid  = GuidDevinterfaceUSBDevice,
                Name       = 0
            };

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

            Marshal.StructureToPtr(dbi, buffer, true);

            // デバイス変更通知を受け取れるように登録
            RegisterDeviceNotification(handle, buffer, 0);

            // 対象のUSBのDescriptionを取得
            _DescriptionUSB = ConfigurationManager.AppSettings["TargetDescription"];

            // デバイス一覧取得
            var devList = MakeDeviceList();

            // 対象のUSBは接続されているか?
            DetectedUSB = devList.Any(x => x.Contains(_DescriptionUSB));
        }
コード例 #2
0
        /// <summary>
        /// Активирует получение уведомлений.
        /// </summary>
        public void Enable()
        {
            if (notificationHandle != IntPtr.Zero) // если уведомления уже активны, ничего не делаем
            {
                return;
            }
            if (this.Handle == IntPtr.Zero) // если окно не было создано, создаём
            {
                CreateHandle(new CreateParams());
            }
            // Структура, описывающая желаемую категорию уведомлений.
            DevBroadcastDeviceinterface dbi = new DevBroadcastDeviceinterface
            {
                DeviceType = DbtDevtypDeviceinterface,
                Reserved   = 0,
                ClassGuid  = subscriptionGuid,
                Name       = 0
            };

            // Преобразуем структуру в указатель для передачи в WINAPI функцию
            dbi.Size = Marshal.SizeOf(dbi);
            IntPtr buffer = Marshal.AllocHGlobal(dbi.Size);

            Marshal.StructureToPtr(dbi, buffer, true);
            // Вызываем WinAPI функцию для подписки на уведомления
            notificationHandle = RegisterDeviceNotification(this.Handle, buffer, 0);
        }
コード例 #3
0
        public UsbMonitor(IntPtr windowPtr)
        {
            var dbi = new DevBroadcastDeviceinterface
            {
                DeviceType = DbtDevtypDeviceinterface,
                Reserved   = 0,
                ClassGuid  = _guidDevinterfaceUsbDevice,
                Name       = 0
            };

            dbi.Size = Marshal.SizeOf(dbi);
            var buffer = Marshal.AllocHGlobal(dbi.Size);

            Marshal.StructureToPtr(dbi, buffer, true);
            var source = HwndSource.FromHwnd(windowPtr);

            if (source == null)
            {
                throw new ArgumentException("Window pointer did not yield valid HWND.");
            }
            var hwndHandle = source.Handle;

            source.AddHook(HwndHandler);
            _notificationHandle = RegisterDeviceNotification(hwndHandle, buffer, 0);
        }
コード例 #4
0
        ///  <summary>
        ///  Requests to receive a notification when a device is attached or removed.
        ///  </summary>
        ///
        ///  <param name="devicePathName"> handle to a device. </param>
        ///  <param name="formHandle"> handle to the window that will receive device events. </param>
        ///  <param name="classGuid"> device interface GUID. </param>
        ///  <param name="deviceNotificationHandle"> returned device notification handle. </param>
        ///
        ///  <returns>
        ///  True on success.
        ///  </returns>
        ///
        internal Boolean RegisterForDeviceNotifications(String devicePathName, IntPtr formHandle, Guid classGuid, ref IntPtr deviceNotificationHandle)
        {
            // A DEV_BROADCAST_DEVICEINTERFACE header holds information about the request.

            var devBroadcastDeviceInterface       = new DevBroadcastDeviceinterface();
            var devBroadcastDeviceInterfaceBuffer = IntPtr.Zero;

            try
            {
                // Set the parameters in the DEV_BROADCAST_DEVICEINTERFACE structure.

                // Set the size.
                var size = Marshal.SizeOf(devBroadcastDeviceInterface);
                devBroadcastDeviceInterface.dbcc_size = size;

                // Request to receive notifications about a class of devices.
                devBroadcastDeviceInterface.dbcc_devicetype = DbtDevtypDeviceinterface;
                devBroadcastDeviceInterface.dbcc_reserved   = 0;

                // Specify the interface class to receive notifications about.
                devBroadcastDeviceInterface.dbcc_classguid = classGuid;

                // Allocate memory for the buffer that holds the DEV_BROADCAST_DEVICEINTERFACE structure.
                devBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal(size);

                // Copy the DEV_BROADCAST_DEVICEINTERFACE structure to the buffer.
                // Set fDeleteOld True to prevent memory leaks.

                Marshal.StructureToPtr(devBroadcastDeviceInterface, devBroadcastDeviceInterfaceBuffer, true);

                // *** API function
                //  summary
                //      Request to receive notification messages when a device in an interface class
                //      is attached or removed.
                //  parameters
                //      Handle to the window that will receive device events.
                //      Pointer to a DEV_BROADCAST_DEVICEINTERFACE to specify the type of
                //      device to send notifications for.
                //      DEVICE_NOTIFY_WINDOW_HANDLE indicates the handle is a window handle.
                //  Returns
                //      Device notification handle or NULL on failure.
                deviceNotificationHandle = RegisterDeviceNotification(formHandle, devBroadcastDeviceInterfaceBuffer, DeviceNotifyAllInterfaceClasses);

                // Marshal data from the unmanaged block devBroadcastDeviceInterfaceBuffer to
                // the managed object devBroadcastDeviceInterface
                Marshal.PtrToStructure(devBroadcastDeviceInterfaceBuffer, devBroadcastDeviceInterface);

                return(deviceNotificationHandle.ToInt32() != IntPtr.Zero.ToInt32());
            }
            finally
            {
                if (devBroadcastDeviceInterfaceBuffer != IntPtr.Zero)
                {
                    // Free the memory allocated previously by AllocHGlobal.
                    Marshal.FreeHGlobal(devBroadcastDeviceInterfaceBuffer);
                }
            }
        }
コード例 #5
0
        public static void RegisterDeviceNotification(IntPtr windowHandle)
        {
            DevBroadcastDeviceinterface dbi = new DevBroadcastDeviceinterface();
            dbi.DeviceType = (int)DbchDeviceType.Deviceinterface;
            dbi.Reserved = 0;
            dbi.Size = Marshal.SizeOf(dbi);

            RegisterDeviceNotification(windowHandle, dbi,
                (uint)DeviceNotifyRecipient.WindowHandle | (uint)DeviceNotifyRecipient.WindowHandle);
        }
コード例 #6
0
        public static void RegisterUsbDeviceNotification()
        {
            UsbPortChanged(null);

#if USE_WMI
            try
            {
                _sports = ReadUsbPorts();

                var             qin   = new WqlEventQuery();
                var             qout  = new WqlEventQuery();
                ManagementScope scope = new ManagementScope("root\\CIMV2");
                scope.Options.EnablePrivileges = true;

                qin.EventClassName  = "__InstanceCreationEvent";
                qout.EventClassName = "__InstanceDeletionEvent";
                qout.WithinInterval = qin.WithinInterval = new TimeSpan(0, 0, 2);
                qout.Condition      = qin.Condition = @"TargetInstance ISA 'Win32_PnPEntity'";

                _arrival = new ManagementEventWatcher(qin);
                _removal = new ManagementEventWatcher(qout);

                _arrival.EventArrived += (ao, aargs) => RaisePortsChangedIfNecessary(true);
                _removal.EventArrived += (ro, rargs) => RaisePortsChangedIfNecessary(false);

                // Start listening for events
                _arrival.Start();
                _removal.Start();
            }
            catch (ManagementException err)
            {
                Log.e("WMI wather install error: {0}", err);
            }
#else
            if (_hookWin == null)
            {
                return;
            }

            DevBroadcastDeviceinterface dbi = new DevBroadcastDeviceinterface
            {
                DeviceType = DbtDevtypDeviceinterface,
                Reserved   = 0,
                ClassGuid  = GuidDevinterfaceUSSerialBDevice,
            };

            dbi.Size = Marshal.SizeOf(dbi);
            IntPtr buffer = Marshal.AllocHGlobal(dbi.Size);
            Marshal.StructureToPtr(dbi, buffer, true);

            _notifyHandle = RegisterDeviceNotification(new WindowInteropHelper(_hookWin).Handle, buffer, 0);

            Marshal.FreeHGlobal(buffer);
#endif
        }
コード例 #7
0
        /// <summary>
        /// Registers a window to receive notifications when USB devices are plugged or unplugged.
        /// </summary>
        /// <param name="windowHandle">Handle to the window receiving notifications.</param>
        public static void RegisterUsbDeviceNotification(IntPtr windowHandle)
        {
            var dbi = new DevBroadcastDeviceinterface {
            DeviceType = DbtDevtypDeviceinterface,
            Reserved = 0,
            ClassGuid = GuidDevinterfaceUSBDevice,
            Name = 0
              };

              dbi.Size = Marshal.SizeOf(dbi);
              var buffer = Marshal.AllocHGlobal(dbi.Size);
              Marshal.StructureToPtr(dbi, buffer, true);

              notificationHandle = RegisterDeviceNotification(windowHandle, buffer, 0);
        }
コード例 #8
0
            /// <summary>
            /// Registers a window to receive notifications when USB devices are plugged or unplugged.
            /// </summary>
            /// <param name="windowHandle">Handle to the window receiving notifications.</param>
            public static void RegisterUsbDeviceNotification(IntPtr windowHandle, Guid interfaz)
            {
                DevBroadcastDeviceinterface dbi = new DevBroadcastDeviceinterface
                {
                    DeviceType = DbtDevtypDeviceinterface,
                    Reserved   = 0,
                    ClassGuid  = interfaz,  // GuidDevinterfaceUSBDevice,
                    Name       = 0
                };

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

                Marshal.StructureToPtr(dbi, buffer, true);
                notificationHandle = RegisterDeviceNotification(windowHandle, buffer, 0);
            }
コード例 #9
0
    /// <summary>
    /// Registers a window to receive notifications when devices are plugged or unplugged.
    /// </summary>
    /// <param name="windowHandle">Handle to the window receiving notifications.</param>
    /// <param name="usbOnly">true to filter to USB devices only, false to be notified for all devices.</param>
    public static void RegisterDeviceNotification(IntPtr windowHandle, bool usbOnly = false)
    {
        var dbi = new DevBroadcastDeviceinterface {
            DeviceType = DbtDevtypDeviceinterface,
            Reserved   = 0,
            ClassGuid  = GuidDevinterfaceUSBDevice,
            Name       = 0
        };

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

        Marshal.StructureToPtr(dbi, buffer, true);

        notificationHandle = RegisterDeviceNotification(windowHandle, buffer, usbOnly ? 0 : DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
    }
コード例 #10
0
ファイル: UsbNotification.cs プロジェクト: zzattack/zektor
        /// <summary>
        ///     Registers a window to receive notifications when USB devices are plugged or unplugged.
        /// </summary>
        /// <param name="windowHandle">Handle to the window receiving notifications.</param>
        private static void RegisterUsbDeviceNotification(IntPtr windowHandle)
        {
            var dbi = new DevBroadcastDeviceinterface {
                DeviceType = DbtDevtypDeviceinterface,
                Reserved   = 0,
                ClassGuid  = GuidUsbDevices,
                Name       = 0
            };

            dbi.Size = Marshal.SizeOf(dbi);
            var buffer = Marshal.AllocHGlobal(dbi.Size);

            Marshal.StructureToPtr(dbi, buffer, true);

            _notificationHandle = RegisterDeviceNotification(windowHandle, buffer, 0);
        }
コード例 #11
0
        /// <summary>
        ///     Register a window handle to receive device change notifications
        /// </summary>
        /// <param name="hWnd">window handle</param>
        /// <returns>handle for unregistering</returns>
        public static IntPtr RegisterUsbDeviceNotification(IntPtr hWnd)
        {
            var dbi = new DevBroadcastDeviceinterface
            {
                DeviceType = DbtDevtypDeviceinterface,
                Reserved   = 0,
                ClassGuid  = GuidDevinterfaceUSBDevice,
                Name       = 0
            };

            dbi.Size = Marshal.SizeOf(dbi);
            var buffer = Marshal.AllocHGlobal(dbi.Size);

            Marshal.StructureToPtr(dbi, buffer, true);

            return(RegisterDeviceNotification(hWnd, buffer, 0));
        }
コード例 #12
0
    /// <summary>
    /// Registers a window to receive notifications when devices are plugged or unplugged.
    /// </summary>
    /// <param name="windowHandle">Handle to the window receiving notifications.</param>
    /// <param name="filterSpecificDeviceTypes">true to filter to USB devices only, false to be notified for all devices.</param>
    public static void RegisterDeviceNotification(IntPtr windowHandle, bool filterSpecificDeviceTypes = filterSpecificDeviceTypes)
    {
        var dbi = new DevBroadcastDeviceinterface
        {
            DeviceType = DbtDevtypDeviceinterface,
            Reserved   = 0,
            //    ClassGuid = GuidDevinterfaceUSBDevice,
            ClassGuid = GuidDevinterfaceMonitor,
            Name      = 0
        };

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

        Marshal.StructureToPtr(dbi, buffer, true);

        notificationHandle = RegisterDeviceNotification(windowHandle, buffer, filterSpecificDeviceTypes ? DEVICE_NOTIFY_WINDOW_HANDLE : DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
    }
コード例 #13
0
ファイル: MainForm.cs プロジェクト: FSpark/UDiskSpark
        /// <summary>
        /// Registers a window to receive notifications when USB devices are plugged or unplugged.
        /// </summary>
        /// <param name="windowHandle">Handle to the window receiving notifications.</param>
        public void RegisterUsbDeviceNotification(IntPtr windowHandle)
        {
            DevBroadcastDeviceinterface dbi = new DevBroadcastDeviceinterface
            {
                DeviceType = Win32.DbtDevtypDeviceinterface,
                Reserved   = 0,
                ClassGuid  = Win32.GUID_IO_MEDIA_ARRIVAL,
                Name       = 0
            };

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

            Marshal.StructureToPtr(dbi, buffer, true);

            deviceNotifyHandle = Win32.RegisterDeviceNotification(windowHandle, buffer, 0);
            MessageBox.Show(deviceNotifyHandle.ToString());
        }
コード例 #14
0
        /// <summary>
        /// Registers a window to receive notifications when USB devices are plugged or unplugged.
        /// </summary>
        /// <param name="windowHandle">Handle to the window receiving notifications.</param>
        private static void RegisterUsbDeviceNotification(IntPtr windowHandle)
        {
            var dbi = new DevBroadcastDeviceinterface {
                DeviceType = DeviceType.DBT_DEVTYP_DEVICEINTERFACE,
                Reserved   = 0,
                Name       = 0,
                ClassGuid  = GUID_DEVINTERFACE_HID,                // not used when flags contains ALL_INTERFACE_CLASSES
            };

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

            Marshal.StructureToPtr(dbi, buffer, true);

            RegisterDeviceNotificationFlags flags =
                RegisterDeviceNotificationFlags.DEVICE_NOTIFY_WINDOW_HANDLE;

            _notificationHandle = RegisterDeviceNotification(windowHandle, buffer, flags);
        }
コード例 #15
0
    public static DevBroadcastDeviceinterface GetDevBroadcastDeviceinterface(Message m)
    {
        DevBroadcastHdr hdr = GetDevBroadcastHdr(m);

        if (hdr.DeviceType == DBT_DEVTYP_DEVICEINTERFACE)
        {
            DevBroadcastDeviceinterface0 dbi0 = (DevBroadcastDeviceinterface0)m.GetLParam(typeof(DevBroadcastDeviceinterface0));
            DevBroadcastDeviceinterface  dbi  = new DevBroadcastDeviceinterface()
            {
                DeviceType = dbi0.DeviceType, ClassGuid = dbi0.ClassGuid
            };
            Byte[] bytes = new Byte[hdr.Size];
            Marshal.Copy((IntPtr)m.LParam, bytes, 0, hdr.Size);
            int offset = Marshal.SizeOf(dbi0) - 2;
            dbi.Name = System.Text.Encoding.Unicode.GetString(bytes, offset, bytes.Length - offset);
            return(dbi);
        }
        return(new DevBroadcastDeviceinterface());
    }
コード例 #16
0
ファイル: MainWindow.xaml.cs プロジェクト: manyanxp/WPF
        /// <summary>
        /// USBデバイスのプラグイン/プラグアウトをWindowに通知されるよう登録
        /// </summary>
        /// <param name="windowHandle">受信するWindowのハンドル</param>
        public static bool RegisterUsbDeviceNotification(IntPtr windowHandle)
        {
            var dbi = new DevBroadcastDeviceinterface
            {
                DeviceType = DbtDevtypDeviceinterface,
                Reserved   = 0,
                ClassGuid  = GuidDevinterfaceUSBDevice,
                Name       = 0
            };

            // マーシャリングで必要なサイズを算出及びヒープ用意
            dbi.Size = Marshal.SizeOf(dbi);
            var buffer = Marshal.AllocHGlobal(dbi.Size);

            Marshal.StructureToPtr(dbi, buffer, true);

            notificationHandle = RegisterDeviceNotification(windowHandle, buffer, 0);

            // この時点でバッファは解放
            Marshal.FreeHGlobal(buffer);

            return(notificationHandle != null);
        }
コード例 #17
0
        ///  <summary>
        ///  Requests to receive a notification when a device is attached or removed.
        ///  </summary>
        ///  
        ///  <param name="devicePathName"> handle to a device. </param>
        ///  <param name="formHandle"> handle to the window that will receive device events. </param>
        ///  <param name="classGuid"> device interface GUID. </param>
        ///  <param name="deviceNotificationHandle"> returned device notification handle. </param>
        ///  
        ///  <returns>
        ///  True on success.
        ///  </returns>
        ///  
        internal Boolean RegisterForDeviceNotifications(String devicePathName, IntPtr formHandle, Guid classGuid, ref IntPtr deviceNotificationHandle)
        {
            // A DEV_BROADCAST_DEVICEINTERFACE header holds information about the request.

            var devBroadcastDeviceInterface = new DevBroadcastDeviceinterface();
            var devBroadcastDeviceInterfaceBuffer = IntPtr.Zero;

            try
            {
                // Set the parameters in the DEV_BROADCAST_DEVICEINTERFACE structure.

                // Set the size.
                var size = Marshal.SizeOf(devBroadcastDeviceInterface);
                devBroadcastDeviceInterface.dbcc_size = size;

                // Request to receive notifications about a class of devices.
                devBroadcastDeviceInterface.dbcc_devicetype = DbtDevtypDeviceinterface;
                devBroadcastDeviceInterface.dbcc_reserved = 0;

                // Specify the interface class to receive notifications about.
                devBroadcastDeviceInterface.dbcc_classguid = classGuid;

                // Allocate memory for the buffer that holds the DEV_BROADCAST_DEVICEINTERFACE structure.
                devBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal(size);

                // Copy the DEV_BROADCAST_DEVICEINTERFACE structure to the buffer.
                // Set fDeleteOld True to prevent memory leaks.

                Marshal.StructureToPtr(devBroadcastDeviceInterface, devBroadcastDeviceInterfaceBuffer, true);

                // *** API function
                //  summary
                //      Request to receive notification messages when a device in an interface class
                //      is attached or removed.
                //  parameters
                //      Handle to the window that will receive device events.
                //      Pointer to a DEV_BROADCAST_DEVICEINTERFACE to specify the type of
                //      device to send notifications for.
                //      DEVICE_NOTIFY_WINDOW_HANDLE indicates the handle is a window handle.
                //  Returns
                //      Device notification handle or NULL on failure.
                deviceNotificationHandle = RegisterDeviceNotification(formHandle, devBroadcastDeviceInterfaceBuffer, DeviceNotifyAllInterfaceClasses);

                // Marshal data from the unmanaged block devBroadcastDeviceInterfaceBuffer to
                // the managed object devBroadcastDeviceInterface
                Marshal.PtrToStructure(devBroadcastDeviceInterfaceBuffer, devBroadcastDeviceInterface);

                return (deviceNotificationHandle.ToInt32() != IntPtr.Zero.ToInt32());
            }
            finally
            {
                if (devBroadcastDeviceInterfaceBuffer != IntPtr.Zero)
                {
                    // Free the memory allocated previously by AllocHGlobal.
                    Marshal.FreeHGlobal(devBroadcastDeviceInterfaceBuffer);
                }
            }
        }
コード例 #18
0
        /// <summary>
        /// registerForDeviceNotification - registers the window (identified by the windowHandle) for
        /// device notification messages from Windows
        /// </summary>
        public bool RegisterForDeviceNotifications(IntPtr windowHandle1)
        {
            Debug.WriteLine("usbGenericHidCommunication:registerForDeviceNotifications() -> Method called");

            // A DEV_BROADCAST_DEVICEINTERFACE header holds information about the request.
            var devBroadcastDeviceInterface       = new DevBroadcastDeviceinterface();
            var devBroadcastDeviceInterfaceBuffer = IntPtr.Zero;

            // Get the required GUID
            var systemHidGuid = new Guid();

            Hid.HidD_GetHidGuid(ref systemHidGuid);

            try
            {
                // Set the parameters in the DEV_BROADCAST_DEVICEINTERFACE structure.
                var size = Marshal.SizeOf(devBroadcastDeviceInterface);
                devBroadcastDeviceInterface.dbcc_size       = size;
                devBroadcastDeviceInterface.dbcc_devicetype = Constants.DbtDevtypDeviceinterface;
                devBroadcastDeviceInterface.dbcc_reserved   = 0;
                devBroadcastDeviceInterface.dbcc_classguid  = systemHidGuid;

                devBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(devBroadcastDeviceInterface, devBroadcastDeviceInterfaceBuffer, true);
                // Register for notifications and store the returned handle
                DeviceNotificationHandle = User32.RegisterDeviceNotification(Handle, devBroadcastDeviceInterfaceBuffer, Constants.DeviceNotifyWindowHandle);

                Marshal.PtrToStructure(devBroadcastDeviceInterfaceBuffer, devBroadcastDeviceInterface);

                if ((DeviceNotificationHandle.ToInt32() == IntPtr.Zero.ToInt32()))
                {
                    Debug.WriteLine(
                        "usbGenericHidCommunication:registerForDeviceNotifications() -> Notification registration failed");
                    return(false);
                }
                else
                {
                    Debug.WriteLine(
                        "usbGenericHidCommunication:registerForDeviceNotifications() -> Notification registration succeded");
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(
                    "usbGenericHidCommunication:registerForDeviceNotifications() -> EXCEPTION: An unknown exception has occured!");
                Debug.WriteLine(ex.Message);
            }
            finally
            {
                // Free the memory allocated previously by AllocHGlobal.
                if (devBroadcastDeviceInterfaceBuffer != IntPtr.Zero)
                {
                    try
                    {
                        Marshal.FreeHGlobal(devBroadcastDeviceInterfaceBuffer);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
                }
            }

            return(false);
        }
コード例 #19
0
ファイル: WinAPI.cs プロジェクト: hoppersoft/winauth
		/// <summary>
		/// Register a window handle to receive device change notifications
		/// </summary>
		/// <param name="hWnd">window handle</param>
		/// <returns>handle for unregistering</returns>
		public static IntPtr RegisterUsbDeviceNotification(IntPtr hWnd)
		{
			DevBroadcastDeviceinterface dbi = new DevBroadcastDeviceinterface
			{
				DeviceType = DbtDevtypDeviceinterface,
				Reserved = 0,
				ClassGuid = GuidDevinterfaceUSBDevice,
				Name = 0
			};

			dbi.Size = Marshal.SizeOf(dbi);
			IntPtr buffer = Marshal.AllocHGlobal(dbi.Size);
			Marshal.StructureToPtr(dbi, buffer, true);

			return RegisterDeviceNotification(hWnd, buffer, 0);
		}
コード例 #20
0
 private static extern IntPtr RegisterDeviceNotification(IntPtr recipient, DevBroadcastDeviceinterface notificationFilter, UInt32 flags);