我正在使用 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
我的代码(主要来自这里):
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;
}
感谢所有的答案