python ctype,python ctype递归结构

I've developped a DLL for a driver in C. I wrote a test program in C++ and the DLL works fine.

Now I'd like to interract with this DLL using Python. I've successfully hidden most of the user defined C structures but there is one point where I have to use C structures. I'm rather new to python so I may get things wrong.

My approach is to redefine a few structures in python using ctype then pass the variable to my DLL. However in these class I have a custom linked list which contains recursive types as follow

class EthercatDatagram(Structure):

_fields_ = [("header", EthercatDatagramHeader),

("packet_data_length", c_int),

("packet_data", c_char_p),

("work_count", c_ushort),

("next_command", EthercatDatagram)]

This fails, because inside EthercatDatagram, EthercatDatagram is not already defined so the parser returns an error.

How should I represent this linked list in python so that my DLL understands it correctly?

解决方案

You almost certainly want to declare next_command as a pointer. Having a structure that contains itself isn't possible (in any language).

I think this is what you want:

class EthercatDatagram(Structure):

pass

EthercatDatagram._fields_ = [

("header", EthercatDatagramHeader),

("packet_data_length", c_int),

("packet_data", c_char_p),

("work_count", c_ushort),

("next_command", POINTER(EthercatDatagram))]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值