如何使用Libre Hardware Monitor库读取GPU传感器

时间:2019-03-02 20:09:41

标签: c# sensor nvapi openhardwaremonitor

我正在使用Libre Hardware Monitor库编写一个简单的C#应用​​程序。除GPU传感器外,其他一切正常。如果将GPUEnabled设置为“ true”,则会收到以下消息:

Managed Debugging Assistant 'PInvokeStackImbalance' : 
'A call to PInvoke function'OpenHardwareMonitorLib!OpenHardwareMonitor.Hardware.
Nvidia.NVAPI+NvAPI_GetInterfaceVersionStringDelegate::Invoke' has unbalanced the stack. 
This is likely because the managed PInvoke signature does not match the unmanaged target
signature. Check that the calling convention and parameters of 
the PInvoke signature match the target unmanaged signature.'

在NVAPI.cs 335行中

public static NvStatus NvAPI_GetInterfaceVersionString(out string version) {
      StringBuilder builder = new StringBuilder(SHORT_STRING_MAX);
      NvStatus status;
      if (_NvAPI_GetInterfaceVersionString != null)
/*335*/ status = _NvAPI_GetInterfaceVersionString(builder);
      else
        status = NvStatus.FUNCTION_NOT_FOUND;
      version = builder.ToString();
      return status;
    }

使用Libre Hardware Monitor,一切正常。
GPU:ASUS-ROG-STRIX-RTX2080-O8G-GAMING
我的代码(主要来自here):

Computer thisPC;

public EMSSettings()
{
    InitializeComponent();

    thisPC = new Computer()
    {
        GPUEnabled = true
    };

    thisPC.Open();
}

private void timer1_Tick(object sender, EventArgs e)
{

    String temp = "";

    foreach (var hardwareItem in thisPC.Hardware)
    {
        if (hardwareItem.HardwareType == HardwareType.CPU || true)
        {
            hardwareItem.Update();
            foreach (IHardware subHardware in hardwareItem.SubHardware)
                subHardware.Update();

            foreach (var sensor in hardwareItem.Sensors)
            {
                if (sensor.SensorType == SensorType.Temperature || true)
                {

                    temp += String.Format("{0} Temperature = {1}\r\n", sensor.Name, sensor.Value.HasValue ? sensor.Value.Value.ToString() : "no value");

                }
            }
        }
    }

    textBox1.Text = temp;

}

感谢所有答案

1 个答案:

答案 0 :(得分:0)

答案在这里:https://stackoverflow.com/a/39203588/9355850
在每个代表之前添加##-- Fake lists l1 <- as.list(1:5) names(l1) <- 1:5 l2 <- as.list(2:5) names(l2) <- 2:5 ##-- Common names and subsetting common_names <- intersect(names(l1), names(l2)) l1 <- l1[common_names] l2 <- l2[common_names]