Nvapi在python3中如何使用?

问题描述 投票:0回答:1

您好,我正在尝试使用 ctypes 设置三个参数,

import ctypes

brightness = 52
contrast = 27
gamma = 1.40

def setBrightness(brightness):
    nvapi = ctypes.WinDLL("nvapi64.dll")
    handle = ctypes.c_uint()
    nvapi.NvAPI_Initialize()
    nvapi.NvAPI_GetPhysicalGPUsFromDisplay(ctypes.byref(handle))
    nvapi.NvAPI_GPU_SetBrightness(handle.value, ctypes.c_uint(brightness))
    nvapi.NvAPI_Unload()

def setContrast(contrast):
    nvapi = ctypes.WinDLL("nvapi64.dll")
    handle = ctypes.c_uint()
    nvapi.NvAPI_Initialize()
    nvapi.NvAPI_GetPhysicalGPUsFromDisplay(ctypes.byref(handle))
    nvapi.NvAPI_GPU_SetContrast(handle.value, ctypes.c_uint(contrast))
    nvapi.NvAPI_Unload()

def setGamma(gamma):
    nvapi = ctypes.WinDLL("nvapi64.dll")
    handle = ctypes.c_uint()
    nvapi.NvAPI_Initialize()
    nvapi.NvAPI_GetPhysicalGPUsFromDisplay(ctypes.byref(handle))
    nvapi.NvAPI_GPU_SetGamma(handle.value, ctypes.c_float(gamma))
    nvapi.NvAPI_Unload()

setBrightness(brightness)
setContrast(contrast)
setGamma(gamma)

但我得到:

  File "C:\Python38\lib\ctypes\__init__.py", line 391, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'NvAPI_Initialize' not found

我该如何解决这个问题?

我正在尝试设置参数。

python ctypes
1个回答
0
投票

我从未使用过nvidia gpu api,但从我所见,

nvapi64.dll
仅公开了2个功能:

  • nvapi_Direct_GetMethod
  • nvapi_QueryInterface

nvapi_QueryInterface
,我们可以通过它们的id来获取该函数。
NvAPI_GPU_SetBrightness
NvAPI_GPU_SetContrast
NvAPI_GPU_SetGamma
函数似乎不存在,所以这里通过一个简单的例子来展示如何调用
NvAPI_GetErrorMessage

from ctypes import (
    CFUNCTYPE,
    WinDLL,
    c_char_p,
    c_int,
    c_uint32,
    c_void_p,
    create_string_buffer,
)


def nvapi_errcheck(result, func, args):
    """
    Error checker for NVAPI functions.
    """
    NVAPI_OK = 0
    if result != NVAPI_OK:
        raise RuntimeError(f"NVAPI error in {func.__name__}. Error code: {result}")
    return result


nvapi = WinDLL("nvapi64")
nvapi_QueryInterface = nvapi.nvapi_QueryInterface
nvapi_QueryInterface.restype = c_void_p
nvapi_QueryInterface.argtypes = [c_uint32]

# From: https://docs.nvidia.com/gameworks/content/gameworkslibrary/coresdk/nvapi/group__nvapifunctions.html#ga773e227cecd41f303965a89446baa5e3
NVAPI_INITIALIZE_ID = 0x0150E828
ptr_NvAPI_Initialize = nvapi_QueryInterface(NVAPI_INITIALIZE_ID)
NvAPI_Initialize = CFUNCTYPE(c_int)(ptr_NvAPI_Initialize)
NvAPI_Initialize.errcheck = nvapi_errcheck

# From: https://docs.nvidia.com/gameworks/content/gameworkslibrary/coresdk/nvapi/group__nvapifunctions.html#gaac13a47b132d2aa6e2ca01731f9244e3
NVAPI_UNLOAD_ID = 0xD22BDD7E
ptr_NvAPI_Unload = nvapi_QueryInterface(NVAPI_UNLOAD_ID)
NvAPI_Unload = CFUNCTYPE(c_int)(ptr_NvAPI_Unload)
NvAPI_Unload.errcheck = nvapi_errcheck

# From: https://docs.nvidia.com/gameworks/content/gameworkslibrary/coresdk/nvapi/group__nvapifunctions.html#ga855150ebfbe69139b26a9e6dcd4da077
NVAPI_GETERRORMESSAGE_ID = 0x6C2D048C
ptr_NvAPI_GetErrorMessage = nvapi_QueryInterface(NVAPI_GETERRORMESSAGE_ID)
NvAPI_GetErrorMessage = CFUNCTYPE(c_int, c_int, c_char_p)(ptr_NvAPI_GetErrorMessage)
NvAPI_GetErrorMessage.errcheck = nvapi_errcheck


NvAPI_Initialize()

# From https://docs.nvidia.com/gameworks/content/gameworkslibrary/coresdk/nvapi/group__nvapitypes.html#gab2e7bd2e9e4ff1e12497fcd77a2d91b8
NVAPI_SHORT_STRING_MAX = 64
NvAPI_ShortString = create_string_buffer(NVAPI_SHORT_STRING_MAX)
NvAPI_GetErrorMessage(-2, NvAPI_ShortString)
print(NvAPI_ShortString.value.decode("utf-8"))  # print "NVAPI_LIBRARY_NOT_FOUND"

NvAPI_Unload()
© www.soinside.com 2019 - 2024. All rights reserved.