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; } // 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) { } } }
public static void Open() { // 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 = 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 { 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); } } } 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; } string mutexName = "Global\\Access_ISABUS.HTP.Method"; try { isaBusMutex = new Mutex(false, mutexName); } catch (UnauthorizedAccessException) { try { isaBusMutex = Mutex.OpenExisting(mutexName, MutexRights.Synchronize); } catch { } } }