My main goal is to implement the correct message loop using P / Invoke calls capable of handling USB HID events. Definitely its functionality should be identical to the following code, which works well in Windows Forms . This descendant of NativeWindow receives events:
public class Win32EventHandler : NativeWindow { public const int WM_DEVICECHANGE = 0x0219; public Win32EventHandler() { this.CreateHandle(new CreateParams()); } protected override void OnHandleChange() { base.OnHandleChange(); IntPtr handle = UsbHelper.RegisterForUsbEvents(this.Handle); } protected override void WndProc(ref Message m) { if (m.Msg == WM_DEVICECHANGE) {
... powered by this event loop:
Win32EventHandler handler = new Win32EventHandler(); var context = new ApplicationContext(); Application.Run(context);
I found out that the implementation of the event loop is quite simple:
while (true) { res = Win32.GetMessage(out msg, IntPtr.Zero, 0, 0); if (res == 0) { break; } Win32.TranslateMessage(ref msg); Win32.DispatchMessage(ref msg); if (msg.message == WM_DEVICECHANGE) {
But I have no idea how to create a basic Window object. Implementing the NativeWindow class seems too complicated for me.
This is my solution at the moment:
public void CustomLoop() { string clsName = "Class"; string wndName = "Window"; Win32.WNDCLASSEX wndClassEx = new Win32.WNDCLASSEX(); wndClassEx.cbSize = (uint)Marshal.SizeOf(wndClassEx); wndClassEx.lpszClassName = clsName; wndClassEx.lpfnWndProc = WndProc; Win32.RegisterClassEx(ref wndClassEx); IntPtr windowHandle = Win32.CreateWindowEx(0, clsName, wndName, 0, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); IntPtr usbEventHandle = UsbHelper.RegisterForUsbEvents(windowHandle); Win32.MSG msg; sbyte res = 0; while (true) { res = Win32.GetMessage(out msg, IntPtr.Zero, 0, 0); if (res == 0) { break; } if (msg.message == WM.DEVICECHANGE) {