2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / samples / embed / teste.c
1 #include <mono/jit/jit.h>
2 #include <mono/metadata/environment.h>
3
4 /*
5  * Very simple mono embedding example.
6  * Compile with: 
7  *      gcc -o teste teste.c `pkg-config --cflags --libs mono` -lm
8  *      mcs test.cs
9  * Run with:
10  *      ./teste test.exe
11  */
12
13 static MonoString*
14 gimme () {
15         return mono_string_new (mono_domain_get (), "All your monos are belong to us!");
16 }
17
18 static void main_function (MonoDomain *domain, const char *file, int argc, char** argv)
19 {
20         MonoAssembly *assembly;
21
22         assembly = mono_domain_assembly_open (domain, file);
23         if (!assembly)
24                 exit (2);
25         /*
26          * mono_jit_exec() will run the Main() method in the assembly.
27          * The return value needs to be looked up from
28          * System.Environment.ExitCode.
29          */
30         mono_jit_exec (domain, assembly, argc, argv);
31 }
32
33
34 int 
35 main(int argc, char* argv[]) {
36         MonoDomain *domain;
37         const char *file;
38         int retval;
39         
40         if (argc < 2){
41                 fprintf (stderr, "Please provide an assembly to load");
42                 return 1;
43         }
44         file = argv [1];
45         /*
46          * mono_jit_init() creates a domain: each assembly is
47          * loaded and run in a MonoDomain.
48          */
49         domain = mono_jit_init (file);
50         /*
51          * We add our special internal call, so that C# code
52          * can call us back.
53          */
54         mono_add_internal_call ("MonoEmbed::gimme", gimme);
55
56         main_function (domain, file, argc - 1, argv + 1);
57         
58         retval = mono_environment_exitcode_get ();
59         
60         mono_jit_cleanup (domain);
61         return retval;
62 }
63