Exemple #1
0
        public static void Close()
        {
            if (_driver != null)
            {
                uint refCount = 0;
                _driver.DeviceIOControl(Interop.Ring0.IOCTL_OLS_GET_REFCOUNT, null, ref refCount);
                _driver.Close();

                if (refCount <= 1)
                    _driver.Delete();

                _driver = null;
            }

            if (_isaBusMutex != null)
            {
                _isaBusMutex.Close();
                _isaBusMutex = null;
            }

            if (_pciBusMutex != null)
            {
                _pciBusMutex.Close();
                _pciBusMutex = null;
            }
        }
Exemple #2
0
        public static void Close()
        {
            if (_driver != null)
            {
                uint refCount = 0;
                _driver.DeviceIOControl(Interop.Ring0.IOCTL_OLS_GET_REFCOUNT, null, ref refCount);
                _driver.Close();

                if (refCount <= 1)
                {
                    _driver.Delete();
                }

                _driver = null;
            }

            if (_isaBusMutex != null)
            {
                _isaBusMutex.Close();
                _isaBusMutex = null;
            }

            if (_pciBusMutex != null)
            {
                _pciBusMutex.Close();
                _pciBusMutex = null;
            }

            // try to delete temporary driver file again if failed during open
            if (_fileName != null && File.Exists(_fileName))
            {
                try
                {
                    File.Delete(_fileName);
                    _fileName = null;
                }
                catch (IOException)
                { }
                catch (UnauthorizedAccessException)
                { }
            }
        }
Exemple #3
0
        public static void Open()
        {
            // no implementation for unix systems
            if (Software.OperatingSystem.IsUnix)
            {
                return;
            }

            if (_driver != null)
            {
                return;
            }


            // clear the current report
            _report.Length = 0;

            _driver = new KernelDriver("WinRing0_1_2_0");
            _driver.Open();

            if (!_driver.IsOpen)
            {
                // driver is not loaded, try to install and open
                _fileName = GetTempFileName();
                if (_fileName != null && ExtractDriver(_fileName))
                {
                    if (_driver.Install(_fileName, out string installError))
                    {
                        _driver.Open();

                        if (!_driver.IsOpen)
                        {
                            _driver.Delete();
                            _report.AppendLine("Status: Opening driver failed after install");
                        }
                    }
                    else
                    {
                        string errorFirstInstall = installError;

                        // install failed, try to delete and reinstall
                        _driver.Delete();

                        // wait a short moment to give the OS a chance to remove the driver
                        Thread.Sleep(2000);

                        if (_driver.Install(_fileName, out string errorSecondInstall))
                        {
                            _driver.Open();

                            if (!_driver.IsOpen)
                            {
                                _driver.Delete();
                                _report.AppendLine("Status: Opening driver failed after reinstall");
                            }
                        }
                        else
                        {
                            _report.AppendLine("Status: Installing driver \"" + _fileName + "\" failed" + (File.Exists(_fileName) ? " and file exists" : string.Empty));
                            _report.AppendLine("First Exception: " + errorFirstInstall);
                            _report.AppendLine("Second Exception: " + errorSecondInstall);
                        }
                    }
                }
                else
                {
                    _report.AppendLine("Status: Extracting driver failed");
                }

                try
                {
                    // try to delete the driver file
                    if (File.Exists(_fileName) && _fileName != null)
                    {
                        File.Delete(_fileName);
                    }

                    _fileName = null;
                }
                catch (IOException)
                { }
                catch (UnauthorizedAccessException)
                { }
            }

            if (!_driver.IsOpen)
            {
                _driver = null;
            }

            const string isaMutexName = "Global\\Access_ISABUS.HTP.Method";

            try
            {
#if NETFRAMEWORK
                //mutex permissions set to everyone to allow other software to access the hardware
                //otherwise other monitoring software cant access
                var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
                var securitySettings  = new MutexSecurity();
                securitySettings.AddAccessRule(allowEveryoneRule);
                _isaBusMutex = new Mutex(false, isaMutexName, out _, securitySettings);
#else
                _isaBusMutex = new Mutex(false, isaMutexName);
#endif
            }
            catch (UnauthorizedAccessException)
            {
                try
                {
#if NETFRAMEWORK
                    _isaBusMutex = Mutex.OpenExisting(isaMutexName, MutexRights.Synchronize);
#else
                    _isaBusMutex = Mutex.OpenExisting(isaMutexName);
#endif
                }
                catch
                { }
            }

            const string pciMutexName = "Global\\Access_PCI";

            try
            {
                _pciBusMutex = new Mutex(false, pciMutexName);
            }
            catch (UnauthorizedAccessException)
            {
                try
                {
#if NETFRAMEWORK
                    _pciBusMutex = Mutex.OpenExisting(pciMutexName, MutexRights.Synchronize);
#else
                    _pciBusMutex = Mutex.OpenExisting(pciMutexName);
#endif
                }
                catch
                { }
            }
        }