//c++中对应结构体声明:
typedef struct
{
WORD CmdWord1;
WORD CmdWord2;
WORD Datablk[32];
WORD StatusWord1;
WORD StatusWord2;
}MSG_DESCRIPTOR_STRUCT;typedef struct
{
WORD BSW;//Block Status Word
DWORD TimeTag;//Time Tag Word,Resolution:1μs
MSG_DESCRIPTOR_STRUCT MsgBlock;
}RMSG_STRUCT;//c++中函数声明:
DLL void __stdcall BC_ReadNextMsg (BYTE ChanNo, WORD *MsgId, RMSG_STRUCT *Msg);//c++中函数调用:
RMSG_STRUCT gstBCRxMsg[2];
BC_ReadNextMsg ( chno, &SentMsgID, &(gstBCRxMsg[0])); //gstBCRxMsg[0]为需要传出的参数
//c#中结构体声明:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct MSG_DESCRIPTOR_STRUCT
{
[MarshalAs(UnmanagedType.U2)]
public ushort CmdWord1;
[MarshalAs(UnmanagedType.U2)]
public ushort CmdWord2;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U2, SizeConst = 32)]
public ushort[] Datablk;
[MarshalAs(UnmanagedType.U2)]
public ushort StatusWord1;
[MarshalAs(UnmanagedType.U2)]
public ushort StatusWord2; }
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct RMSG_STRUCT
{
[MarshalAs(UnmanagedType.U2)]
public ushort BSW;//Block Status Word
[MarshalAs(UnmanagedType.U8)]
public ulong TimeTag;//Time Tag Word,Resolution:1μs
[MarshalAs(UnmanagedType.Struct)]
public MSG_DESCRIPTOR_STRUCT MsgBlock;
}
ushort gwdBCMsgID;
RMSG_STRUCT gstBCRxTestMsg = new RMSG_STRUCT();
gstBCRxTestMsg.MsgBlock = new MSG_DESCRIPTOR_STRUCT();
gstBCRxTestMsg.MsgBlock.Datablk = new ushort[32];//c#中函数声明方式1:
[DllImport("1553.dll")]
public static extern bool BC_ReadNextMsg (byte ChanNo, ref ushort MsgId, ref RMSG_STRUCT Msg);调用方式1:
BC_ReadNextMsg (0, ref gwdBCMsgID ,ref gstBCRxTestMsg);
该种方式能读取成功,但是 gstBCRxTestMsg中的成员变量全为0;为此尝试了另一种方法:在c++中二次包装 DLL为:
BCReadNextMsg ( BYTE chno, WORD &MsgId, RMSG_STRUCT &Msg,WORD &bsw, DWORD &timetag, MSG_DESCRIPTOR_STRUCT &MsgBlock)
{
BC_ReadNextMsg( chno, &(MsgId), &(Msg));
bsw=Msg.BSW;
timetag=Msg.TimeTag;
memcpy(&MsgBlock,&Msg.MsgBlock,sizeof(Msg.MsgBlock));
}
C#中函数声明为:
public static extern void BCReadNextMsg ( byte chno, ref ushort MsgId, ref RMSG_STRUCT Msg,ref ushort bsw, ref ulong timetag, ref MSG_DESCRIPTOR_STRUCT MsgBlock);
调用为:
ushort bsw=0;
ulong timetag=0;
gstBCRxMsg[chno].MsgBlock = new MSG_DESCRIPTOR_STRUCT();BCReadNextMsg(0, ref gwdBCMsgID ,ref gstBCRxTestMsg, ref bsw, ref timetag, ref MsgBlock);
结果bsw, timetag, MsgBlock都有值,可是gstBCRxTestMsg成员变量全为0可是:bsw, timetag, MsgBlock的值都是由gstBCRxTestMsg拷贝过来的啊,为什么前面有数值,而gstBCRxTestMsg却没有数值呢?
typedef struct
{
WORD CmdWord1;
WORD CmdWord2;
WORD Datablk[32];
WORD StatusWord1;
WORD StatusWord2;
}MSG_DESCRIPTOR_STRUCT;typedef struct
{
WORD BSW;//Block Status Word
DWORD TimeTag;//Time Tag Word,Resolution:1μs
MSG_DESCRIPTOR_STRUCT MsgBlock;
}RMSG_STRUCT;//c++中函数声明:
DLL void __stdcall BC_ReadNextMsg (BYTE ChanNo, WORD *MsgId, RMSG_STRUCT *Msg);//c++中函数调用:
RMSG_STRUCT gstBCRxMsg[2];
BC_ReadNextMsg ( chno, &SentMsgID, &(gstBCRxMsg[0])); //gstBCRxMsg[0]为需要传出的参数
//c#中结构体声明:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct MSG_DESCRIPTOR_STRUCT
{
[MarshalAs(UnmanagedType.U2)]
public ushort CmdWord1;
[MarshalAs(UnmanagedType.U2)]
public ushort CmdWord2;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U2, SizeConst = 32)]
public ushort[] Datablk;
[MarshalAs(UnmanagedType.U2)]
public ushort StatusWord1;
[MarshalAs(UnmanagedType.U2)]
public ushort StatusWord2; }
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct RMSG_STRUCT
{
[MarshalAs(UnmanagedType.U2)]
public ushort BSW;//Block Status Word
[MarshalAs(UnmanagedType.U8)]
public ulong TimeTag;//Time Tag Word,Resolution:1μs
[MarshalAs(UnmanagedType.Struct)]
public MSG_DESCRIPTOR_STRUCT MsgBlock;
}
ushort gwdBCMsgID;
RMSG_STRUCT gstBCRxTestMsg = new RMSG_STRUCT();
gstBCRxTestMsg.MsgBlock = new MSG_DESCRIPTOR_STRUCT();
gstBCRxTestMsg.MsgBlock.Datablk = new ushort[32];//c#中函数声明方式1:
[DllImport("1553.dll")]
public static extern bool BC_ReadNextMsg (byte ChanNo, ref ushort MsgId, ref RMSG_STRUCT Msg);调用方式1:
BC_ReadNextMsg (0, ref gwdBCMsgID ,ref gstBCRxTestMsg);
该种方式能读取成功,但是 gstBCRxTestMsg中的成员变量全为0;为此尝试了另一种方法:在c++中二次包装 DLL为:
BCReadNextMsg ( BYTE chno, WORD &MsgId, RMSG_STRUCT &Msg,WORD &bsw, DWORD &timetag, MSG_DESCRIPTOR_STRUCT &MsgBlock)
{
BC_ReadNextMsg( chno, &(MsgId), &(Msg));
bsw=Msg.BSW;
timetag=Msg.TimeTag;
memcpy(&MsgBlock,&Msg.MsgBlock,sizeof(Msg.MsgBlock));
}
C#中函数声明为:
public static extern void BCReadNextMsg ( byte chno, ref ushort MsgId, ref RMSG_STRUCT Msg,ref ushort bsw, ref ulong timetag, ref MSG_DESCRIPTOR_STRUCT MsgBlock);
调用为:
ushort bsw=0;
ulong timetag=0;
gstBCRxMsg[chno].MsgBlock = new MSG_DESCRIPTOR_STRUCT();BCReadNextMsg(0, ref gwdBCMsgID ,ref gstBCRxTestMsg, ref bsw, ref timetag, ref MsgBlock);
结果bsw, timetag, MsgBlock都有值,可是gstBCRxTestMsg成员变量全为0可是:bsw, timetag, MsgBlock的值都是由gstBCRxTestMsg拷贝过来的啊,为什么前面有数值,而gstBCRxTestMsg却没有数值呢?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货