【问题标题】:structure member update using cffi python使用 cffi python 更新结构成员
【发布时间】:2019-07-30 13:43:50
【问题描述】:

大家好,我有一个包含以下成员的结构

struct test {       
    uint8_t         one;      
    uint32_t        two;      
    uint8_t         three;     
    bool_t          four;   
    bool_t          five;  
    bool_t          six;                                         
    bool_t          seven;

};

我有以下python函数

from cffi import FFI
ffi=FFI()
dll = ffi.dlopen( 'test.dll')
def test_one(**kwargs ):
        p_val = ffi.new( 'struct test*' ) #create a struct using cffi
        dout = dll.test_one( p_val ) #call the func from dll
        kwargs['one'] =str( p_val.one) 
        kwargs['two '] = str( p_val.two )
        kwargs['three'] =str( p_val.three) 
        kwargs['four'] = str( p_val.four)
        kwargs['five'] = str( p_val.five)
        kwargs['six'] =str( p_val.six) 
        kwargs['seven'] = str( p_val.seven)
        print kwargs

当我打印结构成员(即 kwargs)时,除了第一个成员之外,结构成员不会更新。我是否传递或接收任何错误的值? 请让我知道我的代码中的任何错误谢谢。

【问题讨论】:

    标签: python struct cffi


    【解决方案1】:

    嗨,我遇到了问题,python 中的 bool_t 占用 1 个字节,但由于某种奇怪的原因,使用 Visual Studio C++(版本 4)创建的 dll 中的 bool_t 占用了 4 个字节,因为只有少数结构元素被更新(幸运的是)。通过#define bool_t char 解决了问题。

    【讨论】:

      猜你喜欢
      • 2019-02-10
      • 2020-08-15
      • 2020-02-05
      • 2013-12-25
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 2016-01-20
      • 1970-01-01
      相关资源
      最近更新 更多