Note that there are some explanatory texts on larger screens.

plurals
  1. POuse python ctypes to interface with nvapi (follow up with demonstration code)
    primarykey
    data
    text
    <p>This is a follow up to my previous question: <a href="https://stackoverflow.com/questions/6163770/help-with-python-ctypes-and-nvapi">help with python ctypes and nvapi</a></p> <p>I am posting another question because I already selected an answer in my previous question and the question has shifted direction now.</p> <p>Using python, I am trying to query nvapi.dll to get my gpu usage value. </p> <p>This is how you would do it in C# (referenced below): <a href="http://code.google.com/p/open-hardware-monitor/source/browse/trunk/Hardware/Nvidia/NVAPI.cs" rel="nofollow noreferrer">http://code.google.com/p/open-hardware-monitor/source/browse/trunk/Hardware/Nvidia/NVAPI.cs</a></p> <p>Because I only need a small subset of the functionality I don't really want to swap into using another language. I think that ctypes should be able to do this... I just cannot figure out how to do it. I have never really used Python ctypes before.</p> <p>In order to query for the gpu usage I first need to call the NvAPI_Initialize function. One must also use the query interface to reference the function:</p> <pre><code>from ctypes import * nvapi = WinDLL("nvapi.dll") nvapi_QueryInterface = nvapi.nvapi_QueryInterface </code></pre> <p>Using the above code, I have access to the nvapi_QueryInterface, but I cannot figure out how to replicate this portion:</p> <pre><code>private static void GetDelegate&lt;T&gt;(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; } } </code></pre> <p>to reference the init and usage functions:</p> <pre><code>GetDelegate(0x0150E828, out NvAPI_Initialize); GetDelegate(0x189A1FDF, out NvAPI_GPU_GetUsages); </code></pre> <p>My understanding of this may still be wrong, but I've spent a good deal of time trying to figure this out so I hope someone can help push me in the right direction because I am a little lost as to how to proceed.</p> <p>Can someone help me understand how I can make this small portion of the code work directly in python? I just need to be able to call the NvAPI_GPU_GetUsages function at the end of the day.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload