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++结构在c#中怎么定义 小弟感激不经 分不多
19 个解决方案
#1
顶起
#2
这个比较难...
#3
有难度才有挑战 高手 来顶起
#4
union本质上是共享存储区域,在C#的结构体中,可以分配一段内存,再写几个方法把内存展示成不同的数据就可以了。
#5
用
[Fileoffset]修饰
[Fileoffset]修饰
union MYUNION
{
int number;
double d;
}
union MYUNION2
{
int i;
char str[128];
};
[ StructLayout( LayoutKind.Explicit )]
public struct MyUnion
{
[ FieldOffset( 0 )]
public int i;
[ FieldOffset( 0 )]
public double d;
}
[ StructLayout( LayoutKind.Explicit, Size=128 )]
public struct MyUnion2_1
{
[ FieldOffset( 0 )]
public int i;
}
[ StructLayout( LayoutKind.Sequential )]
public struct MyUnion2_2
{
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=128 )]
public String str;
}
#6
类包含
#7
[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe public struct _TLSKeyCommand
{
public uint CommandId;
public byte Command;
fixed byte Buffer[16];
public Command_11 Command_11
{
get
{
fixed (byte* p = Buffer)
{
return *(Command_11*)p;
}
}
set
{
fixed (byte* p = Buffer)
{
byte[] b = new byte[sizeof(Command_11)];
Marshal.Copy((IntPtr)(&value), b, 0, b.Length);
Marshal.Copy(b, 0, (IntPtr)p, b.Length);
//可以再找找其他更直接的办法复制数据
}
}
}
//再实现其他的几个
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_11
{
public Int64 NewManagerId;
public Int64 NewLockId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct _Command_22
{
public uint OlaUserId;
public uint NewUserId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_33
{
public uint UserId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_44
{
public uint ReturnUserId;
public bool NeedOpenLock;
}
unsafe public struct _TLSKeyCommand
{
public uint CommandId;
public byte Command;
fixed byte Buffer[16];
public Command_11 Command_11
{
get
{
fixed (byte* p = Buffer)
{
return *(Command_11*)p;
}
}
set
{
fixed (byte* p = Buffer)
{
byte[] b = new byte[sizeof(Command_11)];
Marshal.Copy((IntPtr)(&value), b, 0, b.Length);
Marshal.Copy(b, 0, (IntPtr)p, b.Length);
//可以再找找其他更直接的办法复制数据
}
}
}
//再实现其他的几个
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_11
{
public Int64 NewManagerId;
public Int64 NewLockId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct _Command_22
{
public uint OlaUserId;
public uint NewUserId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_33
{
public uint UserId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_44
{
public uint ReturnUserId;
public bool NeedOpenLock;
}
#8
hdt的方式更好
#9
mark
#10
hdt 回答的我不是很懂 谁能解释下
#11
FieldOffset就是指定该字段从结构的内存布局的第几个字节开始,如果不指定,默认是一个接一个按顺序来的。
#12
用了 soaringbird 的 还是没有写入成功 HDT 的不懂 高手没告诉我把
API 函数是
API(Bool AddCommand) (TLSKeyCommand Command);
API 函数是
API(Bool AddCommand) (TLSKeyCommand Command);
#13
不太懂, 帮顶一下.
#14
用StructLayout 属性设置struct的内存分布结构。查下msdn
#15
[StructLayout(LayoutKind.Explicit, Pack = 1)]
unsafe public struct _TLSKeyCommand
{
[FieldOffset(0)]
public uint CommandId;
[FieldOffset(4)]
public byte Command;
[FieldOffset(8)]
public Command_11 Command_11;
[FieldOffset(8)]
public Command_22 Command_22;
//再实现其他的几个
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_11
{
public Int64 NewManagerId;
public Int64 NewLockId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_22
{
public uint OlaUserId;
public uint NewUserId;
}
unsafe public struct _TLSKeyCommand
{
[FieldOffset(0)]
public uint CommandId;
[FieldOffset(4)]
public byte Command;
[FieldOffset(8)]
public Command_11 Command_11;
[FieldOffset(8)]
public Command_22 Command_22;
//再实现其他的几个
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_11
{
public Int64 NewManagerId;
public Int64 NewLockId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_22
{
public uint OlaUserId;
public uint NewUserId;
}
#16
这里不要这个unsafe,遗留的
#17
高手呢 问题没解决呀 帮顶 。。。
#18
先把问题简单化 如果我用4个结构来定义能行吗
public struct _TLSKeyCommand_11这个定义错在哪呢
{
public uint CommandId;
public byte Command;
public long NewManagerId;
public long NewLockId;
}
#19
人呢 问题解决了 分全部给你 高手呢
#20
#1
顶起
#2
这个比较难...
#3
有难度才有挑战 高手 来顶起
#4
union本质上是共享存储区域,在C#的结构体中,可以分配一段内存,再写几个方法把内存展示成不同的数据就可以了。
#5
用
[Fileoffset]修饰
[Fileoffset]修饰
union MYUNION
{
int number;
double d;
}
union MYUNION2
{
int i;
char str[128];
};
[ StructLayout( LayoutKind.Explicit )]
public struct MyUnion
{
[ FieldOffset( 0 )]
public int i;
[ FieldOffset( 0 )]
public double d;
}
[ StructLayout( LayoutKind.Explicit, Size=128 )]
public struct MyUnion2_1
{
[ FieldOffset( 0 )]
public int i;
}
[ StructLayout( LayoutKind.Sequential )]
public struct MyUnion2_2
{
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=128 )]
public String str;
}
#6
类包含
#7
[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe public struct _TLSKeyCommand
{
public uint CommandId;
public byte Command;
fixed byte Buffer[16];
public Command_11 Command_11
{
get
{
fixed (byte* p = Buffer)
{
return *(Command_11*)p;
}
}
set
{
fixed (byte* p = Buffer)
{
byte[] b = new byte[sizeof(Command_11)];
Marshal.Copy((IntPtr)(&value), b, 0, b.Length);
Marshal.Copy(b, 0, (IntPtr)p, b.Length);
//可以再找找其他更直接的办法复制数据
}
}
}
//再实现其他的几个
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_11
{
public Int64 NewManagerId;
public Int64 NewLockId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct _Command_22
{
public uint OlaUserId;
public uint NewUserId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_33
{
public uint UserId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_44
{
public uint ReturnUserId;
public bool NeedOpenLock;
}
unsafe public struct _TLSKeyCommand
{
public uint CommandId;
public byte Command;
fixed byte Buffer[16];
public Command_11 Command_11
{
get
{
fixed (byte* p = Buffer)
{
return *(Command_11*)p;
}
}
set
{
fixed (byte* p = Buffer)
{
byte[] b = new byte[sizeof(Command_11)];
Marshal.Copy((IntPtr)(&value), b, 0, b.Length);
Marshal.Copy(b, 0, (IntPtr)p, b.Length);
//可以再找找其他更直接的办法复制数据
}
}
}
//再实现其他的几个
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_11
{
public Int64 NewManagerId;
public Int64 NewLockId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct _Command_22
{
public uint OlaUserId;
public uint NewUserId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_33
{
public uint UserId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_44
{
public uint ReturnUserId;
public bool NeedOpenLock;
}
#8
hdt的方式更好
#9
mark
#10
hdt 回答的我不是很懂 谁能解释下
#11
FieldOffset就是指定该字段从结构的内存布局的第几个字节开始,如果不指定,默认是一个接一个按顺序来的。
#12
用了 soaringbird 的 还是没有写入成功 HDT 的不懂 高手没告诉我把
API 函数是
API(Bool AddCommand) (TLSKeyCommand Command);
API 函数是
API(Bool AddCommand) (TLSKeyCommand Command);
#13
不太懂, 帮顶一下.
#14
用StructLayout 属性设置struct的内存分布结构。查下msdn
#15
[StructLayout(LayoutKind.Explicit, Pack = 1)]
unsafe public struct _TLSKeyCommand
{
[FieldOffset(0)]
public uint CommandId;
[FieldOffset(4)]
public byte Command;
[FieldOffset(8)]
public Command_11 Command_11;
[FieldOffset(8)]
public Command_22 Command_22;
//再实现其他的几个
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_11
{
public Int64 NewManagerId;
public Int64 NewLockId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_22
{
public uint OlaUserId;
public uint NewUserId;
}
unsafe public struct _TLSKeyCommand
{
[FieldOffset(0)]
public uint CommandId;
[FieldOffset(4)]
public byte Command;
[FieldOffset(8)]
public Command_11 Command_11;
[FieldOffset(8)]
public Command_22 Command_22;
//再实现其他的几个
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_11
{
public Int64 NewManagerId;
public Int64 NewLockId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Command_22
{
public uint OlaUserId;
public uint NewUserId;
}
#16
这里不要这个unsafe,遗留的
#17
高手呢 问题没解决呀 帮顶 。。。
#18
先把问题简单化 如果我用4个结构来定义能行吗
public struct _TLSKeyCommand_11这个定义错在哪呢
{
public uint CommandId;
public byte Command;
public long NewManagerId;
public long NewLockId;
}
#19
人呢 问题解决了 分全部给你 高手呢