/// <summary> /// Registers a window to receive windows messages when a device is inserted/removed. Need to call this /// from a form when its handle has been created, not in the form constructor. Use form's OnHandleCreated override. /// </summary> /// <param name="hWnd">Handle to window that will receive messages</param> /// <param name="gClass">Class of devices to get messages for</param> /// <returns>A handle used when unregistering</returns> public static IntPtr RegisterForUsbEvents(IntPtr hWnd, Guid gClass) { var oInterfaceIn = new DeviceBroadcastInterface(); oInterfaceIn.Size = Marshal.SizeOf(oInterfaceIn); oInterfaceIn.ClassGuid = gClass; oInterfaceIn.DeviceType = DEVTYP_DEVICEINTERFACE; oInterfaceIn.Reserved = 0; return RegisterDeviceNotification(hWnd, oInterfaceIn, DEVICE_NOTIFY_WINDOW_HANDLE); }
public static extern IntPtr RegisterDeviceNotification(IntPtr hwnd, DeviceBroadcastInterface oInterface, uint nFlags);