通常结构体都继承自这几个类。python
BigEndianStructure, LittleEndianStructure,Structure
数组
而后属性名为_fields_
的集合,集合的单个元素是一个二元组。指针
python
代码from ctypes import * class Test(Structure): _fields_ = [("a",c_int),("b",c_int)] handle = CDLL("./test.dll") handle.test.restype = Test handle.test.argtypes= [c_int] ret = handle.test(2) print(ret.a,ret.b) """ 1 2 hello test2 [Finished in 0.1s] """
C++
代码#include<stdio.h> struct Test { int a; int b; Test(int x = 1,int y = 2) { a = x; b = y; } }; extern "C" Test test(int n) { printf("hello test%d\n",n); return Test(); }
g++ -shared -fPIC -m32 -o test.dll test.cpp
结构体指针 byref(struct),生成一个指针,通常用做传入参数。rest
pointer(struct),生成一个指针,这个是用来直接生成的,相似 int* = (&int_var).经常使用于生成一个指针code
POINTER(type)(init_value),经常使用语生成一个数组。继承
生成数组长度为1的指针POINTER(c_int)(c_int(0))it
生成数组长度为n的指针POINTER(c_int)( (c_int*n)()),类比C++的POINTER<c_int>(new c_int[n]())io
经过属性_pack_
设置,和#param pack()
同样。正整数。编译