---------------------------- test.py ----------------------------
#! /usr/bin/env python
import ctypes
lib_handle = ctypes.CDLL('./libfun.so')
test = lib_handle.test
print test(5)
testA = lib_handle.testA
print testA(1, 3)
testB = lib_handle.testB
print testB('aaaaaaaaaaaaaaaaaaaaa')
testB.restype = ctypes.c_char_p
print testB('bbbbbbbbbbbbbbbbbbbbbbb')
class AA(ctypes.Structure):
_fields_=[("a", ctypes.c_int),("b", ctypes.c_int)]
aa = AA()
aa.a = 1
aa.b = 8
testC = lib_handle.testC
print testC(ctypes.byref(aa))
testD = lib_handle.testD
print testD(ctypes.byref(aa)), aa.a, aa.b
class BB(ctypes.Structure):
_fields_=[("a", ctypes.c_int),("pB", ctypes.c_char_p),("c", ctypes.c_int)]
bb = BB()
bb.a = 1
bb.pB = 'ssssssssssssssssssss'
bb.c = 2
testE = lib_handle.testE
testE.restype = ctypes.c_char_p
print testE(ctypes.byref(bb)), bb.a, bb.c
bb.pB = None
testF = lib_handle.testF
print testF(ctypes.byref(bb)), bb.a, bb.pB, bb.c
print lib_handle
|
------------------------ fun.h------------------
int test(int a);
int testA(int a, int b);
char *testB(char *p);
typedef struct _AA
{
int a;
int b;
}AA, *PAA;
int testC(AA *p);
int testD(AA *p);
typedef struct _BB
{
int a;
char *pB;
int c;
}BB, *PBB;
char *testE(BB *p);
int testF(BB *p);
|
--------------------------------fun.c -------------------------------
#include "fun.h"
int test(int a)
{
return a;
}
int testA(int a, int b)
{
return a + b;
}
char *testB(char *p)
{
return p;
}
int testC(AA *p)
{
return p->a + p->b;
}
int testD(AA *p)
{
int tmp = p->a;
p->a = p->b;
p->b = tmp;
return 0;
}
char *testE(BB *p)
{
int tmp = p->a;
p->a = p->c;
p->c = tmp;
return p->pB;
}
int testF(BB *p)
{
int tmp = p->a;
p->a = p->c;
p->c = tmp;
p->pB = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
return 0;
}
|
编译成so文件
gcc -shared -o libfun.so fun.c |
执行程序
阅读(2805) | 评论(0) | 转发(0) |