求救:C# 调用C++ 回调函数的问题,将无效 VARIANT 传递给 CLR 会导致意外的异常
客户给的dll文件,用c#调用,其中有个方法是要传回调函数c++是这样定义的
1、On_exec_result SetExecResult(On_exec_result pHandler);
2、typedef void(CALLBACK* On_exec_result)(const TAnswer *pAnswer);
3、TAnswer{
char chInfo [64]
int nAnswer
int nMarkets
char chMarketFlag [32][4]
int nDynDate [32]
}
(真搞不明白搞不明白SetExecResult的返回值也是个On_exec_result函数?)
我c#是这样写的:
1、 [DllImport("TAPI.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void SetExecResult(On_exec_result pHandler);
2、public delegate void On_exec_result (TAnswer tAnswer);
3、public struct TAnswer
{
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 64)]
public byte[] chInfo;
[MarshalAs(UnmanagedType.I4)]
public int nAnswer;
[MarshalAs(UnmanagedType.I4)]
public int nMarkets;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 3 * 16)]
public byte[] chMarketFlag;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public byte[] nDynDate;
}
调用的地方:
On_exec_result tHandler = new On_exec_result(OnExecHandler);
SetExecResult(tHandler);
public void OnExecHandle(TAnswer tAnswer)
{
Logger.Info(tAnswer.nAnswer);
}
运行的时候错误:从非托管 VARIANT 转换为托管对象的过程中检测到无效 VARIANT。将无效 VARIANT 传递给 CLR 会导致意外的异常、损坏或数据丢失