private static void Main() { KHOT_PARAMS hotInitParams = new KHOT_PARAMS(); // In the real world, you would want to filter for only *your* device(s). hotInitParams.PatternMatch.DeviceInterfaceGUID = "*"; // The PLUG_ALL_ON_INIT flag will force plug events for matching devices that are already connected. hotInitParams.Flags = KHOT_FLAG.PLUG_ALL_ON_INIT; hotInitParams.OnHotPlug = OnHotPlug; /* Instead of a callback you can send/post messages directly to a window handle. hotInitParams.UserHwnd = MyForm.Handle; hotInitParams.UserMessage = WM_USER_HOT_BASE; */ Console.WriteLine("Monitoring libusbK arrival/removal events."); Console.WriteLine("[PatternMatch]"); Console.WriteLine(hotInitParams.PatternMatch.ToString()); Console.WriteLine("Press [ENTER] to exit.."); while (Console.KeyAvailable) Console.ReadKey(true); // You may set your initial hot handle user context like this. // This example is using it to count connected devices and detect the first OnHotPlug event (Int32.MaxValue). AllKFunctions.LibK_SetDefaultContext(KLIB_HANDLE_TYPE.HOTK, new IntPtr(Int32.MaxValue)); // Start hot-plug detection. HotK hot = new HotK(ref hotInitParams); Console.ReadLine(); hot.Free(); }
/// <Summary>Creates a new hot-plug handle for USB device arrival/removal event monitoring.</Summary> public HotK(ref KHOT_PARAMS InitParams) { bool success = AllKFunctions.HotK_Init(out mHandleStruct, ref InitParams); if (!success) throw new Exception(string.Format("{0} failed initializing. ErrorCode={1:X8}h", GetType().Name, Marshal.GetLastWin32Error())); Debug.Print("{0} Init: handle 0x{1:X16}", GetType().Name, mHandleStruct.Pointer.ToInt64()); }
/// <Summary>Creates a new hot-plug handle for USB device arrival/removal event monitoring.</Summary> public HotK(ref KHOT_PARAMS InitParams) { RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { bool success = Functions.HotK_Init(out handle, ref InitParams); if (!success || handle.IsInvalid || handle.IsClosed) { handle.SetHandleAsInvalid(); int errorCode = Marshal.GetLastWin32Error(); throw new Exception(GetType().Name + " failed. ErrorCode=" + errorCode.ToString("X")); } } }