Search code examples
macosframeworksdllimportmonomacdllnotfoundexception

MonoMac Platform Interop with Framework


I have code that uses the DllImport attribute to talk with an unmanaged assembly. In OS X, this assembly is installed as a framework.

[DllImport("libraryname", CallingConvention = CallingConvention.Cdecl)]
public static extern void FunctionName();

However, this throws a DllNotFoundException in Mono, presumably because it has not been able to resolve the framework.

I looked in the Mono documentation: http://www.mono-project.com/Interop_with_Native_Libraries

They have this little nugget in there:

Mac OS X platforms have a lib prefix and a .dylib suffix, unless they're a Framework, in which case they're a directory and things get more complicated.

But they don't include any info (as far as I could find) about what I should do if it is a directory. Anyone has experience doing this?


Solution

  • This solution is pretty simple if you want to interop with a system framework as its location never changes.

    For example, if you want to access the CFRelease function in the CoreFoundation framework, use:

    [DllImport("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", EntryPoint="CFRelease")]
    public static extern void CFRelease(IntPtr cf);
    

    The Mono loader will load the framework without problem.