static IntPtr RegisterForDeviceNotifications(IntPtr parent) { var usbNotifyHandle = IntPtr.Zero; var bdi = new BroadcastDeviceInterface(); bdi.DbccSize = Marshal.SizeOf(bdi); bdi.BroadcastDeviceType = BroadcastDeviceType.DBT_DEVTYP_DEVICEINTERFACE; bdi.DbccClassguid = DeviceInterfaceHid; var mem = IntPtr.Zero; try { mem = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(BroadcastDeviceInterface))); Marshal.StructureToPtr(bdi, mem, false); usbNotifyHandle = Win32.RegisterDeviceNotification(parent, mem, DeviceNotification.DEVICE_NOTIFY_WINDOW_HANDLE); } catch (Exception e) { Debug.Print("Registration for device notifications Failed. Error: {0}", Marshal.GetLastWin32Error()); Debug.Print(e.StackTrace); } finally { Marshal.FreeHGlobal(mem); } if (usbNotifyHandle == IntPtr.Zero) { Debug.Print("Registration for device notifications Failed. Error: {0}", Marshal.GetLastWin32Error()); } return(usbNotifyHandle); }
static IntPtr RegisterForDeviceNotifications(IntPtr parent) { var usbNotifyHandle = IntPtr.Zero; var bdi = new BroadcastDeviceInterface(); bdi.dbcc_size = Marshal.SizeOf(bdi); bdi.BroadcastDeviceType = BroadcastDeviceType.DBT_DEVTYP_DEVICEINTERFACE; bdi.dbcc_classguid = DeviceInterfaceHid; var mem = IntPtr.Zero; try { mem = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(BroadcastDeviceInterface))); Marshal.StructureToPtr(bdi, mem, false); usbNotifyHandle = Win32.RegisterDeviceNotification(parent, mem, DeviceNotification.DEVICE_NOTIFY_WINDOW_HANDLE); } catch (Exception e) { Debug.Print("Registration for device notifications Failed. Error: {0}", Marshal.GetLastWin32Error()); Debug.Print(e.StackTrace); } finally { Marshal.FreeHGlobal(mem); } if (usbNotifyHandle == IntPtr.Zero) { Debug.Print("Registration for device notifications Failed. Error: {0}", Marshal.GetLastWin32Error()); } return usbNotifyHandle; }