ctypes

1, Pointer

>>> cp = c_char_p('Hello, world!')
>>> cp
c_char_p(139667889814172)
>>> print cp.value
Hello, world!

!note: char* : c_char_p

2, Struct

from ctypes import *

class beer_recipe(Structure):
    _fields_ = [
    ('amt_barley', c_int),
    ('amt_water', c_int),
    ]

my_beer = beer_recipe(10, 20)
print my_beer.amt_barley, my_beer.amt_water

out: 10 20

3, Union

from ctypes import *

class barley_amount(Union):
    _fields_ = [
    ('barley_int', c_int),
    ('barley_char', c_char * 8)
    ]

my_barley = barley_amount(66)
print my_barley.barley_int, my_barley.barley_char

out: 66 B

4, SO (Share Object)

from ctypes import *

libc = CDLL('libip6tc.so.0.1.0')
message = 'Hello, world!'
libc.printf('Testing: %s', message)

out: Testing: Hello, world!

!note: the program above run on ubuntu LTS 14.04 Operating System

你可能感兴趣的:(ctypes)