python - 如何通过ctype将字符串缓冲区从python函数传递给C Api

标签 python c ctypes

我有一个带有以下原型(prototype)的 C api 函数,我希望通过 ctypes 模块从 Python 2.6/2.7 调用它。

C 函数:

int function_name( char *outputbuffer, int outputBufferSize, 
                   const char *input, const char *somestring2 );

这里的 outputbuffer 是一种字符串缓冲区,一旦根据输入和 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

关于python - 如何通过ctype将字符串缓冲区从python函数传递给C Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65195605/

相关文章:

c - 将字符插入cstring

python - 当附加到另一个进程时,GetModuleHandleA 无法获取 python.exe 未使用的模块

python - 如何取消anyio.TaskGroup上下文中的任务?

python - 连接/处理脚本到 PySimpleGUI 按钮

c - 需要帮助在 Linux 中使用 C 语言应用定时器

c - 为什么在 C 中使用指针?

python - 你如何从 sip.voidptr (QImage.constBits()) 到 ctypes void 或 char 指针?

c++ - 从 ctypes 中的回调访问值

python - Dask one-hot 在不知道类别的情况下进行编码

python - Pandas :为groupby标识的每个组分配一个索引