Пример #1
0
        private static void OnHotPlug(KHOT_HANDLE hotHandle,
                                      KLST_DEVINFO_HANDLE deviceInfo,
                                      KLST_SYNC_FLAG plugType)
        {
            string plugText;

            int totalPluggedDeviceCount = (int) hotHandle.GetContext().ToInt64();
            if (totalPluggedDeviceCount == int.MaxValue)
            {
                Console.WriteLine("OnHotPlug is being called for the first time on handle:{0}", hotHandle.Pointer);
                totalPluggedDeviceCount = 0;
            }

            switch (plugType)
            {
                case KLST_SYNC_FLAG.ADDED:
                    plugText = "Arrival";
                    totalPluggedDeviceCount++;
                    break;
                case KLST_SYNC_FLAG.REMOVED:
                    plugText = "Removal";
                    totalPluggedDeviceCount--;
                    break;
                default:
                    throw new ArgumentOutOfRangeException("plugType");
            }

            hotHandle.SetContext(new IntPtr(totalPluggedDeviceCount));

            Console.WriteLine("\n[OnHotPlug] Device {0}:{1} \n",
                              plugText,
                              deviceInfo);
            Console.WriteLine("Total Plugged Device Count: {0}",
                              totalPluggedDeviceCount);
        }
Пример #2
0
        public UsbK(KLST_DEVINFO_HANDLE DevInfo)
        {
            var success = AllKFunctions.LibK_LoadDriverAPI(out driverAPI, DevInfo.DriverID);

            if (!success)
            {
                throw new Exception();
            }
            success = driverAPI.Init(out mHandleStruct, DevInfo);
            if (!success)
            {
                throw new Exception();
            }
        }
Пример #3
0
 private void OnHotPlugInvoked(KHOT_HANDLE hotHandle, KLST_DEVINFO_HANDLE deviceInfo, KLST_SYNC_FLAG plugType)
 {
     string symbolicLink = deviceInfo.SymbolicLink;
     switch (plugType)
     {
         case KLST_SYNC_FLAG.ADDED:
             int iRow = dgvDevices.Rows.Add(new object[] {symbolicLink, deviceInfo.DeviceDesc, deviceInfo.DeviceID});
             dgvDevices.Rows[iRow].Cells[1].ToolTipText = deviceInfo.ToString();
             dgvDevices.Rows[iRow].Cells[2].ToolTipText = deviceInfo.Common.ToString();
             break;
         case KLST_SYNC_FLAG.REMOVED:
             foreach (DataGridViewRow row in dgvDevices.Rows)
             {
                 if (row.Cells[0].Value as string != symbolicLink) continue;
                 dgvDevices.Rows.Remove(row);
                 break;
             }
             break;
         default:
             throw new ArgumentOutOfRangeException("plugType");
     }
 }
Пример #4
0
        /// <Summary>Creates/opens a libusbK interface handle from the device list. This is a preferred method.</Summary>
        public UsbK(KLST_DEVINFO_HANDLE DevInfo)
        {
            bool success = AllKFunctions.LibK_LoadDriverAPI(out driverAPI, DevInfo.DriverID);

            if (!success) throw new Exception(string.Format("{0} failed loading Driver API. ErrorCode={1:X8}h", GetType().Name, Marshal.GetLastWin32Error()));

            success = driverAPI.Init(out mHandleStruct, DevInfo);

            if (!success)
                throw new Exception(string.Format("{0} failed initializing usb device. ErrorCode={1:X8}h", GetType().Name, Marshal.GetLastWin32Error()));

            Debug.Print("{0} Init: handle 0x{1:X16}", GetType().Name, mHandleStruct.Pointer.ToInt64());
        }
Пример #5
0
 /// <Summary>Advances the device list current \ref KLST_DEVINFO position.</Summary>
 public virtual bool MoveNext(out KLST_DEVINFO_HANDLE DeviceInfo)
 {
     return AllKFunctions.LstK_MoveNext(mHandleStruct, out DeviceInfo);
 }
Пример #6
0
 /// <Summary>Find a device by vendor and product id</Summary>
 public virtual bool FindByVidPid(int Vid, int Pid, out KLST_DEVINFO_HANDLE DeviceInfo)
 {
     return AllKFunctions.LstK_FindByVidPid(mHandleStruct, Vid, Pid, out DeviceInfo);
 }
Пример #7
0
 public virtual bool MoveNext(out KLST_DEVINFO_HANDLE DeviceInfo) =>
 AllKFunctions.LstK_MoveNext(mHandleStruct, out DeviceInfo);
Пример #8
0
        protected override void WndProc(ref Message m)
        {
            /* When using the HotK UserHwnd and UserMsg, the add/remove events are seperated into to different messages.
             * Removal = UserMsg + 0
             * Arrival = UserMsg + 1
             */
            if (m.Msg == WM_USER_HOT_REMOVAL || m.Msg == WM_USER_HOT_ARRIVAL)
            {
                KHOT_HANDLE hotHandle = new KHOT_HANDLE(m.WParam);
                KLST_DEVINFO_HANDLE deviceInfo = new KLST_DEVINFO_HANDLE(m.LParam);
                KLST_SYNC_FLAG plugType = (m.Msg == WM_USER_HOT_REMOVAL) ? KLST_SYNC_FLAG.REMOVED : KLST_SYNC_FLAG.ADDED;

                OnHotPlugInvoked(hotHandle, deviceInfo, plugType);
                return;
            }
            base.WndProc(ref m);
        }
Пример #9
0
        /// <Summary>Creates/opens a libusbK interface handle from the device list. This is a preferred method.</Summary>
        public UsbK(KLST_DEVINFO_HANDLE DevInfo)
        {
            if (!Functions.LibK_LoadDriverAPI(out driverAPI, DevInfo.DriverID))
            {
                throw new Exception(GetType().Name + " failed loading Driver API. ErrorCode=" + Marshal.GetLastWin32Error().ToString("X"));
            }

            RuntimeHelpers.PrepareConstrainedRegions();

            try
            {
            }
            finally
            {
                bool success = driverAPI.Init(out handle, DevInfo);

                if (!success || handle.IsInvalid || handle.IsClosed)
                {
                    handle.SetHandleAsInvalid();
                    int errorCode = Marshal.GetLastWin32Error();
                    throw new Exception(GetType().Name + " failed. ErrorCode=" + errorCode.ToString("X"));
                }
            }
        }
Пример #10
0
 /// <Summary>Advances the device list current \ref KLST_DEVINFO position.</Summary>
 public bool MoveNext(out KLST_DEVINFO_HANDLE DeviceInfo)
 {
     return Functions.LstK_MoveNext(handle, out DeviceInfo);
 }
Пример #11
0
 /// <Summary>Find a device by vendor and product id</Summary>
 public bool FindByVidPid(int Vid, int Pid, out KLST_DEVINFO_HANDLE DeviceInfo)
 {
     return Functions.LstK_FindByVidPid(handle, Vid, Pid, out DeviceInfo);
 }
Пример #12
0
 /// <Summary>Gets the \ref KLST_DEVINFO element for the current position.</Summary>
 public bool Current(out KLST_DEVINFO_HANDLE DeviceInfo)
 {
     return Functions.LstK_Current(handle, out DeviceInfo);
 }