我有一个调用本机 C++ dll 的 C# 程序。
C# 具有以下结构:
[StructLayout(LayoutKind.Sequential)]
public struct Phenomenon
{
[MarshalAs(UnmanagedType.U1)] public char Type;
[MarshalAs(UnmanagedType.R8)] public double Jd;
[MarshalAs(UnmanagedType.R8)] public double Loc_jd;
[MarshalAs(UnmanagedType.R8)] public double Angle;
[MarshalAs(UnmanagedType.U1)] public char Tran_dir;
[MarshalAs(UnmanagedType.I2)] public Int16 Warning;
}
C++ 程序具有以下结构定义:
typedef struct
{
char type;
double jd;
double loc_jd;
double angle;
char tran_dir;
short int warning;
} phenomenon;
实例化 Phenomenom 对象的 C# 程序对 C++ 方法具有以下调用:
Phenomenon[] phenomena = new Phenomenon[6];
short status = rsttwi_temp(phenomena);
C++ 只是简单地更新结构并返回。这是 C++ 函数:
__declspec(dllexport) short int rsttwi_temp (phenomenon phen[1])
{
phen[0].type = 'Z';
phen[0].jd = 1.1;
phen[0].loc_jd = 2.2;
phen[0].angle = 3.3;
phen[0].tran_dir = 4.4;
phen[0].warning = '0';
return 0;
}
从 C# 程序中,当我在调用 C++ 函数后打印出结构的值时,该结构尚未更新(即与调用 C++ 函数之前相同)。似乎 C++ 程序能够看到这些值并更新它们,但是在返回时,C# 永远不会看到对结构中值的更新。认为这是一个值与引用的问题,我什至尝试用 C# 程序中的一个类而不是结构来做到这一点。仍然没有运气。
问题:我需要什么样的封送处理才能让被调用的 C++ 函数能够更新传递给它的实际结构?
谢谢你。-Greg Edwards 完全沉浸式软件