来自 dll 问题的 Python Ctypes 函数调用

标签 python c dll ctypes

我需要使用python来计算dll函数。

功能是:

int cbGetConfigString(int InfoType, int BoardNum, int DevNum, int ConfigItem, char *ConfigVal, int* maxConfigLen)

参数

信息类型

The configuration information for each board is grouped into different categories. This argument specifies which category you want. Always set this argument to BOARDINFO.

板号

The number associated with the board when it was installed with InstaCal or created with cbCreateDaqDevice(). BoardNum may be 0 to 99.

DevNum

The purpose of the DevNum argument depends on the ConfigItem being passed. It can serve as a point at which to start the read, an index into the ConfigItem, or it can be ignored.

配置项

The type of information to read from the device. Set it to one of the constants listed in the "ConfigItem argument values" section below.

配置值

Pointer to a user-allocated buffer where the configuration string is copied.

ma​​xConfigLen

Pointer to the value holding the maximum number of bytes to be read from the device into ConfigVal.

请帮助我理解我的代码中有什么问题:

import ctypes

mccdaq=ctypes.WinDLL("C:\\Program Files (x86)\\Measurement Computing\\DAQ\\cbw32.dll")

Open=mccdaq.cbGetConfigString
Open.argtypes =[ctypes.c_int,ctypes.c_int,ctypes.c_int,ctypes.c_int,ctypes.POINTER(ctypes.c_char),ctypes.POINTER(ctypes.c_int)]

INTP=ctypes.POINTER(ctypes.c_int)
num=ctypes.c_int(8)
addr=ctypes.addressof(num)
ptr=ctypes.cast(addr,INTP)

mem=ctypes.c_char()

print Open(2,0,0,363,ctypes.byref(mem),ctypes.byref(num))
print mem
print num

输出:

0 - 没有错误

c_char('1') - 应为 125E737

c_long(7) - 看起来字符数正确..

最佳答案

好的,我发现了问题:)

import ctypes

mccdaq=ctypes.WinDLL("C:\\Program Files (x86)\\Measurement Computing\\DAQ\\cbw32.dll")

Open=mccdaq.cbGetConfigString
Open.argtypes =[ctypes.c_int,ctypes.c_int,ctypes.c_int,ctypes.c_int,ctypes.POINTER(ctypes.c_char*8),ctypes.POINTER(ctypes.c_int)]

num=ctypes.c_int(8)
mem=(ctypes.c_char*8)()

print Open(2,0,0,363,ctypes.byref(mem),ctypes.byref(num))
print mem.value
print num   

现在的输出是:

0

125E737

c_long(7)

关于来自 dll 问题的 Python Ctypes 函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41041686/

相关文章:

python - pandas 按 n 秒分组并应用任意滚动功能

python - django admin - 编辑或查看模型的组权限

python - 如何将 theano.tensor 转换为 numpy.array?

c - 在 Qt Creator 中启用 C99

c - 从 Windows 内部修改 SMBIOS

c++ - 使用 list 的 DLL 重定向

c++ - 更改dll对windows的依赖

python请求错误415

c - 为什么 Linux 机器上的堆栈大小几乎总是 132KiB?

c# - 检查系统中是否存在 DLL