首页 > 解决方案 > 如何通过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 是什么

标签: pythoncctypes

解决方案


首先,导入 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

推荐阅读