Hardware/Ring0.cs
changeset 254 d8079800a888
parent 238 bddc6e01840a
child 255 a608358af258
     1.1 --- a/Hardware/Ring0.cs	Mon Feb 07 22:06:58 2011 +0000
     1.2 +++ b/Hardware/Ring0.cs	Tue Feb 08 22:02:29 2011 +0000
     1.3 @@ -16,7 +16,7 @@
     1.4  
     1.5    The Initial Developer of the Original Code is 
     1.6    Michael Möller <m.moeller@gmx.ch>.
     1.7 -  Portions created by the Initial Developer are Copyright (C) 2010
     1.8 +  Portions created by the Initial Developer are Copyright (C) 2010-2011
     1.9    the Initial Developer. All Rights Reserved.
    1.10  
    1.11    Contributor(s):
    1.12 @@ -40,12 +40,14 @@
    1.13  using System.Reflection;
    1.14  using System.Runtime.InteropServices;
    1.15  using System.Threading;
    1.16 +using System.Text;
    1.17  
    1.18  namespace OpenHardwareMonitor.Hardware {
    1.19    internal static class Ring0 {
    1.20  
    1.21      private static KernelDriver driver;
    1.22      private static Mutex isaBusMutex;
    1.23 +    private static readonly StringBuilder report = new StringBuilder();
    1.24  
    1.25      private const uint OLS_TYPE = 40000;
    1.26      private static IOControlCode
    1.27 @@ -95,28 +97,41 @@
    1.28      }
    1.29  
    1.30      public static void Open() {
    1.31 -      // No implementation for Unix systems
    1.32 +      // no implementation for unix systems
    1.33        int p = (int)Environment.OSVersion.Platform;
    1.34        if ((p == 4) || (p == 128))
    1.35          return;  
    1.36        
    1.37        if (driver != null)
    1.38          return;
    1.39 +
    1.40 +      // clear the current report
    1.41 +      report.Length = 0;
    1.42       
    1.43        driver = new KernelDriver("WinRing0_1_2_0");
    1.44        driver.Open();
    1.45  
    1.46        if (!driver.IsOpen) {
    1.47 +        // driver is not loaded, try to install and open
    1.48          string fileName = Path.GetTempFileName();
    1.49          if (ExtractDriver(fileName)) {
    1.50 +          if (driver.Install(fileName)) {
    1.51 +            File.Delete(fileName);
    1.52 +            driver.Open();
    1.53  
    1.54 -          driver.Install(fileName);
    1.55 -          File.Delete(fileName);
    1.56 -
    1.57 -          driver.Open();
    1.58 -
    1.59 -          if (!driver.IsOpen)
    1.60 -            driver.Delete();
    1.61 +            if (!driver.IsOpen) {
    1.62 +              driver.Delete();
    1.63 +              report.AppendLine("Status: Opening driver failed");
    1.64 +            }
    1.65 +          } else {
    1.66 +            report.AppendLine("Status: Installing driver failed");
    1.67 +            report.AppendLine();
    1.68 +            report.Append("Exception: " + Marshal.GetExceptionForHR(
    1.69 +              Marshal.GetHRForLastWin32Error()).Message);
    1.70 +          }
    1.71 +        } else {
    1.72 +          report.AppendLine(
    1.73 +            "Status: Extracting driver to \"" + fileName + "\" failed");
    1.74          }
    1.75        }
    1.76  
    1.77 @@ -147,6 +162,16 @@
    1.78        isaBusMutex.Close(); 
    1.79      }
    1.80  
    1.81 +    public static string GetReport() {
    1.82 +      if (report.Length > 0) {
    1.83 +        report.Insert(0, "Ring0" + Environment.NewLine +
    1.84 +          Environment.NewLine);
    1.85 +        report.AppendLine();
    1.86 +        return report.ToString();
    1.87 +      } else
    1.88 +        return null;
    1.89 +    }
    1.90 +
    1.91      public static bool WaitIsaBusMutex(int millisecondsTimeout) {
    1.92        try {
    1.93          return isaBusMutex.WaitOne(millisecondsTimeout, false);