我有编组问题。我试图将 C++ 结构翻译成 C#,但它不起作用。
C# 代码
namespace DLL_IMPORT_TEST
{
public struct CUDA_POINT
{
public int x;
public int y;
public CUDA_POINT(int X, int Y)
{
x = X;
y = Y;
}
}
public struct DataStruct
{
public Int64 a;
public long b;
public long c;
public CUDA_POINT d;
public DataStruct(int p1, int p2, long p3, CUDA_POINT p4)
{
a = p1;
b = p2;
c = p3;
d = new CUDA_POINT(p4.x, p4.y);
}
}
[DllImport("DLL_C++.dll", CallingConvention = CallingConvention.StdCall)]
unsafe public static extern void PrintDataStruct(ref DataStruct d1);
private void OnPrintBtnClicked(object sender, EventArgs e)
{
DataStruct d1 = new DataStruct(1000, 1000, -10, new CUDA_POINT(0,0));
string s1 = string.Format("{0} {1} {2} {3}, {4}\n", d1.a, d1.b, d1.c, d1.d.x, d1.d.y);
PrintDataStruct(ref d1);
PrintBox.Text += s1;
}
}
C++ 代码
extern "C" __declspec(dllexport) void PrintDataStruct(DataStruct d1){
ofstream outFile("Output.txt");
outFile << "a == " << d1.a << endl;
outFile << "b == " << d1.b << endl;
outFile << "c == " << d1.c << endl;
outFile << "d == " << d1.d.x << ", " << d1.d.y << endl;
outFile.close();
}
C# 的输出是正确的,但 C++ 的输出是不正确的。
C# 输出
1000
1000
-10
0,0
C++ 输出
1000
1000
-10
-10, -1