2011-05-28 63 views
4

我的最终目标是在python中查询NVAPI的gpu使用情况和其他统计信息。见http://developer.nvidia.com/nvapi帮助python ctypes和nvapi

from ctypes import WinDLL 
nvapi = WinDLL("nvapi.dll") 
print nvapi# <WinDLL 'nvapi.dll', handle 718a0000 at 27c0050> 
print nvapi.nvapi_QueryInterface# <_FuncPtr object at 0x026D8E40> 
print nvapi.nvapi_QueryInterface()# returns 0 
print nvapi.NvAPI_Initialize# AttributeError: function 'NvAPI_Initialize' not found 
print nvapi.NvAPI_SYS_GetChipSetInfo# AttributeError: function 'NvAPI_SYS_GetChipSetInfo' not found 

这是可以从上面的链接下载头文件的副本:http://paste.pound-python.org/show/7337/

在这一点上,我只是想用API来自己熟悉...所以我是什么我做错了?我无法弄清楚如何调用头文件中列出的任何函数。

+0

我在这里发布了一个后续问题:http://stackoverflow.com/questions/6165628/use-python-ctypes-to-interface-with-nvapi-follow-up-with-demonstration-code – user319862 2011-05-29 02:32:30

回答

0

从您的评论摘自:

NvAPI_Initialize call still fails. saying the function is not found.

NvAPI_Initialize没有从动态库nvapi.dll出口。它是NVIDIA®(英伟达™)SDK附带的静态库nvapi.lib中包含的一个符号,因此您无法使用Python调用它。

老实说,这里最简单的路线是在C中创建一个小的封装器DLL,静态链接到nvapi.lib并向Python公开一个友好的界面。

+0

doh。谢谢。我会给那一枪 – user319862 2011-05-28 22:51:36

2

你确定这是一个WinDLL吗?从头文件看,它对我来说看起来像一个标准的C调用惯例。您是否尝试过CDLL

编辑

我现在看到。您指向的标题实际上并不是nvapi.dll的界面 - 它是一个必须静态链接的包装。

从从NVIDIA's developer site下载文档:

Use a Static Link with Applications

NvAPI cannot be dynamically linked to applications. You must create a static link to the library and then call NvAPI_Initialize(), which loads nvapi.dll dynamically.

If the NVIDIA drivers are not installed on the system or nvapi.dll is not present when the application calls NvAPI_Initialize(), the call just returns an error. The application will still load.

我猜想,在nvapi.dll实际调用比这个包装库暴露的那些完全不同。我似乎无法找到任何有关这些文件。也许他们是内部的,并在系统之间改变。

如果你想使用这个接口,我不确定最好的解决方案是什么。它是一个静态库,不是一个动态库,所以ctypes不会处理它,除非你将它包装在另一个DLL中。我不是Python本地代码的专家,所以也许别人会有一个简单的解决方法。抱歉。

+0

我试过CDLL也是如此。它也不起作用。 <_FuncPtr在0x02608EB8对象> 但随后AttributeError的错误:函数 'NvAPI_Initialize' 未找到 – user319862 2011-05-28 21:36:44

+0

我的格式得到了全乱了遗憾。前三个打印工作并显示类似的输出,但NvAPI_Initialize调用仍然失败。说该功能没有找到。 – user319862 2011-05-28 21:38:34

+0

@ user319862:请参阅编辑。 – 2011-05-28 22:22:19