python 传字符串到c的 dll

Python026

python 传字符串到c的 dll,第1张

用ctypes,

>>>from ctypes import *

>>>libc=cdll.msvcrt

>>>libc.printf("hello %s\n","world")

hello world

12

>>>

>>>p = create_string_buffer("Hello", 30)

>>>n=libc.sprintf(p,"hello %s\n","world")

>>>n

12

>>>print p

<ctypes.c_char_Array_30 object at 0x02247C10>

>>>dir(p)

['__class__', '__ctypes_from_outparam__', '__delattr__', '__delitem__', '__delsl

ice__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__',

'__getslice__', '__hash__', '__init__', '__len__', '__module__', '__new__', '__r

educe__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__setslice

__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__',

'_b_base_', '_b_needsfree_', '_length_', '_objects', '_type_', 'raw', 'value']

>>>p.value

'hello world\n'

>>>

用ctypes,

>>>from ctypes import *

>>>libc=cdll.msvcrt

>>>libc.printf("hello %s\n","world")

hello world

12

>>>

>>>p = create_string_buffer("Hello", 30)

>>>n=libc.sprintf(p,"hello %s\n","world")

>>>n

12

>>>print p

<ctypes.c_char_Array_30 object at 0x02247C10>

>>>dir(p)

['__class__', '__ctypes_from_outparam__', '__delattr__', '__delitem__', '__delsl

ice__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__',

'__getslice__', '__hash__', '__init__', '__len__', '__module__', '__new__', '__r

educe__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__setslice

__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__',

'_b_base_', '_b_needsfree_', '_length_', '_objects', '_type_', 'raw', 'value']

>>>p.value

'hello world\n'

>>>