Maison  >  Article  >  développement back-end  >  python 调用C程序的结构体和函数

python 调用C程序的结构体和函数

巴扎黑
巴扎黑original
2016-11-26 11:39:041481parcourir

C代码如下:

 

#include  

  

typedef struct TestDLL_  

{  

    int a;  

    char *b;  

} testdll;  

  

testdll test(testdll t)  

{  

    t.a=t.a+t.a;  

    printf("%d\n%s\n",t.a,t.b);  

    return t;  

}

 

python代码如下:

 

from ctypes import *  

 

#绝对路径 

dllpath='test.dll'  

dll=CDLL(dllpath)  

 

#python内部参数赋值

a=c_int(125)  

b=c_char_p('Hello world,Hello Chengdu')  

  

#定义结构体

class testdll(Structure):  

    _fields_=[('a',c_int),  

             ('b',c_char_p)]  

 

#实例化并赋值

t=testdll()  

t.a=a  

t.b=b  

  

#设置返回值类型

dll.test.restype=testdll  

  

#测试

t=dll.test(t)  

print t.a  

print t.b  

x=raw_input('any key to continue')


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn