如下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;
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;
c++
union MYUNION
{
int number;
double d;
}
c# [ StructLayout( LayoutKind.Explicit )]
public struct MyUnion
{
[ FieldOffset( 0 )]
public int i;
[ FieldOffset( 0 )]
public double d;
}
union 里面有struct c#中结构是引用类型哦 这个的只适合值类型
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;
}
[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;
}
计算好每个字段的偏移量即可。
{
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;
}
[DllImport("Cpp.dll")]
static extern int TestFunction([In, Out] ref TLSKeyCommand Command);TLSKeyCommand x = new TLSKeyCommand();
TestFunction(ref x);