这是“Testlib.c”中的C代码。我只是从C函数返回一个指向结构的指针。并且所有C代码都将编译为lib,以便由python调用。
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int num;
char c;
}aStruct;
aStruct* newStructPointer()
{
aStruct* s = (aStruct*)malloc(sizeof(aStruct)) ;
s->num = 3;
s->c = 'f';
return s;
}
int getNumField(aStruct* a)
{
return a->num;
}
char getCharField(aStruct* a)
{
return a->c;
}
这是Python代码。这很简单,调用newStructPointer来获取一个新结构,然后在其字段中获取数字值。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from ctypes import *
class testlib(object):
def __init__(self):
lib=cdll.LoadLibrary("libtestdylib.dylib")
s=lib.newStructPointer()
num=lib.getNumField(cast(s, c_void_p))
t=testlib()
然后,它崩溃,因为指针已经无效,这是崩溃报告:
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libtestdylib.dylib 0x000000010638ef5c getNumField + 12 (interface.c:26)
1 libffi.dylib 0x00007fff9354ef9c ffi_call_unix64 + 76
2 libffi.dylib 0x00007fff9354f78e ffi_call + 790
3 _ctypes.so 0x000000010637390e _ctypes_callproc + 794
4 _ctypes.so 0x000000010636df80 0x10636b000 + 12160
5 org.python.python 0x0000000106085f72 PyObject_Call + 101
6 org.python.python 0x00000001060ffdf5 PyEval_EvalFrameEx + 15416
7 org.python.python 0x00000001060fc093 PyEval_EvalCodeEx + 1641
8 org.python.python 0x00000001060a3796 0x10607c000 + 161686
9 org.python.python 0x0000000106085f72 PyObject_Call + 101
10 org.python.python 0x00000001060909a7 0x10607c000 + 84391
11 org.python.python 0x0000000106085f72 PyObject_Call + 101
12 org.python.python 0x00000001060cb6ce 0x10607c000 + 325326
13 org.python.python 0x00000001060c7184 0x10607c000 + 307588
14 org.python.python 0x0000000106085f72 PyObject_Call + 101
15 org.python.python 0x00000001060ffdf5 PyEval_EvalFrameEx + 15416
16 org.python.python 0x00000001060fc093 PyEval_EvalCodeEx + 1641
17 org.python.python 0x00000001060fba24 PyEval_EvalCode + 54
18 org.python.python 0x000000010611ac2c 0x10607c000 + 650284
19 org.python.python 0x000000010611acd3 PyRun_FileExFlags + 137
20 org.python.python 0x000000010611a821 PyRun_SimpleFileExFlags + 718
21 org.python.python 0x000000010612b363 Py_Main + 2995
22 libdyld.dylib 0x00007fff8e4215fd start + 1
我已经阅读了https://docs.python.org/2/library/ctypes.html中的python文档,但我发现没有任何帮助。我该如何解决?
答案 0 :(得分:2)
您必须正确定义函数原型才能安全地调用函数:
# define your structure
class aStruct(ctypes.Structure):
_fields_ = [('num', ctypes.c_int),
('c', ctypes.c_char]
# this defines aStructPtr to be a pointer
# to an aStruct
aStructPtr = ctypes.POINTER(aStruct)
# next, define the interface to your functions:
lib.newStructPointer.restype = aStructPtr
lib.newStructPointer.argtypes = [] # void, no arguments
# your getter functions both take the structure pointer as inputs
# and return an integer or char
lib.getNumField.restype = ctypes.c_int
lib.getNumField.argtypes = [aStructPtr]
lib.getCharField.restype = ctypes.c_char
lib.getCharField.argtypes = [aStructPtr]
请注意,通过在ctypes中正确声明结构,您不需要c getter函数。以下内容也应该有效:
s = lib.newStructPointer()
num_value = s.contents.num
char_value = s.contents.c