Using a batch file to launch Gtk# apps on Windows with Mono instead of .NET
Mono is great for cross-platform development – maybe not so great for cross-platform deployment. In working on Chordious, a Gtk# app written entirely in MonoDevelop on an Ubuntu machine, there’s been no greater struggle than trying to find a simple “double-click” launch of Chordious on Windows.
Yes, if you stick with just the “standard” libraries and write your GUI with Windows.Forms, all an end-user has to do is double-click on your executable, and let .NET run it. But what if you don’t want that? What if your app needs to be launched by the Mono Runtime?
Realistically, the bigger hurdle is getting your end-users to install Mono in the first place – but even if you can get them past that – you’ll still need a double-click way to start your app. You’ll never convince them to start up a Mono command prompt and manually launch your app with mono.exe. And unfortunately for us, the Mono installer for Windows isn’t so nice as to add itself to the PATH, so you can’t just get away with a one-line batch file.
@echo off setlocal rem Script: StartMonoApp.cmd rem Author: Jon Thysell <email@example.com> rem Date: 7/15/2014 set _MonoApp=YourMonoApp.exe rem Checking for 32/64bit machine if exist "%SYSTEMROOT%\SysWOW64" ( set WOW6432=\Wow6432Node ) else ( set WOW6432= ) rem Find default Mono version set _MonoBase=HKEY_LOCAL_MACHINE\SOFTWARE%WOW6432%\Novell\Mono echo Looking for Mono registry key %_MonoBase% for /f "Tokens=2*" %%I in ('reg query "%_MonoBase%" /V "DefaultCLR" 2^>nul') do set _MonoVersion=%%J if "%_MonoVersion%" equ "" ( echo ERROR: DefaultCLR not found! goto nomono ) echo Default Mono is %_MonoVersion% rem Find the path to where that version of Mono is installed for /f "Tokens=2*" %%I in ('reg query "%_MonoBase%\%_MonoVersion%" /V "SdkInstallRoot" 2^>nul') do set _MonoPath=%%J if "%_MonoPath" neq "" ( if not exist "%_MonoPath%" ( echo ERROR: SdkInstallRoot not found! goto nomono ) ) echo Mono %_MonoVersion% installed at %_MonoPath% rem Check for the mono.exe binary if not exist "%_MonoPath%\bin\mono.exe" ( echo ERROR: mono.exe not found! goto nomono ) set PATH=%_MonoPath%\bin;%PATH% rem Launch the app pushd %~dp0 echo Launching %_MonoApp% start mono.exe %_MonoApp% %* popd goto :quit :nomono echo ERROR: Unable to find Mono. Please install Mono and try again. pause :quit endlocal
So let’s say you’ve got yourself a nice little Gtk# app named GreatMonoApp.exe. To make a double-click launcher that uses Mono, simply:
- Copy the contents (minus the line numbers) of the script above into Notepad.
- Update line 8 with the name of your executable (in this example, the line should read set _MonoApp=GreatMonoApp.exe).
- Save the file into the same directory as your executable. (You can name the file whatever you want, just make sure you save it as a .cmd, not .txt, file).
There you go! Double-click on that new batch file and your app should launch via Mono. You might see a flicker or two of command prompts, but otherwise works quite well. If something does go wrong, the command prompt will stay open with an (hopefully useful) error message for you to debug.
How does the script work? Essentially it looks for the registry keys that the Mono for Windows installer created, and uses them to find where the mono.exe binary is. Then it adds that folder to your PATH (temporarily) so it can use mono.exe to launch your app. And as mentioned before, if there’s anything wrong (can’t find the registry keys, can’t find the folder or binaries) the script will show an error.
I hope someone out there finds this as useful as I do – I’ve spent forever trying to solve this problem, with progressively more and more complicated scripts. I’ve verified that the script works on Windows XP, 7, 8, and 8.1, 32 and 64-bit.
P.S. I have no reason to believe the script won’t work on Vista, I just don’t have access to a Vista machine to test it. Sorry true believers.