c# - 如何将结构从非托管 C++ 程序传递到 C# 程序?

标签 c# c++ interop struct

这是我的第二篇文章。

这是我正在尝试做的事情:从 C# 调用一个非托管 C++ 程序,从 C# 程序传入一个结构数组,并从 C++ 程序返回一个更新版本的结构数组。

这里是调用的c#程序:

using System;
using System.Runtime.InteropServices;

namespace TestCallingC
{
    class Program
    {
        [DllImport(@"..\..\..\Debug\TestCProgram.dll")]
        public static extern void testfunc(teststruc[] ts);

        static void Main(string[] args)
        {
            teststruc[] teststructs = new teststruc[6];
            for (int i = 0; i < teststructs.Length; i++)
            {
                teststructs[i].int1 = (i + 1) * 100;
                teststructs[i].int2 = (i + 1) * 100;
            }

            testfunc(teststructs);
            for (int i = 0; i < teststructs.Length; i++)
            {
                Console.WriteLine("Int1 = {0}", teststructs[i].int1);
                Console.WriteLine("Int2 = {0}", teststructs[i].int2);
            }
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct teststruc
    {
        [MarshalAs(UnmanagedType.I4)]
        public int int1;
        [MarshalAs(UnmanagedType.I4)]
        public int int2;
    }
}

这里是返回的c++程序:

extern "C" __declspec(dllexport) void testfunc (teststruc* ts)
{
    int i;

    for (i = 0; i < 6; i++)
    {

        ts[i].int1 = i;
        ts[i].int2 = i;
    }

    for (i = 0; i < 6; i++)
    {
        printf("ts[%d].int1 = %d\n", i, ts[i].int1);
        printf("ts[%d].int2 = %d\n", i, ts[i].int2);
    }

    return;
}

我上面提供的版本允许 c++ 程序查看并打印出从 c# 程序传递的入站结构。当控件传回c#程序时,数据和原来设置的一样。它允许被调用的 C++ 程序更新结构。这是控制台输出。第一部分显示来自称为 c++ 程序的更新字段;第二部分是 c# 最初设置的 ws 来电者:

ts[0].int1 = 0
ts[0].int2 = 0
ts[1].int1 = 1
ts[1].int2 = 1    
ts[2].int1 = 2
ts[2].int2 = 2    
ts[3].int1 = 3
ts[3].int2 = 3    
ts[4].int1 = 4
ts[4].int2 = 4
ts[5].int1 = 5
ts[5].int2 = 5

Int1 = 100
Int2 = 100
Int1 = 200
Int2 = 200
Int1 = 300
Int2 = 300
Int1 = 400
Int2 = 400
Int1 = 500
Int2 = 500
Int1 = 600
Int2 = 600

如果我将“ref”标记添加到 c# 调用签名,则从 c++ 程序返回的结构为空:

[DllImport(@"..\..\..\Debug\TestCProgram.dll")]
        public static extern void testfunc(ref teststruc[] ts);

testfunc(ref teststructs);

问题:需要对 c++ 和 c# 程序中的接口(interface)进行哪些更新才能使结构在 c++ 程序中正确更新并返回到 c# 程序?

我已经发现了很多关于类似事情的信息,但没有任何信息能给我正确的组合来实现它。欢迎任何建议。

谢谢。 -格雷格

最佳答案

随机猜测:

[DllImport(@"..\..\..\Debug\TestCProgram.dll")]
public static extern void testfunc([In, Out] teststruc[] ts);

来自 MSDN :

Combining the InAttribute and OutAttribute is particularly useful when applied to arrays and formatted, non-blittable types. Callers see the changes a callee makes to these types only when you apply both attributes. [...]

关于c# - 如何将结构从非托管 C++ 程序传递到 C# 程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1250126/

相关文章:

C# 使用 Interop 写入打开的 Excel 文件

c# - 如何防止/取消 c# 中组合框的值更改?

c# - 为什么调用 ResourceManager 类比直接按名称加载资源更好?

c++ - 具有纯虚方法的抽象类 - 为什么可以执行 "Abstract * abs3;"?

C++ OOP 重复继承模式

c# - 如何在 C# 和 C++ 之间进行互操作

c# - 如何以 32/64 位兼容方式在 C# 中定义 MAPINAMEID 结构?

c# - 为 C# P/Invoke 调用 C++ dll 获取正确参数时出现问题

c# - MiniProfiler 在 ASP.NET MVC2 中工作吗?

c++ - C++中的虚函数层次结构