1
0

Added even more helpers

This commit is contained in:
Benjamin Höglinger-Stelzer 2018-10-22 20:39:35 +02:00
parent 20c8d62a21
commit 843563ae88
6 changed files with 147 additions and 4 deletions

View File

@ -70,7 +70,9 @@
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
</ApplicationDefinition> </ApplicationDefinition>
<Compile Include="Util\OsUpgradeDetection.cs" />
<Compile Include="Util\OSVersionInfo.cs" /> <Compile Include="Util\OSVersionInfo.cs" />
<Compile Include="Util\UEFIHelper.cs" />
<Page Include="MainWindow.xaml"> <Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
@ -102,7 +104,9 @@
<Generator>ResXFileCodeGenerator</Generator> <Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput> <LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource> </EmbeddedResource>
<None Include="app.manifest" /> <None Include="app.manifest">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" /> <None Include="packages.config" />
<None Include="Properties\Settings.settings"> <None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator> <Generator>SettingsSingleFileGenerator</Generator>

View File

@ -19,6 +19,8 @@
<Grid Grid.Row="1"> <Grid Grid.Row="1">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
@ -39,6 +41,15 @@
<Label Grid.Row="2" Grid.Column="0">Windows architecture:</Label> <Label Grid.Row="2" Grid.Column="0">Windows architecture:</Label>
<Label Grid.Row="2" Grid.Column="2" Content="{Binding Path=OsArchitecture}"/> <Label Grid.Row="2" Grid.Column="2" Content="{Binding Path=OsArchitecture}"/>
<Label Grid.Row="3" Grid.Column="0">Current boot mode:</Label>
<Label Grid.Row="3" Grid.Column="2" Content="{Binding Path=BootMode}"/>
<Label Grid.Row="4" Grid.Column="0">Secure Boot state:</Label>
<Label Grid.Row="4" Grid.Column="2" Content="{Binding Path=SecureBootEnabled}"/>
<Label Grid.Row="5" Grid.Column="0">OS upgrade status:</Label>
<Label Grid.Row="5" Grid.Column="2" Content="{Binding Path=OsUpgradeStatus}"/>
</Grid> </Grid>
</Grid> </Grid>

View File

@ -14,19 +14,37 @@ namespace DerpingDrivers
} }
/// <summary> /// <summary>
/// Operating System Architecture display name /// Gets Operating System Architecture display name.
/// </summary> /// </summary>
public string OsArchitecture => public string OsArchitecture =>
OsVersionInfo.OsBits == OsVersionInfo.SoftwareArchitecture.Bit64 ? "64-bit" : "32-bit"; OsVersionInfo.OsBits == OsVersionInfo.SoftwareArchitecture.Bit64 ? "64-bit" : "32-bit";
/// <summary> /// <summary>
/// Operating System display name /// Gets Operating System display name.
/// </summary> /// </summary>
public string OsVersionName => $"{OsVersionInfo.Name} {OsVersionInfo.Edition}"; public string OsVersionName => $"{OsVersionInfo.Name} {OsVersionInfo.Edition}";
/// <summary> /// <summary>
/// Operating System build number /// Gets Operating System build number.
/// </summary> /// </summary>
public string OsVersion => OsVersionInfo.VersionString; public string OsVersion => OsVersionInfo.VersionString;
/// <summary>
/// Gets Boot mode (Legacy BIOS, UEFI).
/// </summary>
public string BootMode => UEFIHelper.IsRunningInUEFIMode ? "UEFI" : "Legacy BIOS";
/// <summary>
/// Gets Secure Boot status.
/// </summary>
public string SecureBootEnabled => UEFIHelper.IsRunningInUEFIMode
? (UEFIHelper.IsSecureBootEnabled ? "Enabled" : "Disabled")
: "Not available";
/// <summary>
/// Gets OS upgrade status.
/// </summary>
public string OsUpgradeStatus =>
OsUpgradeDetection.IsGrandfathered ? "In-place upgraded" : "Clean installation";
} }
} }

View File

@ -0,0 +1,56 @@
using System;
using System.Linq;
using Microsoft.Win32;
namespace DerpingDrivers.Util
{
/// <summary>
/// https://superuser.com/a/1184670
/// </summary>
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;
}
}
}
}

View File

@ -0,0 +1,49 @@
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace DerpingDrivers.Util
{
public class UEFIHelper
{
/// <summary>
/// Checks if Secure Boot is enabled.
/// </summary>
public static bool IsSecureBootEnabled
{
get
{
var val = Convert.ToInt32(Registry.GetValue(
@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecureBoot\State",
"UEFISecureBootEnabled",
0));
return val > 0;
}
}
/// <summary>
/// Checks if the current system is running in UEFI or Legacy BIOS mode.
/// </summary>
/// <remarks>https://theroadtodelphi.com/2013/02/19/how-distinguish-when-windows-was-installed-in-legacy-bios-or-uefi-mode-using-delphi/</remarks>
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);
}
}

5
README.md Normal file
View File

@ -0,0 +1,5 @@
# Derping Drivers!
## Sources
- [Getting Operating System Version Info - Even for Windows 10!](<https://www.codeproject.com/Articles/73000/Getting-Operating-System-Version-Info-Even-for-Win>)