3

I'm porting a .NET-based test automation framework to Mac OS X. I have significant .NET experience, however, I'm totally a rookie in the Mac world.

I need to call Objective C code from .NET / Mono. I started with the "MonoMac - Binding Objective-C Types" tutorial.

  • I created an API definition file. I couldn't compile it, because the type "BaseTypeAttribute" couldn't be found. Later I found that this is not a problem, because the btouch tool will compile it (basetypeattribute-not-found-in-a-binding-project).

  • Since I'm developing for OS X (not iOS), I was not using btouch, I was going to use the bmac tool. Findig that wasn't trivial, but I managed to compile&found it based on this article. Good.

  • I was not able to compile my binding file, because the bmac said "Type or namespace 'Tasks' does not exists in namespace 'System.Threading'". Nevertheless, I specified an output folder for the generated .cs files, fixed the compile error (the Tasks wasn't really needed, the error was caused by an unnecessary using that was generated).

  • So I'm about at the point where I could have been if I followed the manual "Binding New Objective-C Types / Binding_Details" document.

The problem is that I still cannot use my Objective C library. I can compile and run the C# code with MonoDevelop/Xamarin, but it fails with the message:

System.Exception: Could not create an native instance of the type 'My.Type': the native class hasn't been loaded.
It is possible to ignore this condition by setting MonoMac.ObjCRuntime.Class.ThrowOnInitFailure to false.
 at MonoMac.Foundation.NSObject.InitializeObject (Boolean alloced)
 at MonoMac.Foundation.NSObject..ctor (MonoMac.Foundation.NSObjectFlag x)
 at My.Type..ctor ()

I found an article (coreplot-monomac-bindings-crashing) that says the library must be manually loaded before using it, but I had no luck with it. "Dlfcn.dlopen" always return IntPtr.Zero.

  • I'm tried to create my lib as "Cocoa Framework", "Cocoa Library" and "Bundle" in XCode. Which one should I use?
  • Are there any low-level debugging possibilites in Mono(Mac)?
  • Did anybody managed to do this? (calling objc from .net)

thanks in advance, -g

user2519450
  • 91
  • 1
  • 5
  • You need `Dlfcn.dlopen` to return a valid handle. Check that you are copying the framework into your application bundle and loading it with the correct path. I assume this works with libraries - but I have only ever used Cocoa Frameworks. Finally, make sure your native code is compiled for 32 bit. If it's x64 only, it won't work (monomac is currently 32 bit only) – TheNextman Jun 25 '13 at 15:07
  • Maybe this will help: http://stackoverflow.com/questions/15073178/monomac-xamarin-mac-binding-simple-dylib-not-working – TheNextman Jun 25 '13 at 15:08
  • @TheNextman: Thanks for your comment! Is there a tool like the Fusion Loader Log Viewer, that can show me why the library loading fails? Maybe I put my framework to the wrong place (I'm trying to load it from the folder where the XCode put the build result). Maybe I should copy it under /Libraries or something? – user2519450 Jun 26 '13 at 08:49
  • As far as I know, no such tool exists. In terms of troubleshooting, verify you can use your library in a regular Cocoa app first, maybe? It will tend to give slightly better information in case loading fails and will let you validate the library is not a problem. Next, in monomac, you can check the path is fine by doing something like `File.Exists(...path)`. And then, like I say, make sure your library is compiled for 32 bit. – TheNextman Jun 26 '13 at 13:16
  • I would definitely be trying to include your library in your application bundle, rather than trying to load it from wherever xcode drops it – TheNextman Jun 26 '13 at 13:18

0 Answers0