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