[Mono-dev] Populate array of PointF

howard.rubin howard.rubin at hl.konicaminolta.us
Thu Mar 9 23:20:04 UTC 2017


I need to use mono in C++ to populate an array that will be sent to C#.
Here's my output and complete test program:

bash-4.3$ rm array *.dll ; make ; echo '' ; ./array
Mono C# compiler version 4.0.3.0
mcs array.cs /out:array.dll /target:library /reference:System.Drawing.dll
g++ (GCC) 5.1.1 20150618 (Red Hat 5.1.1-4)
g++ array.cpp -o array -g3 -std=c++11 `pkg-config --cflags --libs mono-2`

Point: 42, 1701 <==== GOOD ====
Array: 1.265393E-24, 4.566131E-41 <==== BAD ====
bash-4.3$ 


// array.cs
namespace MyNamespace  {
    public class MyClass {
        public MyClass() {
        }

        public static void PrintPoint(System.Drawing.PointF pt) {
            System.Console.WriteLine("Point: " + pt.X + ", " + pt.Y + "
<==== GOOD ====");
        }

        public static void PrintArray(System.Drawing.PointF[] pts) {
            foreach (System.Drawing.PointF pt in pts) {
                System.Console.WriteLine("Array: " + pt.X + ", " + pt.Y + "
<==== BAD ====");
            }
        }
    }
}


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

int main() {
    MonoDomain* domain = mono_jit_init("array.dll");
    MonoAssembly* assembly = mono_domain_assembly_open(domain, "array.dll");
    MonoImage* image = mono_assembly_get_image(assembly);

    MonoAssembly* DrawingAssembly = mono_domain_assembly_open(domain,
"/usr/lib/mono/4.5/System.Drawing.dll");
    MonoImage* DrawingImage = mono_assembly_get_image(DrawingAssembly);

    ////////////////////////////////////////////////////////////////

    // Create a System.Drawing.PointF
    MonoClass* ptFClass = mono_class_from_name(DrawingImage,
"System.Drawing", "PointF");
    MonoMethodDesc* ptFCtorDesc =
mono_method_desc_new("System.Drawing.PointF:.ctor (single,single)", true);
    MonoMethod* ptFCtor = mono_method_desc_search_in_class(ptFCtorDesc,
ptFClass);

    MonoObject* ptF = mono_object_new(domain, ptFClass);

    float x = 42, y = 1701;
    void* args[] = { &x, &y };
    MonoObject* exception = nullptr;
    mono_runtime_invoke(ptFCtor, ptF, args, &exception);

    ////////////////////////////////////////////////////////////////

    // Call C# method to print the point
    MonoClass* MyClass = mono_class_from_name(image, "MyNamespace",
"MyClass");
    MonoMethodDesc* MyMethodDesc =
mono_method_desc_new("MyNamespace.MyClass:PrintPoint
(System.Drawing.PointF)", true);
    MonoMethod* MyClassMethod =
mono_method_desc_search_in_class(MyMethodDesc, MyClass);

    args[0] = ptF;
    mono_runtime_invoke(MyClassMethod, nullptr, args, &exception); // Prints
"Point: 42, 1701"
    if (exception)
        mono_print_unhandled_exception(exception);

    ////////////////////////////////////////////////////////////////

    // Create 1 element array of PointF and print it
    MonoArray* arrayPtF = mono_array_new(domain, ptFClass, 1);
    mono_array_set(arrayPtF, MonoObject*, 0, ptF);

    MonoMethodDesc* MyMethodDesc2 =
mono_method_desc_new("MyNamespace.MyClass:PrintArray
(System.Drawing.PointF[])", true);
    MonoMethod* MyClassMethod2 =
mono_method_desc_search_in_class(MyMethodDesc2, MyClass);

    args[0] = arrayPtF;
    mono_runtime_invoke(MyClassMethod2, nullptr, args, &exception); //
Prints "Array: -9.872364E+33, 4.591635E-41"

    if (exception)
        mono_print_unhandled_exception(exception);


    mono_jit_cleanup(domain);
    return 0;
}


# Makefile
array : array.cpp array.dll Makefile
	@g++ --version | head -1
	g++ $< -o $@ -g3 -std=c++11 `pkg-config --cflags --libs mono-2`

array.dll : array.cs
	@mcs --version
	mcs $< /out:$@ /target:library /reference:System.Drawing.dll




--
View this message in context: http://mono.1490590.n4.nabble.com/Populate-array-of-PointF-tp4670214.html
Sent from the Mono - Dev mailing list archive at Nabble.com.


More information about the Mono-devel-list mailing list