public static bool RegisterDeviceNotification(IntPtr formHandle, ref IntPtr notificationHandle) { Win32.DEV_BROADCAST_DEVICEINTERFACE dbi = new Win32.DEV_BROADCAST_DEVICEINTERFACE(); int size = 0; IntPtr Buffer = IntPtr.Zero; Guid HidGuid = new Guid(); Win32.HidD_GetHidGuid(ref HidGuid); size = Marshal.SizeOf(dbi); dbi.dbcc_size = size; dbi.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE; dbi.dbcc_reserved = 0; dbi.dbcc_classguid = HidGuid; dbi.dbcc_name = 0; Buffer = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(dbi, Buffer, true); notificationHandle = Win32.RegisterDeviceNotification(formHandle, Buffer, Win32.DEVICE_NOTIFY_WINDOW_HANDLE); if (notificationHandle.ToInt32() == IntPtr.Zero.ToInt32()) { return(false); } return(true); }
protected override void WndProc(ref Message m) { switch (m.Msg) { case MixerNative.MM_MIXM_LINE_CHANGE: _audio.HandleLineChanged(m.WParam, (uint)m.LParam); break; case MixerNative.MM_MIXM_CONTROL_CHANGE: _audio.HandleLineControlChanged(m.WParam, (uint)m.LParam); break; case Win32.WM_DEVICECHANGE: Win32.DEV_BROADCAST_HDR structDecription; switch ((int)m.WParam) { case Win32.DBT_DEVICEARRIVAL: structDecription = (Win32.DEV_BROADCAST_HDR)m.GetLParam(typeof(Win32.DEV_BROADCAST_HDR)); if (structDecription.dbcc_devicetype == Win32.DBT_DEVTYP_DEVICEINTERFACE) { Win32.DEV_BROADCAST_DEVICEINTERFACE devInterface = (Win32.DEV_BROADCAST_DEVICEINTERFACE)m.GetLParam(typeof(Win32.DEV_BROADCAST_DEVICEINTERFACE)); if (devInterface.dbcc_classguid == Win32.MEDIA_CLASS_GUID) { new EmptyDelegate(_audio.HandleDeviceAdding).BeginInvoke(null, null); } } break; case Win32.DBT_DEVICEREMOVECOMPLETE: structDecription = (Win32.DEV_BROADCAST_HDR)m.GetLParam(typeof(Win32.DEV_BROADCAST_HDR)); if (structDecription.dbcc_devicetype == Win32.DBT_DEVTYP_DEVICEINTERFACE) { Win32.DEV_BROADCAST_DEVICEINTERFACE devInterface = (Win32.DEV_BROADCAST_DEVICEINTERFACE)m.GetLParam(typeof(Win32.DEV_BROADCAST_DEVICEINTERFACE)); if (devInterface.dbcc_classguid == Win32.MEDIA_CLASS_GUID) { new EmptyDelegate(_audio.HandleDeviceRemoving).BeginInvoke(null, null); } } break; } break; } base.WndProc(ref m); }
/// <summary> /// Функция, которая забирает у библиотеки сервиса C# управление обработчиком сервиса и передаёт его нашей функции /// </summary> private void RegisterDeviceNotification() { _serviceCallback = new Win32.ServiceControlHandlerEx(ServiceControlHandler); Win32.RegisterServiceCtrlHandlerEx(ServiceName, _serviceCallback, IntPtr.Zero); Win32.DEV_BROADCAST_DEVICEINTERFACE deviceInterface = new Win32.DEV_BROADCAST_DEVICEINTERFACE(); int size = Marshal.SizeOf(deviceInterface); deviceInterface.dbcc_size = size; deviceInterface.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE; IntPtr buffer = default(IntPtr); buffer = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(deviceInterface, buffer, true); _deviceEventHandle = Win32.RegisterDeviceNotification(ServiceHandle, buffer, Win32.DEVICE_NOTIFY_SERVICE_HANDLE | Win32.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES); if (_deviceEventHandle == IntPtr.Zero) { // TODO handle error } }
private void RegisterDeviceNotification() { Win32.DEV_BROADCAST_DEVICEINTERFACE deviceInterface = new Win32.DEV_BROADCAST_DEVICEINTERFACE(); var size = Marshal.SizeOf(deviceInterface); deviceInterface.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE; deviceInterface.dbcc_reserved = 0; deviceInterface.dbcc_classguid = Win32.MEDIA_CLASS_GUID; deviceInterface.dbcc_size = size; IntPtr buffer = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(deviceInterface, buffer, true); _deviceEventHandle = Win32.RegisterDeviceNotification(Handle, buffer, Win32.DEVICE_NOTIFY_WINDOW_HANDLE); if (_deviceEventHandle == IntPtr.Zero) { throw new InvalidOperationException("Failed to register for notifications about devices changes"); } }
public static void RegisterHidNotification(IntPtr handle) { Win32.DEV_BROADCAST_DEVICEINTERFACE dbi = new Win32.DEV_BROADCAST_DEVICEINTERFACE(); int size = Marshal.SizeOf(dbi); dbi.dbcc_size = size; dbi.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE; dbi.dbcc_reserved = 0; dbi.dbcc_classguid = Win32.GUID_DEVINTERFACE_HID; dbi.dbcc_name = ""; IntPtr buffer = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(dbi, buffer, true); IntPtr r = Win32.RegisterDeviceNotification(handle, buffer, Win32.DEVICE_NOTIFY_WINDOW_HANDLE | Win32.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES); if (r == IntPtr.Zero) { } }
/// <summary> /// 注册加密锁事件插拨通知 /// </summary> void RegisterDeviceNotification() { Win32.DEV_BROADCAST_DEVICEINTERFACE dbi = new Win32.DEV_BROADCAST_DEVICEINTERFACE(); int size = Marshal.SizeOf(dbi); dbi.dbcc_size = size; dbi.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE; dbi.dbcc_reserved = 0; dbi.dbcc_classguid = Win32.GUID_DEVINTERFACE_USB_DEVICE; dbi.dbcc_name = 0; IntPtr buffer = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(dbi, buffer, true); WindowInteropHelper helper = new WindowInteropHelper(this); IntPtr ptr = helper.Handle; IntPtr r = Win32.RegisterDeviceNotification(ptr, buffer, Win32.DEVICE_NOTIFY_WINDOW_HANDLE); if (r == IntPtr.Zero) MessageBox.Show(Win32.GetLastError().ToString()); }