Hardware/Ring0.cs
changeset 283 fb88cac4a9aa
parent 281 1c301069cfce
child 285 0820c0558727
     1.1 --- a/Hardware/Ring0.cs	Sun May 15 16:00:22 2011 +0000
     1.2 +++ b/Hardware/Ring0.cs	Sun May 15 16:20:30 2011 +0000
     1.3 @@ -71,6 +71,27 @@
     1.4        IOCTL_OLS_READ_MEMORY = new IOControlCode(OLS_TYPE, 0x841,
     1.5          IOControlCode.Access.Read);
     1.6  
     1.7 +    private static string GetTempFileName() {
     1.8 +
     1.9 +      // try to get a file in the temporary folder
    1.10 +      try {
    1.11 +        return Path.GetTempFileName();        
    1.12 +      } catch (IOException) { } 
    1.13 +        catch (UnauthorizedAccessException) { }
    1.14 +
    1.15 +      // if this failed, we try to create one in the application folder
    1.16 +      string fileName = Path.ChangeExtension(
    1.17 +        Assembly.GetExecutingAssembly().Location, ".sys");
    1.18 +      try {
    1.19 +        using (FileStream stream = File.Create(fileName)) {
    1.20 +          return fileName;
    1.21 +        }        
    1.22 +      } catch (IOException) { } 
    1.23 +        catch (UnauthorizedAccessException) { }
    1.24 +     
    1.25 +      return null;
    1.26 +    }
    1.27 +
    1.28      private static bool ExtractDriver(string fileName) {
    1.29        string resourceName = "OpenHardwareMonitor.Hardware." +
    1.30          (IntPtr.Size == 4 ? "WinRing0.sys" : "WinRing0x64.sys");
    1.31 @@ -118,8 +139,8 @@
    1.32          // driver is not loaded, try to reinstall and open
    1.33  
    1.34          driver.Delete();
    1.35 -        fileName = Path.GetTempFileName();
    1.36 -        if (ExtractDriver(fileName)) {
    1.37 +        fileName = GetTempFileName();
    1.38 +        if (fileName != null && ExtractDriver(fileName)) {
    1.39            if (driver.Install(fileName)) {
    1.40              driver.Open();
    1.41  
    1.42 @@ -128,17 +149,15 @@
    1.43                report.AppendLine("Status: Opening driver failed");
    1.44              }
    1.45            } else {
    1.46 -            report.AppendLine("Status: Installing driver \"" + 
    1.47 -              fileName + "\" failed" + 
    1.48 +            report.AppendLine("Status: Installing driver \"" +
    1.49 +              fileName + "\" failed" +
    1.50                (File.Exists(fileName) ? " and file exists" : ""));
    1.51              report.AppendLine();
    1.52              report.Append("Exception: " + Marshal.GetExceptionForHR(
    1.53                Marshal.GetHRForLastWin32Error()).Message);
    1.54            }
    1.55 -          
    1.56          } else {
    1.57 -          report.AppendLine(
    1.58 -            "Status: Extracting driver to \"" + fileName + "\" failed");
    1.59 +          report.AppendLine("Status: Extracting driver failed");
    1.60          }
    1.61  
    1.62          try {