如下C++结构在c#中怎么定义 小弟感激不经  分不多  
C++原结构定义:
typedef struct _TLSKeyCommand{
DWord  CommandId;
Byte   Command;
union _CommandParams{
 struct _Command_11{
   Int64  NewManagerId;
   Int64  NewLockId;
  }Command_11;
 struct _Command_22{
   DWord  OlaUserId;
   DWord  NewUserId;
  }Command_22;
 struct _Command_33{
   DWord  UserId;
  }Command_33;
 struct _Command_44{
   DWord  ReturnUserId;
   Bool   NeedOpenLock;
  }Command_44;  
 }commandparams;
}TLSKeyCommand, *PLSKeyCommand;

解决方案 »

  1.   

    union 可以用fieldoffset属性定义
    c++
    union MYUNION
    {
        int number;
        double d;
    }
    c# [ StructLayout( LayoutKind.Explicit )]
    public struct MyUnion 
    {
       [ FieldOffset( 0 )]
       public int i;
       [ FieldOffset( 0 )]
       public double d;
    }
      

  2.   

    TO: hdt (倦怠)
     union 里面有struct  c#中结构是引用类型哦   这个的只适合值类型
      

  3.   

    你需要定义好几个结构体. 将union进行拆分. 而且需要好几个重载函数.像下面这种形式.
    typedef struct _TLSKeyCommand1{
    DWord  CommandId;
    Byte  Command;
    struct _Command_11{
      Int64  NewManagerId;
      Int64  NewLockId;
      }Command_11;
    }typedef struct _TLSKeyCommand2{
    DWord  CommandId;
    Byte  Command;
    struct _Command_22{
      DWord  OlaUserId;
      DWord  NewUserId;
      }Command_22;

      

  4.   

    TO:shrinerain  先试试 解决了在 给分 
      

  5.   

    谢谢 shrinerain   这方法不行哦
      

  6.   

    如果, 你的native函数里面没有检查union类型的话.那么你需要用托管C++做一个包装类, C#将各个结构体传给托管C++, 托管C++检测类型, 组合成合适的包含union的结构体, 再传递给native code.
      

  7.   

    涉及到复杂类型marshal时.考虑采用托管C++做包装类, 不然参数Marshal烦死人.
      

  8.   

    复杂类型考虑使用托管C++进行包装,不然的话,union还需要考虑对齐,容易出错。
      

  9.   

    直接这样就是了:
    [StructLayout(LayoutKind.Sequential)]
    public class TLSKeyCommand

    public Int32  CommandId; 
    public Byte  Command; 
    public Int64  NewManagerId; 
    public Int64  NewLockId;  public Int32  OlaUserId; 
    public Int32  NewUserId;  public Int32  UserId; 
    public Int32  ReturnUserId; 
    public Boolean  NeedOpenLock; 
    }
      

  10.   

    你这个结构只是比较大,但并不复杂,也不涉及指针,基本就是值类型,如果有指针,简单的话直接传intptr
    计算好每个字段的偏移量即可。
      

  11.   

    public struct _Command_11
    {
        public Int64 NewManagerId;
        public Int64 NewLockId;
    }
    public struct _Command_22
    {
        public UInt32 OlaUserId;
        public UInt32 NewUserId;
    }
    public struct _Command_33
    {
        public UInt32 UserId;
    }
    public struct _Command_44
    {
        public UInt32 ReturnUserId;
        public UInt32 NeedOpenLock;
    }
    [StructLayout(LayoutKind.Explicit)]
    public struct TLSKeyCommand
    {
        [FieldOffset(0)]
        public UInt32 CommandId;
        [FieldOffset(4)]
        public Byte Command;
        [FieldOffset(8)]
        public _Command_11 Command_11;
        [FieldOffset(8)]
        public _Command_22 Command_22;
        [FieldOffset(8)]
        public _Command_33 Command_33;
        [FieldOffset(8)]
        public _Command_44 Command_44;
    }
      

  12.   

    再类似这样调用:
    [DllImport("Cpp.dll")]
    static extern int TestFunction([In, Out] ref TLSKeyCommand Command);TLSKeyCommand x = new TLSKeyCommand();
    TestFunction(ref x);