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

python教程评论1.9K views1阅读模式

C代码如下:

#include <stdio.h>

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

企鹅博客
  • 本文由 发表于 2019年9月9日 06:42:50
  • 转载请务必保留本文链接:https://www.qieseo.com/339541.html

发表评论