python - 如何通过ctype将字符串缓冲区从python函数传递到C Api
问题描述
我有一个带有以下原型的 C api 函数,我希望通过 ctypes 模块从 Python 2.6/2.7 调用它。
C函数:
int function_name( char *outputbuffer, int outputBufferSize,
const char *input, const char *somestring2 );
这里的 outputbuffer 是一种字符串缓冲区,一旦根据 input 和 somestring2 调用此函数,就会在其中插入一个字符串作为输出。
我们如何在 python 中创建这个缓冲区(输出缓冲区)以及这个函数的 argtype 是什么
解决方案
首先,导入 ctypes 模块。然后,您需要加载包含上述函数的 c dll。之后,您需要设置该函数的参数类型和结果类型。最后,您创建目标缓冲区并调用您的函数。
Python:
import ctypes as ct
MAX_BUFSIZE = 100
mycdll = ct.CDLL("path_to_your_c_dll") # for windows you use ct.WinDLL(path)
mycdll.function_name.argtypes = [ct.c_char_p, ct.c_int,
ct.c_char_p, ct.c_char_p]
mycdll.function_name.restype = ct.c_int
mystrbuf = ct.create_string_buffer(MAX_BUFSIZE)
result = mycdll.function_name(mystrbuf, len(mystrbuf),
b"my_input", b"my_second_input")
使用 strncpy 的工作示例:
import ctypes as ct
MAX_BUFSIZE = 100
mycdll = ct.CDLL("libc.so.6") # on windows you use cdll.msvcrt, instead
mycdll.strncpy.argtypes = [ct.c_char_p, ct.c_char_p, ct.c_size_t]
mycdll.strncpy.restype = ct.c_char_p
mystrbuf = ct.create_string_buffer(MAX_BUFSIZE)
dest = mycdll.strncpy(mystrbuf, b"my_input", len(mystrbuf))
print(mystrbuf.value)
Python 3 输出:
user@Mint20:~/Dokumente/Programmieren/sites/Stackoverflow$ python3 --version
Python 3.8.5
user@Mint20:~/Dokumente/Programmieren/sites/Stackoverflow$ python3 python_ctypes.py
b'my_input'
Python 2 输出:
user@Mint20:~/Dokumente/Programmieren/sites/Stackoverflow$ python2 --version
Python 2.7.18
user@Mint20:~/Dokumente/Programmieren/sites/Stackoverflow$ python2 python_ctypes.py
my_input
推荐阅读
- react-native - Metro 配置不接受阻止列表
- python - 动态小部件放置
- c++ - C++20 自动模板参数未按预期工作(GCC 错误?)
- sql - 具有有限列数的枢轴
- java - 随机快速排序 IndexOutOfBounds 异常
- java - 如何使用 YouTube Java API 通过主题标签获取随机视频
- sql - 在两个查询之间插入批处理语句差异
- .net-core - netcore 3.1 中的迁移不能在使用 MigrationsAssembly 的不同项目中工作
- sql-server - 我正在尝试将以下 SQL 存储过程转换为 Snowflake。但我还找不到 EXEC 语句的替代方法:
- python - 文件名上的 Spyder IDE 字体太大