{"id":432,"date":"2012-11-22T10:41:02","date_gmt":"2012-11-22T09:41:02","guid":{"rendered":"http:\/\/www.martyndavis.com\/?p=432"},"modified":"2022-04-22T09:15:55","modified_gmt":"2022-04-22T08:15:55","slug":"getting-gtk-apps-to-run-on-windows","status":"publish","type":"post","link":"https:\/\/www.martyndavis.com\/?p=432","title":{"rendered":"Getting GTK# Apps to Run on Windows"},"content":{"rendered":"<p><em>In which I explain why GTK# applications built in Monodevelop on Linux don&#8217;t run on Windows, and explain how to fix this&#8230;<\/em><\/p>\n<p><!--more-->As I&#8217;ve mentioned elsewhere, I&#8217;ve been working a lot recently with C# via Monodevelop to create applications that will work cross-platform (specifically Linux, Windows and Mac).<\/p>\n<p>As you probably know, C# and the other .NET languages don&#8217;t compile down to machine code but to CIL (Common Intermediate Language) bytecode which is then interpreted by a JIT (just-in-time) compiler when the application runs. This means I can compile a &#8220;hello world&#8221; console application on Linux, take the resulting hello.exe and run it directly on Windows.<\/p>\n<p>That&#8217;s great for command-line applications, but what if you want to do the same with a GUI application?<\/p>\n<p>Firstly, Monodevelop&nbsp;<em>does<\/em> support WinForms &#8211; but it lacks any kind of GUI designer for this. The built-in designer (called Stetic) outputs GTK#, which is a better option for cross-platform apps anyway &#8211; it just takes a bit of re-adjustment if you&#8217;re used to building WinForms apps (for example the control property equivalent to &#8220;Enabled&#8221; is called &#8220;Sensitive&#8221; &#8211; not immediately apparent!).<\/p>\n<p>So if I build an equivalent &#8220;hello world&#8221; GTK# application consisting of, say, one window with one button that just closes the app, that resulting guihello.exe (or whatever I call it) should run on Windows, right?<\/p>\n<p>Um, no, firstly the obvious point &#8211; if you want to run GTK apps on Windows <a href=\"http:\/\/www.mono-project.com\/Gtk-Sharp_Installer_for_.NET_Framework\">you need the GTK runtimes<\/a>. Install those on Windows and you should be good to go, right? No, there&#8217;s one other gotcha. (<em>Before that &#8211; a quick aside &#8211; if you&#8217;ve experimented with&nbsp;<strong>mkbundle<\/strong> to create static EXEs which package up all dependencies [to avoid needing runtimes], note that it doesn&#8217;t cover GTK# &#8211; you&#8217;ll still need to ensure that&#8217;s installed on the target machine<\/em>).<\/p>\n<p>So, what&#8217;s the second problem? OK, I try to run guihello.exe on Windows by double clicking on it. Hourglass for a few seconds, then nothing. Hmmm.<\/p>\n<p>Looking in the Windows Event Viewer, I see the following error:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nApplication: guihello.exe\nFramework Version: v4.0.30319\nDescription: The process was terminated due to an unhandled exception.\nException Info: System.IO.FileNotFoundException\nStack:\nat MainWindow.Build()\nat MainWindow..ctor()\nat guihello.MainClass.Main(System.String&#x5B;])\n<\/pre>\n<p>That&#8217;s a&nbsp;System.IO.FileNotFoundException&nbsp;exception being thrown before we even get a chance to catch anything. It turns out the problem lies with a reference which is added automatically to Monodevelop&#8217;s GTK#-based projects: &nbsp;<strong>Mono.Posix&nbsp;<\/strong>&#8211; which, if your target machine just has the normal Microsoft .NET runtimes installed, won&#8217;t exist.<\/p>\n<p>So, remove that reference. After you do that you&#8217;ll find that there will be lines in the auto-generated Build() method for the form which now fail &#8211; for example:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nthis.Title = global::Mono.Unix.Catalog.GetString (\"MainWindow\");\n<\/pre>\n<p>The GetString() call is to allow for internationalisation \/ translations. For this test I just changed the lines to use hard-coded strings such as:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nthis.Title = \"Hello World\";\n<\/pre>\n<p>Rebuild, copy the file over to Windows, et voil\u00e0, it now runs.<\/p>\n<p>If you want to prevent Monodevelop from keeping putting back a reference to Mono.Posix and making strings translatable, whenever you enter a string into a widget&#8217;s property in the designer, click the &#8220;&#8230;&#8221; button and uncheck &#8220;Translatable&#8221; in the resultant dialog. Of course this then gives you the problem that you&#8217;ve got hard-coded strings, but that&#8217;s something for another post some time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In which I explain why GTK# applications built in Monodevelop on Linux don&#8217;t run on Windows, and explain how to fix this&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/posts\/432"}],"collection":[{"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=432"}],"version-history":[{"count":8,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/posts\/432\/revisions"}],"predecessor-version":[{"id":853,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/posts\/432\/revisions\/853"}],"wp:attachment":[{"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}