[Mono-list] .NET exe's at cmdline mini tutorial

Per Arneng pt99par@student.bth.se
Sun, 21 Apr 2002 17:20:54 +0200


--------------Boundary-00=_UYBXRKV4YPYETK8Q2V9F
Content-Type: text/plain;
  charset="us-ascii"
Content-Transfer-Encoding: 8bit

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
--------------Boundary-00=_UYBXRKV4YPYETK8Q2V9F
Content-Type: text/html;
  charset="us-ascii";
  name="PELinux.html"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="PELinux.html"


<html>

<head>
  <title>.NET executables directly from the command line in Linux</title>
</head>

<body>

<h1>.NET executables directly from the command line in Linux</h1>
<p>
  <h2>Abstract</h2>
  This is a mini tutorial on how to make .NET executables run directly from the command line like this:
  <p>
    <pre>[pure@p tmp]$ mcs.exe</pre>
  
    or
  
    <pre>[pure@p test]$ ./my_dotnet_app.exe</pre>
 
<p>
  <h2>Registering .NET executables with the binfmt_misc kernel module</h2>
<p>
  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:
  <pre>:name:type:offset:magic:mask:interpreter:</pre>
<p>
  Now as you see we need some info to create this string:
  <p>

  <table border='1'>
    <tr><td><b> Name: </b></td><td> &nbsp; mono </td><td>can be anything you wish</td></tr>
    <tr><td><b> Type: </b></td><td> &nbsp; M </td><td>specifies that we want to use the magic number instead of the extension</td></tr>
    <tr><td><b> Offset: </b></td><td>&nbsp; 74 </td><td>Nr of bytes to where the magic number is found inside the .exe file</td></tr
    <tr><td><b> Magic:</b></td><td>&nbsp; 01 4c </td><td>Magic number as stated <a href='http://www.southern-storm.com.au/docs/pnettools_10.html'>here</a></td></tr>
    <tr><td><b> Mask: </b></td><td> &nbsp;  </td><td>should be left empty in this case</td></tr>
    <tr><td><b> Interpreter: </b></td><td> &nbsp; /usr/local/bin/mono </td><td>path to interpreter or jit ex: mono or mint</td></tr>
  </table>
<p>
  The string we end up with now looks like this:

  <pre>:mono:M:74:\x01\x4c::/usr/local/bin/mono:</pre>

  <p>
   Now lets register this string with binfmt_misc. To achieve that we do like this (as root ):
  <p>
  
  <pre>[root@p tmp]# echo ':mono:M:74:\x01\x4c::/usr/local/bin/mono:' > /proc/sys/fs/binfmt_misc/register</pre>

<p>
  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.
<p>
 
<pre>[pure@p test]$ ./my_dotnet_app.exe</pre>

<p> 
 You probably want to have this registration done at boot time so then you could just add :
<p>
 <pre>echo ':mono:M:74:\x01\x4c::/usr/local/bin/mono:' > /proc/sys/fs/binfmt_misc/register</pre>
<p> 
to one of your rc scripts of your choice.

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

<h2>Sample script for use at startup in /etc/init.d</h2>
<p>
<table border='0' bgcolor='#DDDDDD' width='90%'><tr><td>
<pre>
#!/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

</pre>

</td></tr></table>
<p>

<h2>Links</h2>
 The binfmt_misc home-page - <a href='http://www.tat.physik.uni-tuebingen.de/~rguenth/linux/binfmt_misc.html'> http://www.tat.physik.uni-tuebingen.de/~rguenth/linux/binfmt_misc.html</a><br>
 The binfmt_misc documentation - <a href='http://www.linuxhq.com/kernel/v2.4/doc/binfmt_misc.txt.html'>http://www.linuxhq.com/kernel/v2.4/doc/binfmt_misc.txt.html</a><br>
 ECMA file format extensions - <a href='http://www.southern-storm.com.au/docs/pnettools_10.html'>http://www.southern-storm.com.au/docs/pnettools_10.html</a><br>
<p>
<h2>Author</h2>
Per Arneng <a href='mailto:pt99par@student.bth.se'>pt99par@student.bth.se</a>
</body>
</html>








--------------Boundary-00=_UYBXRKV4YPYETK8Q2V9F--