Python calls structures and functions of C programs

巴扎黑
Release: 2016-12-07 10:16:58
Original
1633 people have browsed it

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


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!