view VBoxFrontend/VBoxMono.c @ 7:a7650e26195f tip

Added support for generating Com Callable Wrappers
author Ivo Smits
date Fri, 06 May 2011 08:12:43 +0200
parents f1deea9c97a0
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 library: /usr/lib/virtualbox/
 *  The location of the mono library (if an alternative build is to be used): /home/ivo/mono/devtest/lib/
 *  To link against the mono library (if the system wide installed version is to be used): -lmono
 *  Where to look for the 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/ -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/ -Wl,-R,/usr/lib/virtualbox \
    /home/ivo/mono/devtest/lib/ -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_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]);
                return 1;
        int ret = mono_jit_exec(domain, assembly, argc - 1, argv + 1);
        return ret;