示例#1
0
 public TetherScriptDevice(DriverProductIds driverProduct)
 {
     try
     {
         OpenDevice(driverProduct);
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Failed to create TetherScript device: " + ex.Message);
     }
 }
示例#2
0
        private bool OpenDevice(DriverProductIds driverProduct)
        {
            try
            {
                //Check driver product type
                if (driverProduct == DriverProductIds.TTC_PRODUCTID_KEYBOARD)
                {
                    HardwareTarget = "hid\\ttcvcontrkb";
                }
                else if (driverProduct == DriverProductIds.TTC_PRODUCTID_MOUSEREL)
                {
                    HardwareTarget = "hid\\ttcvcontrmsrel";
                }
                else if (driverProduct == DriverProductIds.TTC_PRODUCTID_MOUSEABS)
                {
                    HardwareTarget = "hid\\ttcvcontrmsabs";
                }

                //Find the virtual device path
                IEnumerable <EnumerateInfo> SelectedHidDevice = EnumerateDevicesDi(GuidClassHidDevice, true);
                foreach (EnumerateInfo EnumDevice in SelectedHidDevice)
                {
                    if (EnumDevice.HardwareId.ToLower() == HardwareTarget)
                    {
                        DevicePath = EnumDevice.DevicePath;
                        break;
                    }
                }

                FileShareMode           shareModeExclusive  = FileShareMode.FILE_SHARE_NONE;
                FileShareMode           shareModeNormal     = FileShareMode.FILE_SHARE_READ | FileShareMode.FILE_SHARE_WRITE;
                FileDesiredAccess       desiredAccess       = FileDesiredAccess.GENERIC_WRITE;
                FileCreationDisposition creationDisposition = FileCreationDisposition.OPEN_EXISTING;
                FileFlagsAndAttributes  flagsAttributes     = FileFlagsAndAttributes.FILE_FLAG_NORMAL | FileFlagsAndAttributes.FILE_FLAG_OVERLAPPED | FileFlagsAndAttributes.FILE_FLAG_NO_BUFFERING;

                //Try to open the device exclusively
                FileHandle = CreateFile(DevicePath, desiredAccess, shareModeExclusive, IntPtr.Zero, creationDisposition, flagsAttributes, IntPtr.Zero);
                Exclusive  = true;

                //Try to open the device normally
                if (FileHandle == null || FileHandle.IsInvalid || FileHandle.IsClosed)
                {
                    //Debug.WriteLine("Failed to open TetherScript device exclusively, opening normally.");
                    FileHandle = CreateFile(DevicePath, desiredAccess, shareModeNormal, IntPtr.Zero, creationDisposition, flagsAttributes, IntPtr.Zero);
                    Exclusive  = false;
                }

                //Check if the device is opened
                if (FileHandle == null || FileHandle.IsInvalid || FileHandle.IsClosed)
                {
                    //Debug.WriteLine("Failed to open TetherScript device: " + DevicePath);
                    Connected = false;
                    Installed = false;
                    return(false);
                }
                else
                {
                    //Debug.WriteLine("Opened TetherScript device: " + DevicePath + ", exclusively: " + Exclusive);
                    Connected = true;
                    Installed = true;
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to open TetherScript device: " + ex.Message);
                Connected = false;
                Installed = false;
                return(false);
            }
        }