2011-05-28 61 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