[Mono-list] C# interoperation different behavior in Windows and Linux

Robert Jordan robertj at gmx.net
Sun Aug 14 06:01:04 EDT 2011


Hi,

On 12.08.2011 17:45, raincloud wrote:
> Hi, everyone !
> I wrote a simple interoperation test that I ran in Windows 7/MS Visual
> Studio 2010 and Ubuntu 10.04/Mono C# 2.6.7.0. Strangely, the results were
> different.
> In Windows / MS C#:
> - The test loads a function from a DLL file.
> - Then it asks to replace the DLL file with another DLL file with the same
> name and the same function name that implements different functionality.
> - The test loads the second function from the second DLL file.
> - The test runs both functions ...
> ... each function provides a result according to its functionality.
>
> In Linux / Mono:
> ... both functions provide the same result according to functionality of the
> 1-st DLL file.

You're basing your app on undefined behavior.

MS.NET is loading the native DLL at run-time, when the
p/pinvoke function is just about to be invoked for the first
time, while Mono is loading the DLL at JIT-compile-time,
which is a slightly different time.

In your sample, you're calling both functions from
the same method. This means that at invoke-time of
the method (Main) both p/invoke function wrappers are
already compiled and the native DLL behind them is already
loaded. Swapping the native DLL has no effect anymore.

You could circumvent this by invoking the function
from different methods, but it's still undefined
behavior.

Robert



More information about the Mono-list mailing list