有关 python ctypes 加载 c 模块的一个疑问

2014-10-23 18:54:02 +08:00
 geew
比如有这么c函数
int test (char id[15],char *s,char v[32])
函数返回int结果, 但是函数处理结果存储在数组v中

在python中加载了这个模块, 然后调用这个函数, 但我要取得处理结果, 怎么弄呢?
2087 次点击
所在节点    问与答
5 条回复
pimin
2014-10-23 19:11:14 +08:00
test函数是你写的么?
iptux
2014-10-23 19:21:45 +08:00
```python
import ctypes

so = ctypes.CDLL('libtest.so')
so.test.restype = ctypes.c_int
so.test.argtypes = (ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p)

print so.test('arg1', 'arg2', 'arg3')
```
ruoyu0088
2014-10-23 19:52:28 +08:00
geew
2014-10-23 22:08:09 +08:00
@pimin 不是 这个是供应商提供的接口 我写成test举个例子而已 怎么了
geew
2014-10-24 11:41:19 +08:00
@ruoyu0088 谢谢 可以的
In [51]: ch = ctypes.create_string_buffer(256) 把ch传进去就行了

然后ch.value就可以取到值

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/141074

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX