将包含枚举的C ++ Struct编组到C#

时间:2015-03-02 15:01:48

标签: c# c++ struct interop marshalling

以下C ++结构需要编组为C#。它表示来自Tablet触摸传感器的输入,以wm_tablet_flick消息形式出现。因此,我试图在C#应用程序中处理这些消息:

    typedef struct FLICK_DATA {
      FLICKACTION_COMMANDCODE iFlickActionCommandCode  :5;
      FLICKDIRECTION          iFlickDirection  :3;
      BOOL                    fControlModifier  :1;
      BOOL                    fMenuModifier  :1;
      BOOL                    fAltGRModifier  :1;
      BOOL                    fWinModifier  :1;
      BOOL                    fShiftModifier  :1;
      INT                     iReserved  :2;
      BOOL                    fOnInkingSurface  :1;
      INT                     iActionArgument  :16;
    } FLICK_DATA;

其中:

    typedef enum FLICKACTION_COMMANDCODE {
      FLICKACTION_COMMANDCODE_NULL          = 0,
      FLICKACTION_COMMANDCODE_SCROLL        = 1,
      FLICKACTION_COMMANDCODE_APPCOMMAND    = 2,
      FLICKACTION_COMMANDCODE_CUSTOMKEY     = 3,
      FLICKACTION_COMMANDCODE_KEYMODIFIER   = 4 
    } FLICKACTION_COMMANDCODE;

typedef enum FLICKDIRECTION {
  FLICKDIRECTION_RIGHT       = 0,
  FLICKDIRECTION_UPRIGHT     = 1,
  FLICKDIRECTION_UP          = 2,
  FLICKDIRECTION_UPLEFT      = 3,
  FLICKDIRECTION_LEFT        = 4,
  FLICKDIRECTION_DOWNLEFT    = 5,
  FLICKDIRECTION_DOWN        = 6,
  FLICKDIRECTION_DOWNRIGHT   = 7,
  FLICKDIRECTION_INVALID     = 8 
} FLICKDIRECTION;

在C#方面,我得到:

    [StructLayout(LayoutKind.Sequential)]
    private struct FLICKDATA
    {
        internal FLICKACTION_COMMANDCODE iFlickActionCommandCode;
        internal FLICKDIRECTION iFlickDirection;
        public bool fControlModifier;
        public bool fMenuModifier;
        public bool fAltGRModifier;
        public bool fWinModifier;
        public bool fShiftModifier;
        public int iReserved;
        public bool fOnInkingSurface;
        public int iActionArgument;
    }

    public enum FLICKDIRECTION
    {
      FLICKDIRECTION_RIGHT       = 0,
      FLICKDIRECTION_UPRIGHT     = 1,
      FLICKDIRECTION_UP          = 2,
      FLICKDIRECTION_UPLEFT      = 3,
      FLICKDIRECTION_LEFT        = 4,
      FLICKDIRECTION_DOWNLEFT    = 5,
      FLICKDIRECTION_DOWN        = 6,
      FLICKDIRECTION_DOWNRIGHT   = 7,
      FLICKDIRECTION_INVALID     = 8 
    }

    public enum FLICKACTION_COMMANDCODE
    {
      FLICKACTION_COMMANDCODE_NULL          = 0,
      FLICKACTION_COMMANDCODE_SCROLL        = 1,
      FLICKACTION_COMMANDCODE_APPCOMMAND    = 2,
      FLICKACTION_COMMANDCODE_CUSTOMKEY     = 3,
      FLICKACTION_COMMANDCODE_KEYMODIFIER   = 4 
    }

然后我确定结构的大小如下:

private int _flickDataSize = Marshal.SizeOf(new FLICKDATA());

最后尝试使用以下命令将指针转换为结构:

     FLICKDATA data = new FLICKDATA();
     data = (FLICKDATA)Marshal.PtrToStructure(m.WParam, typeof(FLICKDATA));

或使用内置的GetLParam方法:

     data = (FLICKDATA)m.GetLParam(typeof(FLICKDATA));

但两种解决方案都不起作用,无论是空的还是垃圾。我在这里做错了什么?

1 个答案:

答案 0 :(得分:-1)

您是否尝试过投放数据?而不是直接尝试访问它?或者也许创建一个指向FlickData的指针。也没有数据是随机的!/ Garbage它的存在是因为你只需要了解其原因。

所以,请尝试创建指向FlickData的指针或:

FLICKDATA * data = FLICKDATA; 试试(也许必须玩它)

尝试将Flickdata投射到您的Data varible中: 自动数据(static_cast(FLICKDirection);