Home > Article > Backend Development > Python calls structures and functions of C programs
The C code is as follows:
#include
typedef struct TestDLL_
{
int a;
char *b;
} testdll;
testdll test( testdll t)
{
t.a=t.a+t.a;
printf("%dn%sn",t.a,t.b);
return t;
}
python The code is as follows:
from ctypes import *
#Absolute path
dllpath='test.dll'
dll=CDLL(dllpath)
#Python internal parameter assignment
a=c_int(125)
b =c_char_p('Hello world,Hello Chengdu')
#Define structure
class testdll(Structure):
_fields_=[('a',c_int),
)]
#Instantiate and assign
t=testdll()
t.a=a
t.b=b
#Set the return value type
dll.test.restype=testdll
#test
t=dll.test(t)
print t.a
print t.b
x=raw_input('any key to continue')