Search code examples
c#objective-ccocoamono

Mixing C# with Objective-C


I would like to use larger body of C# code as a library for Objective-C (Cocoa) application.

I discovered MonoMac project which wraps Cocoa code, but I would rather have standard Cocoa application written in Objective-C, which can call wrapped C# code (other way around).

On Windows I am used to make C++/CLI project which wraps .NET code and exports plain old C interface for C/C++ based apps.

Is there some simple way to achieve this?


Solution

  • There is, obviously, no such language as C++/CLI on Mac OS. On Windows, C++/CLI actually compiles as managed code ran by the CLR, that runs native code; since on Mac OS Mono isn't integrated to the system, it's rather the other way around. Your app is native, and it can host managed code.

    Mono exposes functions to host a CLR virtual machine inside a process. Since CLR classes aren't directly exposed to your C code, you'll be able to call methods of objects through reflection-like calls.

    There is documentation on how to embed Mono into an application on the official site. Since you're not interested in running .NET programs directly, you should rather read the "Invoking Methods in the CIL Universe" section. On Mac OS, you'll want to link against the Mono framework from your /Library/Frameworks folder, instead of using pkg-config.

    This really shouldn't replace an actual reading of the above document, but the following can be seen as a guide as to what to expect:

    #include <glib/glib.h>
    #include <mono/jit/jit.h>
    #include <mono-metadata/assembly.h>
    #include <mono/metadata/debug-helpers.h>
    
    // create an app domain
    // http://en.wikipedia.org/wiki/Application_Domain
    MonoDomain* domain = mono_jit_init("Domain");
    
    // mandatory Cocoa call to show that Mono and ObjC work together
    NSBundle* mainBundle = [NSBundle mainBundle];
    NSString* dll = [mainBundle pathForResource:@"your-dll" ofType:@"dll"];
    
    // load the referenced assembly in our domain
    MonoAssembly* assembly = mono_domain_assembly_open(domain, [dll UTF8String]);
    MonoImage* image = mono_assembly_get_image(assembly);
    
    // find the class we want to wrap and create an uninitialized instance
    MonoClass* classHandle = mono_class_from_name(image, "Name.Space", "YourClass");
    MonoObject* object = mono_object_new(domain, classHandle);
    
    // this calls the default, argument-less ctor
    // for more complex constructors, you need to find the method handle and call it
    // (helpful hint: constructors are internally called ".ctor", so the description
    // string will look like "Name.Space.Class:.ctor()")
    mono_runtime_object_init(object);
    
    // get a method handle to whatever you like
    const char* descAsString = "Name.Space.YourClass:YourMethod()";
    MonoMethodDesc* description = mono_method_desc_new(descAsString);
    MonoMethod* method = mono_method_desc_search_in_class(description, classHandle);
    
    // call it
    void* args[0];
    mono_runtime_invoke(method, object, args, NULL);
    
    // when you're done, shutdown the runtime by destroying the app domain
    mono_jit_cleanup(domain);
    

    If you don't find this very appealing, you may want to go the other way around, as you mentioned, and look into MonoMac, which provides .NET bindings to a large portion of the APIs you may want to use in a Mac application (Cocoa, CoreImage, CoreAnimation, etc) and means to create your own bindings.