[Mono-list] porting from .NET to Mono

Jonathan Pryor jonpryor at vt.edu
Thu Nov 18 15:10:17 EST 2010


On Thu, 2010-11-18 at 08:06 -0800, Steve Lessard wrote:
> I would like to get your advice on porting code from .NET to Mono.  I
> have a class library that I wrote for .NET 3.5 on Windows. In addition
> to working in .NET on Windows I want to get it working in Mono on
> non-Windows platforms (especially OS X on Intel chips.) This class
> library is currently about 90% C# and 10% managed C++.

C++/CLI that is not /clr:pure is not supported by mono on any platform.

> My initial thoughts on a plan for porting the code:
>       * If available replace each p/invoke to a Win32 API with a call
>         to a Mono class/method providing similar functionality.

Doubtful; if you could do that, then there'd likely be a .NET method you
could be using already (in which case, wouldn't you already be using
it?).

>       * If not available I plan to leave the p/invoke in place and
>         build a platform specific native library (.dylib on OS X)
>         providing similar functionality with the same signature.

A slight variation, there's:

        http://mono-project.com/DllMap

This allows you to change the native library name and the function
names, as long as the functions have identical signatures...

>       * Stub out or #ifdef out the code entirely. (For example the
>         code that ties in to Windows event logging is not essential
>         functionality.)

Generally not ideal, as it means you need to do multiple builds for each
platform.

Then there's option 4: Runtime platform checks:

        static readonly bool IsWinApi = Path.DirectorySepartorChar == '\
        \';
        /* ... */
        if (IsWinApi)
                CallWinApiMethod ();
        else
                DoSomethingElse();

Runtime checks are generally preferred.

> My initial thoughts on a strategy for porting the code:
>       * Port this library to Mono on Windows

Good first step.

>       * Port it to Mono on WINE on non-Windows platforms

Skip.

>       * Port it to Mono on non-Windows platforms

Good idea.

> My questions:
>      1. Can Mono on Windows run managed C++ code?

I do not think so, unless the C++/CLI code is compiled as /clr:pure.

>      1. Assuming the managed C++ code makes no platform specific
>         calls, is clean for 32/64 bit architectures, clean for bit
>         endianness could it be recompiled can Mono on non-Windows
>         platforms run managed C++ code?

Again, is it compiled as /clr:pure?  If it is, it should run; if it
isn't, it won't run.

>      1. Would porting to Mono on WINE on OS X as an intermediate step
>         be worth the effort?

No.

>      1. Is there a better or alternative porting strategy that you
>         would recommend?

No.

 - Jon




More information about the Mono-list mailing list