Python interacts with C + +

Source: Internet
Author: User

Python and C + + can interact effectively, and C + + calls some small usages of python

Wrote a Python script import exception, which could be a coding problem (such as the presence of Chinese),
Python defaults to ASCII
can add:
#!/usr/bin/python
#-*-Coding:utf-8-*-
See also: https://www.python.org/dev/peps/pep-0263/

Define Class C Data structures:
Class Point (Structure):
_pack_ = 1
_fields_ = [(' x ', c_uint8),
(' Y ', c_uint8),
]
To define an array of points to initialize:
Pointlist = [Point (0,0),
Point (+),
]
Visit: pointlist[i].x, POINTLIST[I].Y
can also be cast to pointer access
PPoint = Cast (AddressOf (Point), POINTER (point))
Pointer has an attribute contents as an instance object
Ppoint.contents.x, Ppoint.contents.y

To define an array:
Class Array (Structure):
_pack_ = 1
_fields_ = [(' IArray ', c_uint8 * 5),
(' Barray ', C_ubyte * 5),
]
To define a buffer:
BUF = C_buffer ("Hello", 520)
BUF = Create_string_buffer ("Hello", 10)
The second one is new, and the old ones can be used.

cTYPES Data type: https://docs.python.org/2/library/ctypes.html#ctypes.c_ubyte

AddressOf (obj): Get ctypes type Address
ByRef (obj, offset):
Returns the cTYPES type lightweight pointer
Corresponds to the following C code: (((char *) &obj) + offset)
sizeof: Calculating the number of bytes for a variable and type

Some of the functions of the C library are called:
libc = Cdll.msvcrt
fopen = Libc.fopen
Fwrite = Libc.fwrite
Fclose = Libc.fclose
fseek = Libc.fseek
Fread = Libc.fread
Ftell = Libc.ftell
memset = Libc.memset
memcpy = libc.memcpy
You can get the address of the function, as in C, note that these function operations require an address
You can use AddressOf to get the address of the cTYPES type

Receive the parameters from C + +:
If a Unicode character is passed to a python string, an unexpected error will occur.
Although it is correct to write param content by printing and writing to a file, the actual call to the LIBC library will still
Path not found
STR (Unicode (param))

When using Python, it is more helpful to use the language if you know more about its intrinsic mechanism.

The following code:

1 #!/usr/bin/python2 #-*-coding:utf-8-*-3 ImportOS, sys4  fromcTYPESImport*5 6 classCPoint (Structure):7_pack_ = 18_fields_ = [('x', c_uint8),9('y', c_uint8),Ten                 ] One      A classCArray (Structure): -_pack_ = 1 -_fields_ = [('IArray', C_uint8 * 5), the('Barray', C_char * 5), -                 ] -      - defeditbuf (buf, len): +TMP =buf -i =0 +val = C_char ('7') A      whileI <Len: atMemmove (AddressOf (TMP) +i,addressof (val), sizeof (Val))#AddressOf (TMP) +i equivalent to pointer shift -i + = 1 -  -LIBC =CDLL.MSVCRT -fopen =Libc.fopen -Fwrite =Libc.fwrite inFclose =Libc.fclose -Fseek =Libc.fseek toFread =Libc.fread +Ftell =Libc.ftell -memset =Libc.memset thememcpy =libc.memcpy *  $ if __name__=='__main__':Panax NotoginsengPoint = CPoint (5,6) -PPoint =cast (AddressOf (Point), POINTER (CPoint)) the     Print "Point.x =%d, Point.y =%d"%(Point.x, Point.y) +     Print "Ppoint.x =%d, Ppoint.y =%d"%(ppoint.contents.x, Ppoint.contents.y) A     PrintPoint, Ppoint.contents#Here are two address values that indicate that the system has a new request for ppoint a point to save the copied value the  +Pointlist = [CPoint (0,1), -CPoint (2,3) $                  ]     $     Printpointlist[0].x, Pointlist[0].y#Array -  -Iarr = (C_uint8 * 5) (5,6,7,8) theBARR = (C_char * 5) ('a','b','C') -     Printbarr[:]WuyiCArray = CArray (Iarr, barr[:])#C_uint8 and C_char have different ways of assigning initial values, but they haven't studied the     Printcarray.iarray[:] -     Printcarray.barray[:] Wu  -BUF = C_buffer ("12345", 8)#similar c_char*8 About     PrintBuf.value $Editbuf (buf,8)#Modify the BUF value to modify the memory -     PrintBuf.value -Memmove (AddressOf (BUF), ByRef (C_char ('a')) , sizeof (C_char) ) -     PrintBuf.value A  +     #C library function call theFP =fopen ("D:\\pythoncallclib.txt","wb+") -Fwrite (Buf,1,sizeof (BUF), FP)#77777777 $Fseek (fp,3, 0) theFwrite (AddressOf (C_char ('8')),, FP) the fclose (FP) the  the     #sizeof -     Print "c_uint8 =%d bytes, C_int =%d bytes, buf =%d bytes"%(sizeof (c_uint8), sizeof (C_int), sizeof (BUF)) in  the     #cast can cast buf into our custom type theBUF = Create_string_buffer ("ABC", 8) Aboutp =cast (AddressOf (BUF), POINTER (CPoint)) the     Printp.contents.x, P.contents.y the     

Python interacts with C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.