OpenHardwareMonitorLib.h该文件的代码

时间: 2024-03-21 10:44:14 浏览: 32
以下是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或环境中使用。

相关推荐

最新推荐

recommend-type

ASP某书店图书销售管理系统的设计与实现(源代码+论文)【ASP】.zip

ASP某书店图书销售管理系统的设计与实现(源代码+论文)【ASP】
recommend-type

施工混凝土配合比动态管理台账(新样板版).xls

施工混凝土配合比动态管理台账(新样板版).xls
recommend-type

Lua移植到标准ANSI C环境

Lua移植到标准ANSI C环境
recommend-type

施工混凝土配合比动态管理台账.docx

施工混凝土配合比动态管理台账.docx
recommend-type

TwinCAT3 入门教程 V4.11

TwinCAT3 入门教程 V4.11
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

优化MATLAB分段函数绘制:提升效率,绘制更快速

![优化MATLAB分段函数绘制:提升效率,绘制更快速](https://ucc.alicdn.com/pic/developer-ecology/666d2a4198c6409c9694db36397539c1.png?x-oss-process=image/resize,s_500,m_lfit) # 1. MATLAB分段函数绘制概述** 分段函数绘制是一种常用的技术,用于可视化不同区间内具有不同数学表达式的函数。在MATLAB中,分段函数可以通过使用if-else语句或switch-case语句来实现。 **绘制过程** MATLAB分段函数绘制的过程通常包括以下步骤: 1.
recommend-type

SDN如何实现简易防火墙

SDN可以通过控制器来实现简易防火墙。具体步骤如下: 1. 定义防火墙规则:在控制器上定义防火墙规则,例如禁止某些IP地址或端口访问,或者只允许来自特定IP地址或端口的流量通过。 2. 获取流量信息:SDN交换机会将流量信息发送给控制器。控制器可以根据防火墙规则对流量进行过滤。 3. 过滤流量:控制器根据防火墙规则对流量进行过滤,满足规则的流量可以通过,不满足规则的流量则被阻止。 4. 配置交换机:控制器根据防火墙规则配置交换机,只允许通过满足规则的流量,不满足规则的流量则被阻止。 需要注意的是,这种简易防火墙并不能完全保护网络安全,只能起到一定的防护作用,对于更严格的安全要求,需要
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。