Hi!

Made a mini tutorial and a startup script.. heard about some other program 
that did something about it too but then it was to late, i had already done 
it so here it is .. 

/per
Title: .NET executables directly from the command line in Linux

.NET executables directly from the command line in Linux

Abstract

This is a mini tutorial on how to make .NET executables run directly from the command line like this:

[pure@p tmp]$ mcs.exe
or
[pure@p test]$ ./my_dotnet_app.exe

Registering .NET executables with the binfmt_misc kernel module

To do this we need to echo a a string containing the association to the 'register' file in the '/proc/sys/fs/binfmt_misc' directory. The format of this string looks like this:

:name:type:offset:magic:mask:interpreter:

Now as you see we need some info to create this string:

Name:   mono can be anything you wish
Type:   M specifies that we want to use the magic number instead of the extension
Offset:   74 Nr of bytes to where the magic number is found inside the .exe file
Magic:  01 4c Magic number as stated here
Mask:   should be left empty in this case
Interpreter:   /usr/local/bin/mono path to interpreter or jit ex: mono or mint

The string we end up with now looks like this:

:mono:M:74:\x01\x4c::/usr/local/bin/mono:

Now lets register this string with binfmt_misc. To achieve that we do like this (as root ):

[root@p tmp]# echo ':mono:M:74:\x01\x4c::/usr/local/bin/mono:' > /proc/sys/fs/binfmt_misc/register

A file called 'mono' (or the name you chose) should have been created in this directory. You can try it out by executing a .NET executable but remember that the executable bit has to be set.

[pure@p test]$ ./my_dotnet_app.exe

You probably want to have this registration done at boot time so then you could just add :

echo ':mono:M:74:\x01\x4c::/usr/local/bin/mono:' > /proc/sys/fs/binfmt_misc/register

to one of your rc scripts of your choice.

Important

Make sure that the registration is done after Wine makes its registrations for windows exe files. Otherwise wine will try to execute the file.

Sample script for use at startup in /etc/init.d

#!/bin/bash
# This startup script enables .NET portable executables to be executed directly
# from command line or by clicking on them.
# The script it converted from wine's by Per Arneng

RETVAL=0

function start () {
    /sbin/modprobe binfmt_misc &> /dev/null
    
    RETVAL=$?
    
    echo ':mono:M:74:\x01\x4c::/usr/local/bin/mono:' > /proc/sys/fs/binfmt_misc/register
    
    return $RETVAL
}

function stop () {
    echo "-1" > /proc/sys/fs/binfmt_misc/mono
    
    RETVAL=$?
    
    return $RETVAL
}

case "$1" in
    start)start;;
    stop)stop;;
    status)[[ -e /proc/sys/fs/binfmt_misc/mono ]] && echo .NET exe registration enabled || echo .NET exe registration disabled ;;
    restart);;
    reload);;
    *) echo "Usage: ${0##*/} {start|status|stop}"; exit 1;
esac

exit $RETVAL

Links

The binfmt_misc home-page - http://www.tat.physik.uni-tuebingen.de/~rguenth/linux/binfmt_misc.html
The binfmt_misc documentation - http://www.linuxhq.com/kernel/v2.4/doc/binfmt_misc.txt.html
ECMA file format extensions - http://www.southern-storm.com.au/docs/pnettools_10.html

Author

Per Arneng [EMAIL PROTECTED]

Reply via email to