view VBoxFrontend/VBoxMono.c @ 2:f1deea9c97a0

Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
author Ivo Smits
date Wed, 04 May 2011 16:10:08 +0200
parents
children
line wrap: on
line source
???/* To compile, you may have to specify:
 *  The include paths for mono: -I /usr/include/mono-1.0/ -I /usr/include/glib-2.0/ -I /usr/lib/glib-2.0/include/
 *  The location of the VBoxXPCOMC.so library: /usr/lib/virtualbox/VBoxXPCOMC.so
 *  The location of the mono library (if an alternative build is to be used): /home/ivo/mono/devtest/lib/libmono-2.0.so
 *  To link against the mono library (if the system wide installed version is to be used): -lmono
 *  Where to look for the VBoxXPCOMC.so library at run time: -Wl,-R,/usr/lib/virtualbox
 *  Where to look for the mono library at run time (if non-standard): -R,/home/ivo/mono/devtest/lib/
 *
 * The resulting commandline may look like this:
   gcc VBoxMono.c -o VBoxMono -lmono \
    -I /usr/include/mono-1.0/ -I /usr/include/glib-2.0/ -I /usr/lib/glib-2.0/include/ \
    /usr/lib/virtualbox/VBoxXPCOMC.so -Wl,-R,/usr/lib/virtualbox
 * The resulting commandline may look like this if mono is installed in an alternative location:
   gcc VBoxMono.c -o VBoxMono \
    -I /usr/include/mono-1.0/ -I /usr/include/glib-2.0/ -I /usr/lib/glib-2.0/include/ \
    /usr/lib/virtualbox/VBoxXPCOMC.so -Wl,-R,/usr/lib/virtualbox \
    /home/ivo/mono/devtest/lib/libmono-2.0.so -Wl,-R,/home/ivo/mono/devtest/lib/
 * Then make the binary owned by root and set the suid bit:
   sudo chown root VBoxMono
   sudo chmod u+s VBoxMono
*/

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <mono/jit/jit.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/mono-config.h>

extern int RTR3InitAndSUPLib();

int main(int argc, char *argv[]) {
        if (argc < 2) {
                fprintf(stderr, "VBoxMono: Usage: %s assembly.exe\n", argv[0]);
                return 1;
        }
        int rc = RTR3InitAndSUPLib();
        if (rc != 0) {
                fprintf(stderr, "VBoxMono: Could not initialize VirtualBox driver (%d)\n", rc);
                return rc;
        }
        int gid = getuid();
        if (gid != getegid()) setregid(gid, gid);
        int uid = getuid();
        if (uid != geteuid()) setreuid(uid, uid);
        mono_config_parse(NULL);
        //mono_set_dirs("/home/ivo/mono/devtest/lib", "/etc");
        MonoDomain *domain = mono_jit_init_version("VirtualBox", "v2.0.50727");
        if (!domain) {
                fprintf(stderr, "VBoxMono: Could not create application domain\n");
                return 1;
        }
        MonoAssembly *assembly = mono_domain_assembly_open(domain, argv[1]);
        if (!assembly) {
                fprintf(stderr, "VBoxMono: Could not load assembly %s\n", argv[1]);
                mono_jit_cleanup(domain);
                return 1;
        }
        int ret = mono_jit_exec(domain, assembly, argc - 1, argv + 1);
        mono_jit_cleanup(domain);
        return ret;
}