请问python中如何传递一个字符串给c程序dll中c函数,同时c函数如何返回一个字符串给python

2025-01-07 17:01:20
推荐回答(1个)
回答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

>>> 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'
>>>