V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
geew
V2EX  ›  问与答

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

  •  
  •   geew · 2014-10-23 18:54:02 +08:00 · 2081 次点击
    这是一个创建于 3495 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如有这么c函数
    int test (char id[15],char *s,char v[32])
    函数返回int结果, 但是函数处理结果存储在数组v中

    在python中加载了这个模块, 然后调用这个函数, 但我要取得处理结果, 怎么弄呢?
    5 条回复    2014-10-24 11:41:19 +08:00
    pimin
        1
    pimin  
       2014-10-23 19:11:14 +08:00
    test函数是你写的么?
    iptux
        2
    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
        3
    ruoyu0088  
       2014-10-23 19:52:28 +08:00   ❤️ 1
    geew
        4
    geew  
    OP
       2014-10-23 22:08:09 +08:00
    @pimin 不是 这个是供应商提供的接口 我写成test举个例子而已 怎么了
    geew
        5
    geew  
    OP
       2014-10-24 11:41:19 +08:00
    @ruoyu0088 谢谢 可以的
    In [51]: ch = ctypes.create_string_buffer(256) 把ch传进去就行了

    然后ch.value就可以取到值
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2161 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:34 · PVG 19:34 · LAX 04:34 · JFK 07:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.