gpt4 book ai didi

python - 从 Python 调用 DLL 函数

转载 作者:太空狗 更新时间:2023-10-30 00:14:11 26 4
gpt4 key购买 nike

我想从 Python 调用 DLL 中的函数。但是我收到了这个错误:

"Attribute Error function not found"

这是我的代码:

import os
import ctypes


os.chdir("C:\\Program Files\\Compact Automated Testing System V2.0")

# Load DLL into memory.
CATSDll = ctypes.WinDLL ("CATS.dll")

# Set up prototype and parameters for the desired function call.
CATSDllApiProto = ctypes.WINFUNCTYPE (ctypes.c_uint8,ctypes.c_double)

CATSDllApiParams = (1, "p1", 0), (1, "p2", 0),

# Actually map the call (setDACValue) to a Python name.
CATSDllApi = CATSDllApiProto (("setDACValue", CATSDll), CATSDllApiParams)

# Set up the variables and call the Python name with them.
p1 = ctypes.c_uint8 (1)
p2 = ctypes.c_double (4)

CATSDllApi(p1,p2)

但是 DLL 文档显示了一个函数 setDACValue,其中 ChannelIdDAC Voltage 作为输入。

以上内容基于 StackOverflow 提供的一段代码。

我也尝试过使用 cdll.LoadLibrary 然后调用该函数的简单方法,但这也给出了同样的错误。

谁能告诉我哪里出了问题?谢谢。

完整回溯:

Traceback (most recent call last):
File "C:\Users\AEC_FULL\Softwares\eclipse\plugins\org.python.pydev_3.9.2.201502050007\pysrc\pydevd.py", line 2235, in <module>
globals = debugger.run(setup['file'], None, None)
File "C:\Users\AEC_FULL\Softwares\eclipse\plugins\org.python.pydev_3.9.2.201502050007\pysrc\pydevd.py", line 1661, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Users\AEC_FULL\Saravanan\Workspace\CATS\CATS.py", line 18, in <module>
CATSDllApi = CATSDllApiProto (("setDACValue", CATSDll), CATSDllApiParams)
AttributeError: function 'setDACValue' not found

Signature of the setDACValue

最佳答案

当你指定函数的原型(prototype)时,你不仅应该指定参数的类型,还应该指定返回类型作为 WINFUNCTYPE 的第一个参数。 .因此,行

CATSDllApiProto = ctypes.WINFUNCTYPE (ctypes.c_uint8,ctypes.c_double)

应替换为

CATSDllApiProto = ctypes.WINFUNCTYPE (ctypes.c_uint8, ctypes.c_uint8,ctypes.c_double)

关于python - 从 Python 调用 DLL 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29824777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com