热门话题

Php 研究室

一段python调用.so动态库的代码    作者:xieaotian发表于2008-08-21 15:17:29

---------------------------- test.py#! /usr/local/binimport ctypeslib_handle = ctypes.CDLL('./libfun.so')test = lib_handle.testprint test(5)testA = lib_handle.testAprint testA(1, 3)testB = lib_handle.testBprint testB('aaaaaaaaaaaaaaaaaaaaa')testB.restype = ctypes.c_char_pprint testB('bbbbbbbbbbbbbbbbbbbbbbb')class AA(ctypes.Structure): _fields_=[("a", ctypes.c_int),("b", ctypes.c_int)] aa = AA()aa.a = 1aa.b = 8testC = lib_handle.testCprint testC(ctypes.byref(aa))testD = lib_handle.testDprint testD(ctypes.byref(aa)), aa.a, aa.bclass BB(ctypes.Structure): _fields_=[("a", ctypes.c_int),("pB", ctypes.c_char_p),("c", ctypes.c_int)]bb = BB()bb.a = 1bb.pB = 'ssssssssssssssssssss'bb.c = 2testE = lib_handle.testEtestE.restype = ctypes.c_char_pprint testE(ctypes.byref(bb)), bb.a, bb.cbb.pB = NonetestF = lib_handle.testFprint testF(ctypes.byref(bb)), bb.a, bb.pB, bb.cprint 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;}

回复主题
Copyright © 2008-2010 版权所属:中国Python联盟 www.okpython.com
京ICP备08012290号 村长QQ:81356625 E-mail:xieaotian@163.com