private void CreateHelperWindow() { HelperWindowHandle = User32.CreateWindowEx( WindowExStyles.WS_EX_OVERLAPPEDWINDOW, CLASS_NAME, "Emotion Helper Window", WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CLIPCHILDREN, 0, 0, 100, 100, IntPtr.Zero, IntPtr.Zero, Kernel32.GetModuleHandle(null), IntPtr.Zero ); if (HelperWindowHandle == IntPtr.Zero) { CheckError("Win32: Failed to create helper window.", true); } // HACK: The command to the first ShowWindow call is ignored if the parent // process passed along a STARTUPINFO, so clear that with a no-op call User32.ShowWindow(HelperWindowHandle, ShowWindowCommands.SW_HIDE); // Register for HID device notifications var dbi = new DevBroadcastDeviceInterfaceW(); dbi.DbccSize = (uint)Marshal.SizeOf(dbi); dbi.DbccDeviceType = DeviceType.DeviceInterface; dbi.DbccClassGuid = User32Guids.GuidDevInterfaceHid; _deviceNotificationHandle = User32.RegisterDeviceNotificationW(HelperWindowHandle, ref dbi, DeviceNotificationFlags.WindowHandle); CheckError("Registering for device notifications."); while (User32.PeekMessage(out Message msg, HelperWindowHandle, 0, 0, PeekMessageFlags.PM_REMOVE)) { User32.TranslateMessage(ref msg); User32.DispatchMessage(ref msg); } Kernel32.SetLastError(0); CheckError("Creating helper window."); }
public static extern IntPtr RegisterDeviceNotificationW( IntPtr hRecipient, ref DevBroadcastDeviceInterfaceW notificationFilter, DeviceNotificationFlags flags );