Interactive python and C (ctypes library)

Interactive python and C (ctypes library)

ctypes is an external Python library, you can use python callable already compiled C language functions and data types and data exchange. ctypes official documents in https://docs.python.org/3/library/ctypes.html

 

1. ctypes basic data types map

2. python call the library c language

1. Generate c language function

#Step 1:  test.c

#include <stdio.h>

the Add int (int A, int B)
{
    return A + B;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
2. libtest.so compiler generates dynamic link library file (DLL)

-fPIC -shared -o libtest.so test.c gcc  
1
3. call DLL files

#Step 3:  test.py
from ctypes import *

mylib = CDLL ( "libtest.so") or cdll.LoadLibrary ( "libtest.so")   
the Add = mylib.add
add.argtypes = [c_int, c_int] # parameter types, two int (c_int is ctypes type, supra table)
add.restype = # c_int return type, int (c_int is ctypes type, see table above)
SUM = the Add (. 3,. 6)
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
3. pointers and references

For instance pointer assignment would only change the memory address to which it points, rather than changing the contents of memory. Examples pointer contents have a property that returns an object pointer points to.

Function Description
byref (x [, offset]) Returns the address x, x must be an instance ctypes type. Corresponds to the c & x. offset is the offset.
pointer (x) creates and returns a pointer points to an instance of x, x is an instance object.
The POINTER (type) a return type, which is a pointer to the type of type pointer type, a type of ctypes type.
Import ctype * from  
I c_int = (. 1)  
PI = POINTER (c_int) (I)  
PI2 = pointer (I)
Print # pi.contents object returns a pointer value
Print pi2.contents
. 1
2
. 3
. 4
. 5
. 6
pointer and a POINTER The difference is, pointer returns an instance, pOINTER a return type.

4. The data structure type

Structures and Unions must inherit Structure and Union base classes that are defined in the ctypes module, each subclass must define a property fields, fields is a two-dimensional list of tuples, contain the name and type of each field, and this field type must be a ctypes type, such as c_int, or any other type of inheritance ctypes, such as Structure, Union, Array, pointer or the like.

* ctypes Import from  
Import types  
class the Test (Structure):  
    the _fields_ = [( 'X', c_int),  
                ( 'Y', c_char)]  
test1 the Test = (. 1, 2)  
. 1
2
. 3
. 4
. 5
. 6
. The structure for when the operation list, i.e. contains a pointer pointing to the structure, need to be defined as follows

from ctypes import *  
import types  
class Test(Structure):  
    pass  
Test._fields_ = [('x', c_int),  
                ('y', c_char),  
                ('next', POINTER(Test))]  
1
2
3
4
5
6
7
参考文献: 
1. http://blog.csdn.net/jsky_studio/article/details/38597655 
2. http://blog.csdn.net/linda1000/article/details/12623527 
3. https://www.cnblogs.com/night-ride-depart/p/4907613.html

It is noted that: more portions of text and the code reference from reference, if in doubt, references to view detailed explanation.
----------------
Disclaimer: This article is the original article CSDN bloggers "awakeljw", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/awakeljw/article/details/79369703

 

Published 145 original articles · won praise 223 · Views 1.28 million +

Guess you like

Origin blog.csdn.net/qq_36838191/article/details/104765634