From da432ef07cf125c019afd3a27f447da428404611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20M=C3=B6ller?= Date: Thu, 26 May 2016 17:27:24 +0200 Subject: [PATCH] Fixed the Assembly.GetEntryAssembly() can be null issue when loading the library from unmanaged code. --- Hardware/Ring0.cs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/Hardware/Ring0.cs b/Hardware/Ring0.cs index 940c0ce..9f5a44c 100644 --- a/Hardware/Ring0.cs +++ b/Hardware/Ring0.cs @@ -4,7 +4,7 @@ License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. - Copyright (C) 2010-2012 Michael Möller + Copyright (C) 2010-2016 Michael Möller */ @@ -45,17 +45,22 @@ namespace OpenHardwareMonitor.Hardware { IOCTL_OLS_READ_MEMORY = new IOControlCode(OLS_TYPE, 0x841, IOControlCode.Access.Read); + private static Assembly GetAssembly() { + return typeof(Ring0).Assembly; + } + private static string GetTempFileName() { // try to create one in the application folder - string fileName = Path.ChangeExtension( - Assembly.GetEntryAssembly().Location, ".sys"); - try { - using (FileStream stream = File.Create(fileName)) { - return fileName; - } - } catch (IOException) { } - catch (UnauthorizedAccessException) { } + string location = GetAssembly().Location; + if (!string.IsNullOrEmpty(location)) { + try { + string fileName = Path.ChangeExtension(location, ".sys"); + using (FileStream stream = File.Create(fileName)) { + return fileName; + } + } catch (Exception) { } + } // if this failed, try to get a file in the temporary folder try { @@ -78,12 +83,11 @@ namespace OpenHardwareMonitor.Hardware { (OperatingSystem.Is64BitOperatingSystem() ? "WinRing0x64.sys" : "WinRing0.sys"); - string[] names = - Assembly.GetExecutingAssembly().GetManifestResourceNames(); + string[] names = GetAssembly().GetManifestResourceNames(); byte[] buffer = null; for (int i = 0; i < names.Length; i++) { if (names[i].Replace('\\', '.') == resourceName) { - using (Stream stream = Assembly.GetExecutingAssembly(). + using (Stream stream = GetAssembly(). GetManifestResourceStream(names[i])) { buffer = new byte[stream.Length];