[Mono-devel-list] P/Invoke and DLLs

Jonathan Pryor jonpryor at vt.edu
Tue Feb 8 07:21:31 EST 2005

On Mon, 2005-02-07 at 17:48 -0800, Frank Bergmann wrote:
> - This one project I have requires wrapping a c - dll. Trying to execute
>   this project resulted of course into a "System.DllNotFoundException".
> I managed to compile this DLL under OS X so how do I tell mono to use it?

What's the name of the library under Win32 vs. OS X?  On OS X, Mono
prepends "lib" and appends ".dylib" to the library name when no
extension is specified.  Thus:

	[DllImport ("SomeLib") private static extern void Foo ();

would use SomeLib.dll on Windows and libSomeLib.dylib on OS X.  Is your
library named libSomeLib.dylib?

However, if your DllImport library includes an extension, such as
[DllImport ("SomeLib.dll")], then this automatic prepending and
appending is disabled.  Alternatively, you may have completely different
library names for each platform -- for example GTK+ is libgtk-
win32-2.0-0.dll on Windows and libgtk-x11-2.0.so on Linux.
In these cases, you need to provide a .config file for your assembly to
specify the library mapping to use:

	<!-- file: MyAssembly.dll.config -->
		<dllmap dll="SomeLib.dll" target="libSomeLib.dylib"/>
		<dllmap dll="libgtk-win32-2.0-0.dll"

The .dll.config file needs to be in the same directory as the assembly.

Finally, at some point GLib had a problem where it was appending .so
instead of .dylib on OS X.  I don't know if this is still the case, but
using the .config file will work around this.

> - Under Win32 callback functions passed into a C-DLL require the
> function to use _stdcall so my question would be what to use for OS X ... gcc
> doesn't seem to know about __attribute(stdcall)__ ... so what do you use for
> that

It's actually __attribute__((stdcall)), but it doesn't matter.  On non-
Windows platforms Mono expects functions to use the cdecl calling
convention, which is also the default for these platforms.

You might want to read the Managed Code Interop guide:


 - Jon

More information about the Mono-devel-list mailing list