예제 #1
0
        public static void Close()
        {
            if (driver != null)
            {
                uint refCount = 0;
                driver.DeviceIOControl(IOCTL_OLS_GET_REFCOUNT, null, ref refCount);
                driver.Close();

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

            if (isaBusMutex != null)
            {
                isaBusMutex.Close();
                isaBusMutex = 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)
                {
                }
            }
예제 #2
0
        public static void Open()
        {
            // no implementation for unix systems
            if (IsOpen || OperatingSystem.IsLinux)
            {
                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))
                {
                    string installError;
                    if (driver.Install(fileName, out installError))
                    {
                        driver.Open();

                        if (!driver.IsOpen)
                        {
                            driver.Delete();
                            report.AppendLine("Status: Opening driver failed after install");
                        }
                    }
                    else
                    {
                        var 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 var 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" : ""));
                            report.AppendLine("First Exception: " + errorFirstInstall);
                            report.AppendLine("Second Exception: " + errorSecondInstall);
                        }
                    }
                }
                else
                {
                    report.AppendLine("Status: Extracting driver failed");
                }

                try
                {
                    // try to delte the driver file
                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }
                    fileName = null;
                }
                catch (IOException)
                {
                }
                catch (UnauthorizedAccessException)
                {
                }
            }

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

            var mutexName = "Global\\Access_ISABUS.HTP.Method";

            try
            {
                isaBusMutex = new Mutex(false, mutexName);
            }
            catch (UnauthorizedAccessException)
            {
                try
                {
                    isaBusMutex = Mutex.OpenExisting(mutexName);
                }
                catch
                {
                }
            }
        }