通过单声道将数组从 C++ 传递给 C#

Posted

技术标签:

【中文标题】通过单声道将数组从 C++ 传递给 C#【英文标题】:Pass array to C# from C++ via mono 【发布时间】:2015-06-02 22:26:11 【问题描述】:

我需要使用单声道将数组从 C++ 传递到 C#。但我无法编译 mono_array_set()。那么如何将数组从 C++ 传递到 C#?

我尝试过 mono_runtime_invoke(),它可以编译但会出现运行时错误。

mcs /nologo /warn:4 /debug:pdbonly /o /nowarn:3003 /platform:x64 /out:array.dll /target:library array.cs
g++ array.cpp -g3 `pkg-config --cflags --libs mono-2` -o array
<snip> 
array.cpp:32:5: note: in expansion of macro ‘mono_array_set’

// array.cpp
#include <mono/jit/jit.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/debug-helpers.h>

int main(int argc, char* argv[]) 
    MonoDomain* domain = mono_jit_init("./array.dll");;
    MonoAssembly* assembly = mono_domain_assembly_open(domain, "./array.dll");
    MonoImage* image = mono_assembly_get_image(assembly);

    MonoClass* containsClass = mono_class_from_name(image, "IntArray", "ContainsAnInt");
    MonoMethodDesc* containsDesc = mono_method_desc_new("IntArray.ContainsAnInt:.ctor(int)", false);
    MonoMethod* containsCtor = mono_method_desc_search_in_class(containsDesc, containsClass);
    MonoObject* containsObject = mono_object_new(domain, containsClass);

    void* args[1];
    int value = 7;
    args[0] = &value;

    MonoObject* exception = NULL;
    mono_runtime_invoke(containsCtor, containsObject, args, &exception);

    MonoClass* unpackageClass = mono_class_from_name(image, "IntArray", "Unpackage");

    args[0] = containsObject;
    MonoMethodDesc* returnIntDesc = mono_method_desc_new("IntArray.Unpackage:ReturnInt(IntArray.ContainsAnInt)", true);
    MonoMethod* returnIntMethod = mono_method_desc_search_in_class(returnIntDesc, unpackageClass);
    mono_runtime_invoke(returnIntMethod, NULL, args, &exception); // <--- as expected, outputs "In ReturnInt: 7"

    MonoArray* theArray = mono_array_new(domain, containsClass, 1);
    //// Following will not compile
    mono_array_set(theArray, MonoClass*, 0, containsObject);
    ////
    MonoMethodDesc* returnElementDesc = mono_method_desc_new("IntArray.Unpackage:ReturnElement(IntArray.ContainsAnInt[])", true);
    MonoMethod* returnElementMethod = mono_method_desc_search_in_class(returnElementDesc, unpackageClass);
    mono_runtime_invoke_array(returnElementMethod, NULL, theArray, &exception); // <--- should output "In ReturnElement: 7"

    mono_jit_cleanup(domain);


// array.cs
using System;
using System.IO;

namespace IntArray 
    public class ContainsAnInt 
       public ContainsAnInt(int i)  IntValue = i;  
       public int IntValue  get; set; 
    
    public class Unpackage 
        public static int ReturnInt(ContainsAnInt n) 
            Console.WriteLine("In ReturnInt: " + n.IntValue); 
            return n.IntValue;
        
        public static int ReturnElement(ContainsAnInt[] n) 
            Console.WriteLine("In ReturnElement: " + n[0].IntValue); 
            return n[0].IntValue;
        
    

【问题讨论】:

【参考方案1】:

这行得通:

MonoArray* theArray = mono_array_new(domain, containsClass, 1);
mono_array_set(theArray, MonoObject*, 0, containsObject);
args[0] = theArray;
mono_runtime_invoke(returnElementMethod, NULL, args, &exception); // <--- as expected, outputs "In ReturnElement: 7"

【讨论】:

【参考方案2】:

这也有效,因为我能够定义一个布局与 System.Drawing.PointF 完全相同的 C 结构。如果我需要从 C++ 填充 C# 类的数组,仍然没有答案?

MonoArray* arrayPtF = mono_array_new(domain, ptFClass, 2);
struct cpoint 
    float x, y;
 mycpoint[] =   42, 1701 ,  1701, 42  ;
mono_array_set(arrayPtF, cpoint, 0, mycpoint[0]);
mono_array_set(arrayPtF, cpoint, 1, mycpoint[1]);

【讨论】:

以上是关于通过单声道将数组从 C++ 传递给 C#的主要内容,如果未能解决你的问题,请参考以下文章

嵌入式单声道:将数组从 C# DLL 返回/复制到 C++

将二维数组从 C# 传递到 C++

通过 Net Core 将 C++ 数组传递给 C# 层

将可写 StringBuilder 数组从 C# 传递给 C++

如何将结构数组从 c++ 传递到 c#?

通过互操作将字符串数组从 C# 传递到 C++