Code Optimization

Interesting things in software development and code optimization

C#.NET and Cross-Platform application

Today I'm going to share my experience with c#.net and cross-platform desktop application.

I did write a simple and small windows desktop application under Visual Studio 2017 Community Edition and was wondering if I could run it on Linux Ubuntu and Mac.

The application was just one form and a few combo boxes and buttons app and used Newtonsoft.JSON and web communication. So nothing platform-dependent (except windows forms and controls itself ;) ) and I was almost sure it should work on linux and mac.




First thing I did remember was Mono: https://www.mono-project.com/ and another thing was Gtk#.

I did start with Gtk# and did play a few hours with it. Then I just ported my code to the standard Windows Form Application project and framework .NET Framework 7 just to find out how it would go :)


So I started trying it on Ubuntu. To run simple Windows Forms EXE application on Ubuntu you need mono:

sudo apt-get --assume-yes install mono-runtime

Now I tried to run my exe:

mono MyApp.exe

And I got the following error:

Could not load signature of System.Windows.Forms.Control:GetAccessibilityObjectById due to: Could not load file or assembly or one of its dependencies. assembly:Accessibility, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a type:<unknown type> member:<none>
Unhandled Exception:
System.TypeLoadException: Could not load type 'MyApp.Form1' from assembly 'MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

"Hmm, what does it mean?" As mono should already have all this System.*.dll and Microsoft.*.dll libraries I just deleted everything except EXE and Newtonsoft.Json.dll and tried one more time and I got another exception:

Unhandled Exception:
System.IO.FileNotFoundException: Could not load file or assembly or one of its dependencies.
File name: 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
I did google a little bit and found some posts about mono and that mono-complete had much more libraries needed to run .Net.
So I installed mono-complete:

sudo apt-get --assume-yes install mono-complete
And tried to run my EXE and what do you think? Sure - not so easy ;), here is another exception I got:

Unhandled Exception:
System.TypeInitializationException: The type initializer for 'System.Windows.Forms.XplatUI' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Windows.Forms.X11DesktopColors' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Console' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.ConsoleDriver' threw an exception. ---> System.Exception: Magic number is wrong: 542
  at System.TermInfoReader.ReadHeader (System.Byte[] buffer, System.Int32& position) [0x0002b] in <8f2c484307284b51944a1a13a14c0266>:0 
  at System.TermInfoReader..ctor (System.String term, System.String filename) [0x00065] in <8f2c484307284b51944a1a13a14c0266>:0 
  at System.TermInfoDriver..ctor (System.String term) [0x00058] in <8f2c484307284b51944a1a13a14c0266>:0 
  at System.ConsoleDriver.CreateTermInfoDriver (System.String term) [0x00000] in <8f2c484307284b51944a1a13a14c0266>:0 
  at System.ConsoleDriver..cctor () [0x00062] in <8f2c484307284b51944a1a13a14c0266>:0 
   --- End of inner exception stack trace ---
  at System.Console.SetupStreams (System.Text.Encoding inputEncoding, System.Text.Encoding outputEncoding) [0x0000a] in <8f2c484307284b51944a1a13a14c0266>:0 
  at System.Console..cctor () [0x000a8] in <8f2c484307284b51944a1a13a14c0266>:0 
   --- End of inner exception stack trace ---
  at System.Windows.Forms.X11DesktopColors..cctor () [0x001bb] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
   --- End of inner exception stack trace ---
  at System.Windows.Forms.XplatUIX11..ctor () [0x0007c] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
  at System.Windows.Forms.XplatUIX11.GetInstance () [0x0001c] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
  at System.Windows.Forms.XplatUI..cctor () [0x0007d] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
   --- End of inner exception stack trace ---
  at System.Windows.Forms.Application.EnableVisualStyles () [0x00006] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
  at MyApp.Program.Main () [0x00000] in <33f4886a9603454889cff03ba442909e>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'System.Windows.Forms.XplatUI' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Windows.Forms.X11DesktopColors' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Console' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.ConsoleDriver' threw an exception. ---> System.Exception: Magic number is wrong: 542
  at System.TermInfoReader.ReadHeader (System.Byte[] buffer, System.Int32& position) [0x0002b] in <8f2c484307284b51944a1a13a14c0266>:0 
  at System.TermInfoReader..ctor (System.String term, System.String filename) [0x00065] in <8f2c484307284b51944a1a13a14c0266>:0 
  at System.TermInfoDriver..ctor (System.String term) [0x00058] in <8f2c484307284b51944a1a13a14c0266>:0 
  at System.ConsoleDriver.CreateTermInfoDriver (System.String term) [0x00000] in <8f2c484307284b51944a1a13a14c0266>:0 
  at System.ConsoleDriver..cctor () [0x00062] in <8f2c484307284b51944a1a13a14c0266>:0 
   --- End of inner exception stack trace ---
  at System.Console.SetupStreams (System.Text.Encoding inputEncoding, System.Text.Encoding outputEncoding) [0x0000a] in <8f2c484307284b51944a1a13a14c0266>:0 
  at System.Console..cctor () [0x000a8] in <8f2c484307284b51944a1a13a14c0266>:0 
   --- End of inner exception stack trace ---
  at System.Windows.Forms.X11DesktopColors..cctor () [0x001bb] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
   --- End of inner exception stack trace ---
  at System.Windows.Forms.XplatUIX11..ctor () [0x0007c] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
  at System.Windows.Forms.XplatUIX11.GetInstance () [0x0001c] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
  at System.Windows.Forms.XplatUI..cctor () [0x0007d] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
   --- End of inner exception stack trace ---
  at System.Windows.Forms.Application.EnableVisualStyles () [0x00006] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
  at MyApp.Program.Main () [0x00000] in <33f4886a9603454889cff03ba442909e>:0 
"God! What are you even talking about?" - I did google a little bit more and decided to try one suggestion about some additional libraries that might be required to run .NET application, here you are:

sudo apt-get --assume-yes install libappindicator0.1-cil-dev
sudo apt-get --assume-yes install libgtk2.0-cil
sudo apt-get --assume-yes install libmono-posix4.0-cil

some of the libraries were already installed but just in case I write them here anyway.

"Lets run" I thought and tried again. "Wow! It works". Cool!

Sometime it gave me a error even my application ran successfully, something like:

Failed to load module "canberra-gtk-module"

so I did find out that it required one more library:

sudo apt-get install libcanberra-gtk-module libcanberra-gtk3-module


Finally I came to the following - you need to install some libraries and mono itself firstly:

sudo apt-get update
sudo apt-get --assume-yes install mono-runtime
sudo apt-get --assume-yes install mono-complete

sudo apt --assume-yes install libappindicator0.1-cil-dev
sudo apt --assume-yes install libgtk2.0-cil
sudo apt --assume-yes install libmono-posix4.0-cil

After that I was able to run my EXE application from command line (make sure your Terminal is pointed to the EXE folder) like that:

mono MyApp.EXE


And I was really impressed when it brought up windows form with buttons combos etc.

Even more it did communicate good via internet and JSON was working as well :) 

and this is for EXE compiled for .Net Framework 7





Then I did start trying similar things on Mac mini with Mac OS X 10.13 but unfortunately with no luck :(

I did try a few cases and was trying to resolve some issues, missed libraries, etc but unfortunately 

I came up to that that it gave just exception stack trace with errors :(


Not sure if I will continue trying to find out a way to run it under Mac but I any success I will let you know ASAP ;)


Thank you


1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y



Loading