DS4Windows.HidDevice.OpenDevice C# (CSharp) Method

OpenDevice() public method

public OpenDevice ( bool isExclusive ) : void
isExclusive bool
return void
        public void OpenDevice(bool isExclusive)
        {
            if (IsOpen) return;
            try
            {
                if (safeReadHandle == null || safeReadHandle.IsInvalid)
                    safeReadHandle = OpenHandle(_devicePath, isExclusive);
            }
            catch (Exception exception)
            {
                IsOpen = false;
                throw new Exception("Error opening HID device.", exception);
            }

            IsOpen = !safeReadHandle.IsInvalid;
            IsExclusive = isExclusive;
        }

Usage Example

Example #1
0
        private static void OpenDevice(HidDevice hDevice)
        {
            hDevice.OpenDevice(IsExclusiveMode);
            if (!hDevice.IsOpen && IsExclusiveMode)
            {
                try
                {
                    WindowsIdentity  identity  = WindowsIdentity.GetCurrent();
                    WindowsPrincipal principal = new WindowsPrincipal(identity);
                    bool             elevated  = principal.IsInRole(WindowsBuiltInRole.Administrator);

                    if (!elevated)
                    {
                        // Launches an elevated child process to re-enable device
                        string           exeName   = Process.GetCurrentProcess().MainModule.FileName;
                        ProcessStartInfo startInfo = new ProcessStartInfo(exeName)
                        {
                            Verb      = "runas",
                            Arguments = "re-enabledevice " + DevicePathToInstanceId(hDevice.DevicePath)
                        };

                        Process child = Process.Start(startInfo);
                        if (!child.WaitForExit(30000))
                        {
                            child.Kill();
                        }
                        else if (child.ExitCode == 0)
                        {
                            hDevice.OpenDevice(IsExclusiveMode);
                        }
                    }
                    else
                    {
                        ReEnableDevice(DevicePathToInstanceId(hDevice.DevicePath));
                        hDevice.OpenDevice(IsExclusiveMode);
                    }
                }
                catch (Exception) { }
            }

            // TODO in exclusive mode, try to hold both open when both are connected
            if (IsExclusiveMode && !hDevice.IsOpen)
            {
                hDevice.OpenDevice(false);
            }
        }
All Usage Examples Of DS4Windows.HidDevice::OpenDevice