private static IntPtr RegisterForDeviceNotifications(IntPtr parent) { IntPtr intPtr = IntPtr.Zero; BroadcastDeviceInterface broadcastDeviceInterface = default(BroadcastDeviceInterface); broadcastDeviceInterface.dbcc_size = Marshal.SizeOf(broadcastDeviceInterface); broadcastDeviceInterface.BroadcastDeviceType = BroadcastDeviceType.DBT_DEVTYP_DEVICEINTERFACE; broadcastDeviceInterface.dbcc_classguid = RawInput.AllUsbDeviceInterfaceHid; IntPtr intPtr2 = IntPtr.Zero; try { intPtr2 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(BroadcastDeviceInterface))); Marshal.StructureToPtr(broadcastDeviceInterface, intPtr2, false); intPtr = Win32.RegisterDeviceNotification(parent, intPtr2, DeviceNotification.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES); } catch (Exception ex) { DebugCenter.GetInstance().appendToFile(ex.Message + "\r\n" + ex.StackTrace); } finally { Marshal.FreeHGlobal(intPtr2); } if (intPtr == IntPtr.Zero) { DebugCenter.GetInstance().appendToFile("Registration for device notifications Failed. Error: " + Marshal.GetLastWin32Error().ToString()); } return(intPtr); }
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); }
private static IntPtr RegisterForDeviceNotifications(IntPtr parent) { IntPtr notifyHandle = IntPtr.Zero; BroadcastDeviceInterface broadcastDeviceInterface = default(BroadcastDeviceInterface); broadcastDeviceInterface.dbcc_size = Marshal.SizeOf(broadcastDeviceInterface); broadcastDeviceInterface.BroadcastDeviceType = BroadcastDeviceType.DBT_DEVTYP_DEVICEINTERFACE; broadcastDeviceInterface.dbcc_classguid = DeviceInterfaceHid; IntPtr interfacePtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(BroadcastDeviceInterface))); try { Marshal.StructureToPtr(broadcastDeviceInterface, interfacePtr, false); notifyHandle = Win32Methods.RegisterDeviceNotification(parent, interfacePtr, DeviceNotification.DEVICE_NOTIFY_WINDOW_HANDLE); } catch (Exception ex) { Debug.Print("Registration for device notifications Failed. Error: {0}", Marshal.GetLastWin32Error()); } finally { Marshal.FreeHGlobal(interfacePtr); } if (notifyHandle == IntPtr.Zero) { Debug.Print("Registration for device notifications Failed: Error: {0}", Marshal.GetLastWin32Error()); } return(notifyHandle); }
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; }