Using mono to call C # from C / C ++

I need to write docx files on Linux, so I am compiling Open-XML-SDK with mono . I am trying to do what this website suggests .

What can I do to understand why I can't get the MonoMethod * object for the constructor, or better, get this to work?

Here are my sample programs and output:

// hello.cs
using System;
namespace HelloWorld
{
    class Hello 
    {
    public Hello(string s) { _s = s; }

    public void DoSomething(string s) { _s = s; }

    public string _s;
    }
}

      


// hello.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("hello.dll");
  MonoAssembly* assembly = mono_domain_assembly_open(domain, "hello.dll");
  MonoImage* image = mono_assembly_get_image(assembly);

  MonoClass* helloClass = mono_class_from_name(image, "HelloWorld", "Hello");


  MonoMethodDesc* ctorDesc = mono_method_desc_new("HelloWorld.Hello:Hello(string)", false);
  MonoMethod* ctorMethod = mono_method_desc_search_in_class(ctorDesc, helloClass);

  printf("ctorDesc from mono_method_desc_new() is %p\n", ctorDesc);
  printf("ctorMethod from mono_method_desc_search_in_class() is %p    <----\n", ctorMethod);


  MonoMethodDesc* doDesc = mono_method_desc_new("HelloWorld.Hello:DoSomething(string)", false);
  MonoMethod* doMethod = mono_method_desc_search_in_class(doDesc, helloClass);

  printf("doDesc from mono_method_desc_new() is %p\n", doDesc);
  printf("doMethod from mono_method_desc_search_in_class() is %p\n", doMethod);


  mono_jit_cleanup(domain);
}

      


$ mcs /nologo /warn:4 /debug:pdbonly /o  /out:hello.dll /target:library hello.cs /reference:WindowsBase.dll
$ gcc hello.cpp -g3 `pkg-config --cflags --libs mono-2` -o hello
$ ./hello
ctorDesc from mono_method_desc_new() is 0x22b1920
ctorMethod from mono_method_desc_search_in_class() is (nil)    <----
doDesc from mono_method_desc_new() is 0x22b2590
doMethod from mono_method_desc_search_in_class() is 0x224ae38

$ uname -a
Linux U14-OOXML 3.16.0-37-generic #51~14.04.1-Ubuntu SMP Wed May 6 15:23:14 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

      

+3


source to share


1 answer


What a class constructor Hello

is Hello(string)

is a lie told by C # to make it more familiar to C ++ and Java programmers.

But the Mono runtime, like the Microsoft .NET CLR, works with compiled code, not C #. The true name of each instance constructor .ctor

, regardless of the type name, and .cctor

for the type initializer (aka "static constructor"). If you are looking .ctor(string)

instead Hello(string)

, you must meet with success. There are several working examples on the internet, it's easier to find once you know what to look for.



Elsewhere C # lies about naming, for default indexers, nested types, closures ... just a few. In all of these cases, you can see the true naming convention existing in the metadata using the disassembler ( ildasm

in .NET, not sure about the name of the equivalent Mono tool).

+3


source







All Articles