这是我之前问题的后续:有关 python ctypes 和 nvapi 的帮助 https://stackoverflow.com/questions/6163770/help-with-python-ctypes-and-nvapi
我正在发布另一个问题,因为我已经在上一个问题中选择了答案,并且问题现在已经改变了方向。
使用 python,我尝试查询 nvapi.dll 以获取我的 GPU 使用值。
这是在 C# 中执行此操作的方法(如下引用):http://code.google.com/p/open-hardware-monitor/source/browse/trunk/Hardware/Nvidia/NVAPI.cs http://code.google.com/p/open-hardware-monitor/source/browse/trunk/Hardware/Nvidia/NVAPI.cs
因为我只需要一小部分功能,所以我真的不想换成使用另一种语言。我认为 ctypes 应该能够做到这一点......我只是不知道如何做到这一点。我以前从未真正使用过Python ctypes。
为了查询 GPU 使用情况,我首先需要调用 NvAPI_Initialize 函数。还必须使用查询接口来引用该函数:
from ctypes import *
nvapi = WinDLL("nvapi.dll")
nvapi_QueryInterface = nvapi.nvapi_QueryInterface
使用上面的代码,我可以访问 nvapi_QueryInterface,但我不知道如何复制这部分:
private static void GetDelegate<T>(uint id, out T newDelegate)
where T : class
{
IntPtr ptr = nvapi_QueryInterface(id);
if (ptr != IntPtr.Zero)
{
newDelegate =
Marshal.GetDelegateForFunctionPointer(ptr, typeof(T)) as T;
}
else
{
newDelegate = null;
}
}
引用 init 和 use 函数:
GetDelegate(0x0150E828, out NvAPI_Initialize);
GetDelegate(0x189A1FDF, out NvAPI_GPU_GetUsages);
我对此的理解可能仍然是错误的,但我花了很多时间试图弄清楚这一点,所以我希望有人可以帮助我朝正确的方向前进,因为我对如何进行有点迷失。
有人可以帮助我理解如何使这一小部分代码直接在 python 中工作吗?我只需要能够在一天结束时调用 NvAPI_GPU_GetUsages 函数。
Thanks.