public static void Close() { if (driver == null) { return; } 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; } }
public static void Open(string PathService) { // no implementation for unix systems int p = (int)Environment.OSVersion.Platform; if ((p == 4) || (p == 128)) { 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 = PathService + "\\" + (OperatingSystem.Is64BitOperatingSystem() ? "WinRing0x64.sys" : "WinRing0.sys"); string installError; if (driver.Install(fileName, out 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); string errorSecondInstall; if (driver.Install(fileName, out 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); } } } if (!driver.IsOpen) { driver = null; } string mutexName = "Global\\Access_ISABUS.HTP.Method"; try { isaBusMutex = new Mutex(false, mutexName); } catch (UnauthorizedAccessException) { try { isaBusMutex = Mutex.OpenExisting(mutexName, MutexRights.Synchronize); } catch { } } }