Mono embedding sample.
authorPaolo Molaro <lupus@oddwiz.org>
Fri, 19 Jul 2002 17:24:56 +0000 (17:24 -0000)
committerPaolo Molaro <lupus@oddwiz.org>
Fri, 19 Jul 2002 17:24:56 +0000 (17:24 -0000)
svn path=/trunk/mono/; revision=5938

samples/embed/test.cs [new file with mode: 0644]
samples/embed/teste.c [new file with mode: 0644]

diff --git a/samples/embed/test.cs b/samples/embed/test.cs
new file mode 100644 (file)
index 0000000..9e6f922
--- /dev/null
@@ -0,0 +1,11 @@
+using System;
+using System.Runtime.CompilerServices;
+
+class Mono {
+       [MethodImplAttribute(MethodImplOptions.InternalCall)]
+       extern static string gimme();
+
+       static void Main() {
+               Console.WriteLine (gimme ());
+       }
+}
diff --git a/samples/embed/teste.c b/samples/embed/teste.c
new file mode 100644 (file)
index 0000000..4727c51
--- /dev/null
@@ -0,0 +1,48 @@
+#include <mono/jit/jit.h>
+
+/*
+ * Very simple mono embedding example.
+ * Compile with: 
+ *     gcc -o teste teste.c `pkg-config --cflags --libs mono`
+ *     mcs test.exe
+ * Run with:
+ *     ./teste test.exe
+ */
+
+static MonoString*
+gimme () {
+       return mono_string_new (mono_domain_get (), "All your monos are belong to us!");
+}
+
+int 
+main(int argc, char* argv[]) {
+       MonoDomain *domain;
+       MonoAssembly *assembly;
+       const char *file;
+       int retval;
+
+       if (argc < 2)
+               return 1;
+       file = argv [1];
+       /*
+        * mono_jit_init() creates a domain: each assembly is
+        * loaded and run in a MonoDomain.
+        */
+       domain = mono_jit_init (file);
+       /*
+        * We add our special internal call, so that C# code
+        * can call us back.
+        */
+       mono_add_internal_call ("Mono::gimme", gimme);
+       assembly = mono_domain_assembly_open (domain, file);
+       if (!assembly)
+               return 2;
+       /*
+        * mono_jit_exec() will run the Main() method in the assembly
+        * and return the value.
+        */
+       retval = mono_jit_exec (domain, assembly, argc - 1, argv + 1);
+       mono_jit_cleanup (domain);
+       return retval;
+}
+