OpenHardwareMonitorLib.h该文件的代码
时间: 2024-03-21 19:44:14 浏览: 16
以下是OpenHardwareMonitorLib.h文件的代码示例:
```csharp
using System;
using System.Runtime.InteropServices;
namespace OpenHardwareMonitor.Hardware
{
public enum SensorType
{
Voltage,
Clock,
Temperature,
Load,
Fan,
Flow,
Control,
Level,
Factor,
Power,
Data,
SmallData,
Throughput,
ClockMultiplier,
Unknown
}
public enum HardwareType
{
Mainboard,
SuperIO,
CPU,
RAM,
GpuNvidia,
GpuAti,
TBalancer,
Heatmaster,
HDD,
SSD,
LCDScreen,
Fans,
Pump,
TEC,
Generic,
Unknown
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct Sensor
{
public string Name;
public string Identifier;
public float Value;
public SensorType SensorType;
public Hardware Hardware;
public int Index;
public bool IsDefaultHidden;
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct Hardware
{
public string Name;
public string Identifier;
public HardwareType HardwareType;
public int Index;
public IHardware[] SubHardware;
public ISensor[] Sensors;
}
public interface IComputer
{
string GetReport();
IHardware[] Hardware { get; }
void Traverse(Action<IHardware> action);
void Traverse(Action<IHardware, IHardware> action);
void Open();
void Close();
bool IsOpen { get; }
}
public interface IHardware
{
string Name { get; }
string Identifier { get; }
HardwareType HardwareType { get; }
IHardware[] SubHardware { get; }
ISensor[] Sensors { get; }
void Traverse(Action<IHardware> action);
void Traverse(Action<IHardware, IHardware> action);
void Update();
int Index { get; }
}
public interface ISensor
{
string Name { get; }
string Identifier { get; }
float Value { get; }
SensorType SensorType { get; }
IHardware Hardware { get; }
int Index { get; }
bool IsDefaultHidden { get; }
}
public interface ISettings
{
bool? GetValue(string id);
void SetValue(string id, bool value);
}
public class UpdateVisitor : IVisitor
{
public void VisitComputer(IComputer computer)
{
computer.Traverse(new Action<IHardware>(VisitHardware));
}
public void VisitHardware(IHardware hardware)
{
hardware.Update();
}
public void VisitSensor(ISensor sensor) { }
public void VisitParameter(IParameter parameter) { }
public void VisitIdentifier(IIdentifier identifier) { }
}
public class Computer : IComputer
{
private readonly UpdateVisitor updateVisitor = new UpdateVisitor();
private readonly IdentifierVisitor identifierVisitor = new IdentifierVisitor();
private readonly HardwareVisitor hardwareVisitor = new HardwareVisitor();
private readonly SensorVisitor sensorVisitor = new SensorVisitor();
private readonly ParameterVisitor parameterVisitor = new ParameterVisitor();
private ISettings settings;
private readonly IHardware[] hardware;
public Computer()
{
hardware = new IHardware[0];
}
public Computer(ISettings settings)
{
this.settings = settings;
IntPtr ptr = IntPtr.Zero;
try
{
ptr = OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.Initialize();
hardware = CreateHardware(ptr);
}
finally
{
if (ptr != IntPtr.Zero)
OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.Close(ptr);
}
}
public IHardware[] Hardware
{
get { return hardware; }
}
public void Open()
{
IntPtr ptr = IntPtr.Zero;
try
{
ptr = OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.Initialize();
hardwareVisitor.Reset();
OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.Update(ptr);
hardwareVisitor.VisitComputerNode(ptr);
}
finally
{
if (ptr != IntPtr.Zero)
OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.Close(ptr);
}
}
public void Close() { }
public bool IsOpen
{
get { return true; }
}
public string GetReport()
{
IntPtr ptr = IntPtr.Zero;
try
{
ptr = OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.Initialize();
return Marshal.PtrToStringAnsi(OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.GetReport(ptr));
}
finally
{
if (ptr != IntPtr.Zero)
OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.Close(ptr);
}
}
public void Traverse(Action<IHardware> action)
{
foreach (IHardware hw in hardware)
action(hw);
}
public void Traverse(Action<IHardware, IHardware> action)
{
foreach (IHardware hw in hardware)
Traverse(hw, action);
}
private void Traverse(IHardware hardware, Action<IHardware, IHardware> action)
{
foreach (IHardware subHardware in hardware.SubHardware)
{
action(hardware, subHardware);
Traverse(subHardware, action);
}
}
private IHardware[] CreateHardware(IntPtr ptr)
{
hardwareVisitor.Reset();
OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.TraverseComputer(ptr, hardwareVisitor, IntPtr.Zero);
return hardwareVisitor.Hardware;
}
}
internal class HardwareVisitor : IVisitor
{
private readonly IdentifierVisitor identifierVisitor = new IdentifierVisitor();
private readonly SensorVisitor sensorVisitor = new SensorVisitor();
private readonly Hardware root = new Hardware();
private Hardware current;
public HardwareVisitor()
{
current = root;
}
public Hardware[] Hardware
{
get { return root.SubHardware; }
}
public void Reset()
{
current = root;
root.SubHardware = new IHardware[0];
}
public void VisitComputerNode(IntPtr data)
{
VisitHardwareNode(data);
}
public void VisitHardwareNode(IntPtr data)
{
IntPtr ptr = Marshal.ReadIntPtr(data);
Hardware hardware = new Hardware
{
Name = Marshal.PtrToStringAnsi(OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.GetHardwareName(ptr)),
Identifier = identifierVisitor.GetIdentifier(ptr),
HardwareType = OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.GetHardwareType(ptr),
Index = OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.GetHardwareIndex(ptr)
};
current.SubHardware = ArrayExtensions.Add(current.SubHardware, hardware);
current = hardware;
OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.TraverseSubtree(data, this, data);
sensorVisitor.Reset();
OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.TraverseSensors(data, sensorVisitor, data);
current.Sensors = sensorVisitor.Sensors;
}
public void VisitSensorNode(IntPtr data)
{
throw new NotImplementedException();
}
public void VisitParameterNode(IntPtr data)
{
throw new NotImplementedException();
}
public void VisitParameter(IntPtr parameter)
{
throw new NotImplementedException();
}
}
internal class IdentifierVisitor
{
public string GetIdentifier(IntPtr ptr)
{
IntPtr identifierPtr = OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.GetHardwareIdentifier(ptr);
return identifierPtr == IntPtr.Zero ? null : Marshal.PtrToStringAnsi(identifierPtr);
}
}
internal class SensorVisitor : IVisitor
{
private readonly IdentifierVisitor identifierVisitor = new IdentifierVisitor();
private readonly System.Collections.Generic.List<ISensor> sensors = new System.Collections.Generic.List<ISensor>();
public ISensor[] Sensors
{
get { return sensors.ToArray(); }
}
public void Reset()
{
sensors.Clear();
}
public void VisitComputerNode(IntPtr data)
{
throw new NotImplementedException();
}
public void VisitHardwareNode(IntPtr data)
{
throw new NotImplementedException();
}
public void VisitSensorNode(IntPtr data)
{
IntPtr ptr = Marshal.ReadIntPtr(data);
sensors.Add(new Sensor
{
Name = Marshal.PtrToStringAnsi(OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.GetSensorName(ptr)),
Value = OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.GetSensorValue(ptr),
SensorType = OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.GetSensorType(ptr),
Identifier = identifierVisitor.GetIdentifier(ptr),
Hardware = new Hardware { Identifier = identifierVisitor.GetIdentifier(OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.GetSensorParent(ptr)) },
Index = OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.GetSensorIndex(ptr),
IsDefaultHidden = OpenHardwareMonitor.Interop.OpenHardwareMonitorLib.GetSensorIsDefaultHidden(ptr)
});
}
public void VisitParameterNode(IntPtr data)
{
throw new NotImplementedException();
}
public void VisitParameter(IntPtr parameter)
{
throw new NotImplementedException();
}
}
internal class ParameterVisitor : IVisitor
{
public void VisitComputerNode(IntPtr data)
{
throw new NotImplementedException();
}
public void VisitHardwareNode(IntPtr data)
{
throw new NotImplementedException();
}
public void VisitSensorNode(IntPtr data)
{
throw new NotImplementedException();
}
public void VisitParameterNode(IntPtr data)
{
throw new NotImplementedException();
}
public void VisitParameter(IntPtr parameter)
{
throw new NotImplementedException();
}
public void Dispose()
{
throw new NotImplementedException();
}
}
}
```
该代码定义了一些枚举类型和接口,并实现了硬件监视器的基本功能,包括读取并解析硬件和传感器信息。请注意,该代码涉及一些C#语言的特性和语法,需要在支持C#编译的IDE或环境中使用。