Skip to content

Commit

Permalink
Cave in and use Nvapi wrapper for grabbing driver version
Browse files Browse the repository at this point in the history
Previously I was trying to use the driver release date, but this was frequently
_behind_ the publish date, resulting in false positives.
  • Loading branch information
gmemstr committed Jul 25, 2022
1 parent caa7024 commit 1cd93b0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
2 changes: 1 addition & 1 deletion MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void MainWindow_ContentRendered(object sender, EventArgs e) {
(bool, string) result = Nvidia.API.IsNewDriver();
Grid appGrid = (Grid)this.FindName("appGrid");

(string, DateTime) gpu = GPU.Current();
(string, decimal) gpu = GPU.Current();
gpuName.Text = gpu.Item1;
string guessedGpu = API.GuessGPU();
gpuName.Text += $" ({guessedGpu})";
Expand Down
36 changes: 14 additions & 22 deletions Nvidia/NvidiaWeb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
using System.Xml;
using System.Collections.Generic;
using System.Management;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
using NvAPIWrapper;
using NvAPIWrapper.GPU;

namespace NvidiaDrivers.Nvidia {
public class API {
Expand All @@ -16,7 +19,7 @@ public class API {
public static (bool, string) IsNewDriver() {
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");

(string, DateTime) gpu = GPU.Current();
(string, decimal) gpu = GPU.Current();

// split graphics card name into vendor and model
string[] gpuSplit = gpu.Item1.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
Expand All @@ -38,10 +41,9 @@ public static (bool, string) IsNewDriver() {
var web = new HtmlWeb();
var doc = web.Load(downloadPage);

var element = doc.DocumentNode.SelectSingleNode("//*[@id=\"tdReleaseDate\"]").InnerText.Trim();
// Parse date from "2021.11.16"
DateTime.TryParseExact(element, "yyyy.M.dd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out DateTime releaseDate); // If release date is newer than driver date, print it.
if (releaseDate > gpu.Item2) {
var element = doc.DocumentNode.SelectSingleNode("//*[@id=\"tdVersion\"]").InnerText.Trim().Replace("WHQL", "").Replace(" ", "");
decimal parsedLatest = decimal.Parse(element);
if (gpu.Item2 < parsedLatest) {
return (true, downloadPage);
}
else {
Expand All @@ -51,7 +53,7 @@ public static (bool, string) IsNewDriver() {

// Attempts to guess series of card based on various factors. Currently does not support notebooks.
public static string GuessGPU() {
(string, DateTime) gpu = GPU.Current();
(string, decimal) gpu = GPU.Current();

// split graphics card name into vendor and model
string[] gpuSplit = gpu.Item1.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
Expand Down Expand Up @@ -144,23 +146,13 @@ private static string osToLookup() {
public class GPU {
public String Name { get; set; }

public static (string, DateTime) Current() {
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");
public static (string, decimal) Current()
{
PhysicalGPU[] gpus = NvAPIWrapper.GPU.PhysicalGPU.GetPhysicalGPUs();
PhysicalGPU gpu = gpus[0];
decimal driver = NVIDIA.DriverVersion * 0.01m;

string graphicsCard = string.Empty;
DateTime driverDate = DateTime.Now;
foreach (ManagementObject mo in searcher.Get()) {
foreach (PropertyData property in mo.Properties) {
if (property.Name == "Description") {
graphicsCard = property.Value.ToString();
}
if (property.Name == "DriverDate") {
driverDate = DateTime.ParseExact(property.Value.ToString().Substring(0, 8), "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
}
}
}

return (graphicsCard, driverDate);
return (gpu.FullName, driver);
}
}

Expand Down
1 change: 1 addition & 0 deletions nvidia-drivers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.38" />
<PackageReference Include="NvAPIWrapper.Net" Version="0.8.1.101" />
<PackageReference Include="System.Management" Version="6.0.0" />
</ItemGroup>

Expand Down

0 comments on commit 1cd93b0

Please sign in to comment.