From 843563ae889834b95a142524d23be8239927713d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=B6glinger-Stelzer?= Date: Mon, 22 Oct 2018 20:39:35 +0200 Subject: [PATCH] Added even more helpers --- DerpingDrivers/DerpingDrivers.csproj | 6 ++- DerpingDrivers/MainWindow.xaml | 11 +++++ DerpingDrivers/MainWindow.xaml.cs | 24 ++++++++-- DerpingDrivers/Util/OsUpgradeDetection.cs | 56 +++++++++++++++++++++++ DerpingDrivers/Util/UEFIHelper.cs | 49 ++++++++++++++++++++ README.md | 5 ++ 6 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 DerpingDrivers/Util/OsUpgradeDetection.cs create mode 100644 DerpingDrivers/Util/UEFIHelper.cs create mode 100644 README.md diff --git a/DerpingDrivers/DerpingDrivers.csproj b/DerpingDrivers/DerpingDrivers.csproj index 523bcef..e7065dd 100644 --- a/DerpingDrivers/DerpingDrivers.csproj +++ b/DerpingDrivers/DerpingDrivers.csproj @@ -70,7 +70,9 @@ MSBuild:Compile Designer + + MSBuild:Compile Designer @@ -102,7 +104,9 @@ ResXFileCodeGenerator Resources.Designer.cs - + + Designer + SettingsSingleFileGenerator diff --git a/DerpingDrivers/MainWindow.xaml b/DerpingDrivers/MainWindow.xaml index c0ac040..08e7b14 100644 --- a/DerpingDrivers/MainWindow.xaml +++ b/DerpingDrivers/MainWindow.xaml @@ -19,6 +19,8 @@ + + @@ -39,6 +41,15 @@ diff --git a/DerpingDrivers/MainWindow.xaml.cs b/DerpingDrivers/MainWindow.xaml.cs index c28af84..77ea289 100644 --- a/DerpingDrivers/MainWindow.xaml.cs +++ b/DerpingDrivers/MainWindow.xaml.cs @@ -14,19 +14,37 @@ namespace DerpingDrivers } /// - /// Operating System Architecture display name + /// Gets Operating System Architecture display name. /// public string OsArchitecture => OsVersionInfo.OsBits == OsVersionInfo.SoftwareArchitecture.Bit64 ? "64-bit" : "32-bit"; /// - /// Operating System display name + /// Gets Operating System display name. /// public string OsVersionName => $"{OsVersionInfo.Name} {OsVersionInfo.Edition}"; /// - /// Operating System build number + /// Gets Operating System build number. /// public string OsVersion => OsVersionInfo.VersionString; + + /// + /// Gets Boot mode (Legacy BIOS, UEFI). + /// + public string BootMode => UEFIHelper.IsRunningInUEFIMode ? "UEFI" : "Legacy BIOS"; + + /// + /// Gets Secure Boot status. + /// + public string SecureBootEnabled => UEFIHelper.IsRunningInUEFIMode + ? (UEFIHelper.IsSecureBootEnabled ? "Enabled" : "Disabled") + : "Not available"; + + /// + /// Gets OS upgrade status. + /// + public string OsUpgradeStatus => + OsUpgradeDetection.IsGrandfathered ? "In-place upgraded" : "Clean installation"; } } \ No newline at end of file diff --git a/DerpingDrivers/Util/OsUpgradeDetection.cs b/DerpingDrivers/Util/OsUpgradeDetection.cs new file mode 100644 index 0000000..f78a57f --- /dev/null +++ b/DerpingDrivers/Util/OsUpgradeDetection.cs @@ -0,0 +1,56 @@ +using System; +using System.Linq; +using Microsoft.Win32; + +namespace DerpingDrivers.Util +{ + /// + /// https://superuser.com/a/1184670 + /// + public class OsUpgradeDetection + { + public static bool IsGrandfathered + { + get + { + using (var setupKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\Setup")) + { + using (var upgradeKey = setupKey?.OpenSubKey("Upgrade")) + { + // if this key isn't there, don't look any further + if (upgradeKey == null) return false; + + // only look at "Source OS (...)" sub-keys + foreach (var sosKeyName in setupKey.GetSubKeyNames().Where(v => + v.StartsWith("Source OS", StringComparison.InvariantCultureIgnoreCase))) + { + using (var sosKey = setupKey.OpenSubKey(sosKeyName)) + { + var productName = sosKey?.GetValue("ProductName") as string; + + if (string.IsNullOrEmpty(productName)) + { + continue; + } + + // TODO: untested but should work + if (productName.StartsWith("Windows 7", StringComparison.InvariantCultureIgnoreCase)) + { + return true; + } + + // TODO: untested but should work + if (productName.StartsWith("Windows 8", StringComparison.InvariantCultureIgnoreCase)) + { + return true; + } + } + } + } + } + + return false; + } + } + } +} \ No newline at end of file diff --git a/DerpingDrivers/Util/UEFIHelper.cs b/DerpingDrivers/Util/UEFIHelper.cs new file mode 100644 index 0000000..5f0a77f --- /dev/null +++ b/DerpingDrivers/Util/UEFIHelper.cs @@ -0,0 +1,49 @@ +using System; +using System.Runtime.InteropServices; +using Microsoft.Win32; + +namespace DerpingDrivers.Util +{ + public class UEFIHelper + { + /// + /// Checks if Secure Boot is enabled. + /// + public static bool IsSecureBootEnabled + { + get + { + var val = Convert.ToInt32(Registry.GetValue( + @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecureBoot\State", + "UEFISecureBootEnabled", + 0)); + + return val > 0; + } + } + + /// + /// Checks if the current system is running in UEFI or Legacy BIOS mode. + /// + /// https://theroadtodelphi.com/2013/02/19/how-distinguish-when-windows-was-installed-in-legacy-bios-or-uefi-mode-using-delphi/ + public static bool IsRunningInUEFIMode + { + get + { + GetFirmwareEnvironmentVariable( + string.Empty, + "{00000000-0000-0000-0000-000000000000}", + IntPtr.Zero, 0); + + var error = Marshal.GetLastWin32Error(); + + return error != 0x01; // ERROR_INVALID_FUNCTION + } + } + + [DllImport("kernel32.dll", EntryPoint = "GetFirmwareEnvironmentVariableW", SetLastError = true, + CharSet = CharSet.Unicode, ExactSpelling = true)] + private static extern uint GetFirmwareEnvironmentVariable(string lpName, string lpGuid, IntPtr pBuffer, + uint nSize); + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..d7dfddc --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Derping Drivers! + +## Sources + +- [Getting Operating System Version Info - Even for Windows 10!]() \ No newline at end of file