Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Hardware.Info.Aot.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static void Main(string[] _)

static void Test(bool test)
{
IHardwareInfo hardwareInfo = new HardwareInfo();
using IHardwareInfo hardwareInfo = new HardwareInfo();

hardwareInfo.RefreshOperatingSystem();
hardwareInfo.RefreshMemoryStatus();
Expand Down
2 changes: 2 additions & 0 deletions Hardware.Info.Aot/Windows/WmiLightObjectAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ internal sealed class WmiLightObjectAdapter : IWmiPropertySource
public object this[string propertyName] => _inner[propertyName];

public WmiObject GetWmiObject() => _inner;

public void Dispose() => _inner.Dispose();
}
}
11 changes: 10 additions & 1 deletion Hardware.Info.Aot/Windows/WmiLightQueryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public IEnumerable<IWmiPropertySource> QueryRelated(string scope, IWmiPropertySo
_wmiConnectionDict[scope] = wmiConnection;
}

WmiLightObjectAdapter? wmiLightObjectAdapter = wmiPropertySource as WmiLightObjectAdapter;
using WmiLightObjectAdapter? wmiLightObjectAdapter = wmiPropertySource as WmiLightObjectAdapter;

if (wmiLightObjectAdapter is null)
yield break;
Expand All @@ -61,5 +61,14 @@ public IEnumerable<IWmiPropertySource> QueryRelated(string scope, IWmiPropertySo
yield return new WmiLightObjectAdapter(mo);
}
}

public void Dispose()
{
foreach (var wmiConnection in _wmiConnectionDict.Values)
{
wmiConnection.Dispose();
}
_wmiConnectionDict.Clear();
}
}
}
5 changes: 5 additions & 0 deletions Hardware.Info.Core/HardwareInfoBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ public void RefreshAll()
/// </summary>
public void RefreshVideoControllerList() => VideoControllerList = _platformHardwareInfo.GetVideoControllerList();

public void Dispose()
{
_platformHardwareInfo.Dispose();
}

#region Static

private static bool _pingInProgress;
Expand Down
5 changes: 3 additions & 2 deletions Hardware.Info.Core/IHardwareInfo.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace Hardware.Info
{
/// <summary>
/// Main Hardware.Info interface
/// </summary>
public interface IHardwareInfo
public interface IHardwareInfo : IDisposable
{
/// <summary>
/// Operating system
Expand Down
5 changes: 3 additions & 2 deletions Hardware.Info.Core/IPlatformHardwareInfo.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Hardware.Info, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f9caba3b554c3c962fd85b3722680234d5f5ca2abec47a345a22e9f11e5abbb6c480c7b3506c1d493732056c439f9bee9f79e8f4d67ce6c1104ea88cc4c0273e6c85612e5a0f8d9aca97454da75c92817874a1d18d89fa97de1fab64c8e000f44350a0142486bf0b16e767fc4f2daefe1a6f062467f79e59d969899540a93bdf")]
[assembly: InternalsVisibleTo("Hardware.Info.Aot, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f9caba3b554c3c962fd85b3722680234d5f5ca2abec47a345a22e9f11e5abbb6c480c7b3506c1d493732056c439f9bee9f79e8f4d67ce6c1104ea88cc4c0273e6c85612e5a0f8d9aca97454da75c92817874a1d18d89fa97de1fab64c8e000f44350a0142486bf0b16e767fc4f2daefe1a6f062467f79e59d969899540a93bdf")]

namespace Hardware.Info
{
public interface IPlatformHardwareInfo
public interface IPlatformHardwareInfo : IDisposable
{
OS GetOperatingSystem();
MemoryStatus GetMemoryStatus();
Expand Down
5 changes: 4 additions & 1 deletion Hardware.Info.Core/Linux/PlatformHardwareInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

Expand Down Expand Up @@ -1233,5 +1232,9 @@ 1680x1050 59.88

return videoControllerList;
}

public void Dispose()
{
}
}
}
4 changes: 4 additions & 0 deletions Hardware.Info.Core/Mac/PlatformHardwareInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1309,5 +1309,9 @@ public List<VideoController> GetVideoControllerList()

return videoControllerList;
}

public void Dispose()
{
}
}
}
6 changes: 4 additions & 2 deletions Hardware.Info.Core/Windows/IWmiPropertySource.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Hardware.Info.Windows
using System;

namespace Hardware.Info.Windows
{
internal interface IWmiPropertySource
internal interface IWmiPropertySource : IDisposable
{
object this[string propertyName] { get; }
}
Expand Down
5 changes: 3 additions & 2 deletions Hardware.Info.Core/Windows/IWmiQueryProvider.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace Hardware.Info.Windows
{
internal interface IWmiQueryProvider
internal interface IWmiQueryProvider : IDisposable
{
IEnumerable<IWmiPropertySource> Query(string scope, string query);

Expand Down
57 changes: 54 additions & 3 deletions Hardware.Info.Core/Windows/PlatformHardwareInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public void GetOs()

foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, queryString))
{
using var _ = mo;

_os.Name = GetPropertyString(mo["Caption"]);
_os.VersionString = GetPropertyString(mo["Version"]);

Expand Down Expand Up @@ -188,6 +190,8 @@ public List<Battery> GetBatteryList()

foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, queryString))
{
using var _ = mo;

Battery battery = new Battery
{
FullChargeCapacity = GetPropertyValue<uint>(mo["FullChargeCapacity"]),
Expand Down Expand Up @@ -215,6 +219,8 @@ public List<BIOS> GetBiosList()

foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, queryString))
{
using var _ = mo;

BIOS bios = new BIOS
{
Caption = GetPropertyString(mo["Caption"]),
Expand All @@ -241,6 +247,8 @@ public List<ComputerSystem> GetComputerSystemList()

foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, queryString))
{
using var _ = mo;

ComputerSystem computerSystem = new ComputerSystem
{
Caption = GetPropertyString(mo["Caption"]),
Expand Down Expand Up @@ -277,6 +285,8 @@ public List<CPU> GetCpuList(bool includePercentProcessorTime = true, int millise
{
foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, queryString))
{
using var _ = mo;

CpuCore core = new CpuCore
{
Name = GetPropertyString(mo["Name"]),
Expand All @@ -288,6 +298,8 @@ public List<CPU> GetCpuList(bool includePercentProcessorTime = true, int millise

foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, totalQueryString))
{
using var _ = mo;

percentProcessorTime = GetPropertyValue<ulong>(mo["PercentProcessorTime"]);
}
}
Expand All @@ -302,6 +314,8 @@ public List<CPU> GetCpuList(bool includePercentProcessorTime = true, int millise

foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, processorQueryString))
{
using var _ = mo;

percentProcessorTime = GetPropertyValue<ushort>(mo["LoadPercentage"]);
}
}
Expand Down Expand Up @@ -343,6 +357,8 @@ public List<CPU> GetCpuList(bool includePercentProcessorTime = true, int millise
// Unified = 5
foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, cacheMemoryquery))
{
using var _ = mo;

ushort CacheType = GetPropertyValue<ushort>(mo["CacheType"]);
uint MaxCacheSize = 1024 * GetPropertyValue<uint>(mo["MaxCacheSize"]);

Expand All @@ -363,6 +379,8 @@ public List<CPU> GetCpuList(bool includePercentProcessorTime = true, int millise

foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, processorQuery))
{
using var _ = mo;

uint maxClockSpeed = GetPropertyValue<uint>(mo["MaxClockSpeed"]);

uint currentClockSpeed = (uint)(maxClockSpeed * (processorPerformance / 100));
Expand Down Expand Up @@ -409,6 +427,8 @@ public override List<Drive> GetDriveList()

foreach (IWmiPropertySource DiskDrive in _wmiQueryProvider.Query(_managementScope, diskDriveQueryString))
{
using var _ = DiskDrive;

Drive drive = new Drive
{
Caption = GetPropertyString(DiskDrive["Caption"]),
Expand All @@ -428,6 +448,8 @@ public override List<Drive> GetDriveList()

foreach (IWmiPropertySource DiskPartition in _wmiQueryProvider.Query(_managementScope, diskPartitionQueryString))
{
using var __ = DiskPartition;

Partition partition = new Partition
{
Bootable = GetPropertyValue<bool>(DiskPartition["Bootable"]),
Expand All @@ -446,6 +468,8 @@ public override List<Drive> GetDriveList()

foreach (IWmiPropertySource LogicalDisk in _wmiQueryProvider.Query(_managementScope, logicalDiskQueryString))
{
using var ___ = LogicalDisk;

Volume volume = new Volume
{
Caption = GetPropertyString(LogicalDisk["Caption"]),
Expand Down Expand Up @@ -479,6 +503,8 @@ public List<Keyboard> GetKeyboardList()

foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, queryString))
{
using var _ = mo;

Keyboard keyboard = new Keyboard
{
Caption = GetPropertyString(mo["Caption"]),
Expand All @@ -502,6 +528,8 @@ public List<Memory> GetMemoryList()

foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, queryString))
{
using var _ = mo;

Memory memory = new Memory
{
BankLabel = GetPropertyString(mo["BankLabel"]),
Expand All @@ -518,7 +546,7 @@ public List<Memory> GetMemoryList()
{
memory.MaxVoltage = GetPropertyValue<uint>(mo["MaxVoltage"]);
memory.MinVoltage = GetPropertyValue<uint>(mo["MinVoltage"]);

memory.MemoryType = (MemoryType)GetPropertyValue<uint>(mo["SMBIOSMemoryType"]);
}

Expand All @@ -536,15 +564,17 @@ public List<Monitor> GetMonitorList()

foreach (IWmiPropertySource win32PnpEntityMo in _wmiQueryProvider.Query(_managementScope, win32PnpEntityQuery))
{
using var _ = win32PnpEntityMo;

try
{
string deviceId = GetPropertyString(win32PnpEntityMo["DeviceId"]);
string win32DesktopMonitorQuery = $"SELECT Caption, Description, MonitorManufacturer, MonitorType, Name, PixelsPerXLogicalInch, PixelsPerYLogicalInch FROM Win32_DesktopMonitor WHERE PNPDeviceId='{deviceId}'".Replace(@"\", @"\\");

string wmiMonitorIdQuery = $"SELECT Active, ProductCodeID, SerialNumberID, ManufacturerName, UserFriendlyName, WeekOfManufacture, YearOfManufacture FROM WmiMonitorID WHERE InstanceName LIKE '{deviceId}%'".Replace(@"\", "_");

IWmiPropertySource? desktopMonitorMo = _wmiQueryProvider.Query(_managementScope, win32DesktopMonitorQuery).FirstOrDefault();
IWmiPropertySource? wmiMonitorIdMo = _wmiQueryProvider.Query(_managementScopeWmi, wmiMonitorIdQuery).FirstOrDefault();
using IWmiPropertySource? desktopMonitorMo = _wmiQueryProvider.Query(_managementScope, win32DesktopMonitorQuery).FirstOrDefault();
using IWmiPropertySource? wmiMonitorIdMo = _wmiQueryProvider.Query(_managementScopeWmi, wmiMonitorIdQuery).FirstOrDefault();

Monitor monitor = new Monitor();

Expand Down Expand Up @@ -588,6 +618,8 @@ public List<Motherboard> GetMotherboardList()

foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, queryString))
{
using var _ = mo;

Motherboard motherboard = new Motherboard
{
Manufacturer = GetPropertyString(mo["Manufacturer"]),
Expand All @@ -609,6 +641,8 @@ public List<Mouse> GetMouseList()

foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, queryString))
{
using var _ = mo;

Mouse mouse = new Mouse
{
Caption = GetPropertyString(mo["Caption"]),
Expand All @@ -632,6 +666,8 @@ public override List<NetworkAdapter> GetNetworkAdapterList(bool includeBytesPers

foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, queryString))
{
using var _ = mo;

NetworkAdapter networkAdapter = new NetworkAdapter
{
AdapterType = GetPropertyString(mo["AdapterType"]),
Expand All @@ -655,6 +691,8 @@ public override List<NetworkAdapter> GetNetworkAdapterList(bool includeBytesPers

foreach (IWmiPropertySource managementObject in _wmiQueryProvider.Query(_managementScope, query))
{
using var __ = managementObject;

networkAdapter.BytesSentPersec = GetPropertyValue<ulong>(managementObject["BytesSentPersec"]);
networkAdapter.BytesReceivedPersec = GetPropertyValue<ulong>(managementObject["BytesReceivedPersec"]);

Expand All @@ -671,6 +709,8 @@ public override List<NetworkAdapter> GetNetworkAdapterList(bool includeBytesPers

foreach (IWmiPropertySource configuration in _wmiQueryProvider.QueryRelated(_managementScope, mo, "Win32_NetworkAdapterConfiguration"))
{
using var __ = configuration;

foreach (string str in GetPropertyArray<string>(configuration["DefaultIPGateway"]))
if (IPAddress.TryParse(str, out address))
networkAdapter.DefaultIPGatewayList.Add(address);
Expand Down Expand Up @@ -706,6 +746,8 @@ public List<Printer> GetPrinterList()

foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, queryString))
{
using var _ = mo;

Printer printer = new Printer
{
Caption = GetPropertyString(mo["Caption"]),
Expand Down Expand Up @@ -733,6 +775,8 @@ public List<SoundDevice> GetSoundDeviceList()

foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, queryString))
{
using var _ = mo;

SoundDevice soundDevice = new SoundDevice
{
Caption = GetPropertyString(mo["Caption"]),
Expand All @@ -756,6 +800,8 @@ public List<VideoController> GetVideoControllerList()

foreach (IWmiPropertySource mo in _wmiQueryProvider.Query(_managementScope, queryString))
{
using var _ = mo;

VideoController videoController = new VideoController
{
Manufacturer = GetPropertyString(mo["AdapterCompatibility"]),
Expand Down Expand Up @@ -807,5 +853,10 @@ public List<VideoController> GetVideoControllerList()

return videoControllerList;
}

public void Dispose()
{
_wmiQueryProvider.Dispose();
}
}
}
2 changes: 1 addition & 1 deletion Hardware.Info.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static void Main(string[] _)

static void Test(bool test)
{
IHardwareInfo hardwareInfo = new HardwareInfo();
using IHardwareInfo hardwareInfo = new HardwareInfo();

hardwareInfo.RefreshOperatingSystem();
hardwareInfo.RefreshMemoryStatus();
Expand Down
2 changes: 2 additions & 0 deletions Hardware.Info/Windows/ManagementObjectAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ internal sealed class ManagementObjectAdapter : IWmiPropertySource
public object this[string propertyName] => _inner[propertyName];

public ManagementBaseObject GetManagementBaseObject() => _inner;

public void Dispose() => _inner.Dispose();
}
}
Loading