开发者

Union-within-structure syntax in ctypes

开发者 https://www.devze.com 2023-01-11 06:30 出处:网络
Quick question about ctypes syntax, as documentation for Unions isn\'t clear for a beginner like me. Say I want to implement an INPUT structure (see here):

Quick question about ctypes syntax, as documentation for Unions isn't clear for a beginner like me.

Say I want to implement an INPUT structure (see here):

typedef struct tagINPUT {
  DWORD type;
  union {
    MOUSEINPUT    mi;
    KEYBDINPUT    ki;
    HARDWAREINPUT hi;
  } ;
} INPUT, *PINPUT;
开发者_运维知识库

Should I or do I need to change the following code?

class INPUTTYPE(Union):
    _fields_ = [("mi", MOUSEINPUT),
                ("ki", KEYBDINPUT),
                ("hi", HARDWAREINPUT)]

class INPUT(Structure):
    _fields_ = [("type", DWORD),
                (INPUTTYPE)]

Not sure I can have an unnamed field for the union, but adding a name that isn't defined in the Win32API seems dangerous.

Thanks,

Mike


Your Structure syntax isn't valid:

AttributeError: '_fields_' must be a sequence of pairs

I believe you want to use the anonymous attribute in your ctypes.Structure. It looks like the ctypes documentation creates a TYPEDESC structure (which is very similar in construction to the tagINPUT).

Also note that you'll have to define DWORD as a base type for your platform.

0

精彩评论

暂无评论...
验证码 换一张
取 消