Home  >  Article  >  Backend Development  >  Python calls structures and functions of C programs

Python calls structures and functions of C programs

巴扎黑
巴扎黑Original
2016-12-07 10:16:581697browse

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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn