C++导出结构体
C++ 封装一个函数返回结构体,c#使用C++的DLL文件调用函数,返回结构体
C++代码
struct LIBCOMPILEERROR_API tagCheckResultInfo { int m_taskId; int m_checkResult; int m_varCount; }; extern "C" __declspec(dllexport)tagCheckResultInfo GetCheckResult() { tagCheckResultInfo info; info.m_taskId = 1001; info.m_checkResult = 0; // 成功 info.m_varCount = 5; return info; }
C#代码
[StructLayout(LayoutKind.Sequential)] public struct tagCheckResultInfo { public int m_taskId; public int m_checkResult; public int m_varCount; // 如果 C++ 版本有指针成员(如 tagVarCheckResult* m_varResult), // 这里应该用 IntPtr,然后通过 Marshal.PtrToStructure 读取。 } [DllImport("ConsoleApplication1.dll", CallingConvention = CallingConvention.Cdecl)] public static extern tagCheckResultInfo GetCheckResult(); private void button1_Click(object sender, EventArgs e) { tagCheckResultInfo result = GetCheckResult(); // 使用结果 MessageBox.Show($"TaskId: {result.m_taskId}, Result: {result.m_checkResult}, VarCount: {result.m_varCount}"); }
在C#这里要定义一个跟C++一样的结构体

浙公网安备 33010602011771号