Chinaunix首页 | 论坛 | 博客
  • 博客访问: 755159
  • 博文数量: 265
  • 博客积分: 6010
  • 博客等级: 准将
  • 技术积分: 1985
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-13 12:33
文章分类

全部博文(265)

文章存档

2011年(1)

2010年(66)

2009年(198)

我的朋友

分类: LINUX

2009-12-23 20:58:26

---------------------------- 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


执行程序

python test.py


阅读(2768) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~