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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容