我正在为 Python 的 C 代码创建一个包装器。C代码基本上在终端中运行,具有以下主要函数原型:
void main(int argc, char *argv[]){
f=fopen(argv[1],"r");
f2=fopen(argv[2],"r");
Run Code Online (Sandbox Code Playgroud)
所以基本上读取的参数是终端中的字符串。我创建了以下 python ctype 包装器,但看来我使用了错误的类型。我知道从终端传递的参数被读取为字符,但等效的 python 侧包装器给出以下错误:
import ctypes
_test=ctypes.CDLL('test.so')
def ctypes_test(a,b):
_test.main(ctypes.c_char(a),ctypes.c_char(b))
ctypes_test("323","as21")
TypeError: one character string expected
Run Code Online (Sandbox Code Playgroud)
我尝试添加一个字符,只是为了检查共享对象是否被执行,它与打印命令一样工作,但暂时直到共享对象中的代码部分需要文件名为止。我也尝试过
ctypes.c_char_p但是得到了。
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
Run Code Online (Sandbox Code Playgroud)
根据评论中的建议更新为以下内容:
def ctypes_test(a,b):
_test.main(ctypes.c_int(a),ctypes.c_char_p(b))
ctypes_test(2, "323 as21")
Run Code Online (Sandbox Code Playgroud)
但遇到同样的错误。
使用适用于 Windows 的测试 DLL:
#include <stdio.h>
__declspec(dllexport)
void main(int argc, char* argv[])
{
for(int i = 0; i < argc; ++i)
printf("%s\n", argv[i]);
}
Run Code Online (Sandbox Code Playgroud)
这段代码会调用它。 argv在 C 中基本上是 a char**,所以ctypes类型是POINTER(c_char_p). 您还必须传递字节字符串,并且它不能是 Python 列表。它必须是一个ctypes指针数组。
>>> from ctypes import *
>>> dll = CDLL('./test')
>>> dll.main.restype = None
>>> dll.main.argtypes = c_int, POINTER(c_char_p)
>>> args = (c_char_p * 3)(b'abc', b'def', b'ghi')
>>> dll.main(len(args), args)
abc
def
ghi
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1736 次 |
| 最近记录: |