[Mono-list] DllImport behaves different between .Net and Mono

Andreia Gaita shana.ufie at gmail.com
Fri Jul 27 12:59:45 EDT 2007


On 7/27/07, Steven Schulze <sschulze at vmware.com> wrote:
> On .Net, the DLL is found if I just use the dll name by itself, not the full
> path, something like:
>
>     [DllImport(@"Foo.dll")]
>     public static extern bool Bar(string text);
>
> On Mono, the DLL is not found unless I hardcode the full path, something
> like this:
>
>
>     [DllImport(@"C:\Program Files\MyApp\Foo.dll")]
>     public static extern bool Bar(string text);
>
> This presents a problem because I cannot make assumptions about where the
> app will end up when it gets installed.  It seems a bad idea to hard code
> the absolute path of anything at compile time.  How can I make Mono act like
> .Net in this regard?  Once again, this part of the code will only get
> executed when running on Windows.

There is a lot of code in mono itself that imports windows-specific
dlls to interact with the system, and it has no problems doing so with
the dll name. Please refer to
http://www.mono-project.com/PInvoke#Library_Handling for the proper
way of using DllImport on mono.
You can also check why your mono is not loading the dll by calling, on
a mono command prompt:

X:> set MONO_LOG_LEVEL=debug
X:> set MONO_LOG_MASK=dll
X:> mono --debug [nameofprogram.exe] >log.txt

This will log to a file named log.txt a lot of debug messages,
including those about locating dlls, so that if there are problems
locating your dll, it should show up there.

andreia gaita


More information about the Mono-list mailing list