You've already forked linux-packaging-mono
data
debian
docs
HtmlAgilityPack
deploy
sources
mono-api-assembly.html
mono-api-class.html
mono-api-codegen.html
mono-api-counters.html
mono-api-debug.html
mono-api-decimal.html
mono-api-domains.html
mono-api-dynamic-codegen.html
mono-api-embedding.html
mono-api-exc.html
mono-api-gc.html
mono-api-gchandle.html
mono-api-image.html
mono-api-internal.html
mono-api-jit.html
mono-api-jitinternal.html
mono-api-marshal.html
mono-api-metadata.html
mono-api-methods.html
mono-api-object.html
mono-api-profiler.html
mono-api-reflection.html
mono-api-security.html
mono-api-string.html
mono-api-threads.html
mono-api-type.html
mono-api-types.html
mono-api-unsorted.html
mono-api-utils.html
mono-api-vm.html
mono-api-wapi.html
svgs
ChangeLog
Makefile.am
Makefile.in
README
TODO
abc-removal.txt
api-style.css
assembly-bundle
check-coverage
check-exports
convert.cs
current-api
docs.make
documented
embedded-api
exceptions
exdoc
file-share-modes
gc-issues
gc-variables-in-c
glossary.txt
ignore
internal-calls
ir-desc
jit-imt
jit-thoughts
jit-trampolines
local-regalloc.txt
magic.diff
mini-doc.txt
mono-api-metadata.html
mono-file-formats.config
mono-file-formats.source
mono-tools.config
mono-tools.source
mono_handle_d
monoapi.source
new-regalloc
object-layout
opcode-decomp.txt
precise-gc
produce-lists
public
public-api
release-notes-1.0.html
remoting
ssapre.txt
stack-alignment
stack-overflow.txt
threading
toc.xml
tree-mover.txt
unmanaged-calls
eglib
external
ikvm-native
libgc
m4
man
mcs
mono
msvc
po
runtime
samples
scripts
support
tools
AUTHORS
COPYING.LIB
ChangeLog.REMOVED.git-id
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
build-mingw32.sh
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-core.spec
mono-core.spec.in
mono-uninstalled.pc.in
test-driver
winconfig.h
169 lines
5.9 KiB
HTML
169 lines
5.9 KiB
HTML
<h2>Embedding Mono</h2>
|
|
|
|
<p>The simplest way of embedding Mono is illustrated here:
|
|
<pre>
|
|
int main (int argc, char *argv)
|
|
{
|
|
/*
|
|
* Load the default Mono configuration file, this is needed
|
|
* if you are planning on using the dllmaps defined on the
|
|
* system configuration
|
|
*/
|
|
mono_config_parse (NULL);
|
|
|
|
/*
|
|
* mono_jit_init() creates a domain: each assembly is
|
|
* loaded and run in a MonoDomain.
|
|
*/
|
|
MonoDomain *domain = mono_jit_init ("startup.exe");
|
|
|
|
/*
|
|
* Optionally, add an internal call that your startup.exe
|
|
* code can call, this will bridge startup.exe to Mono
|
|
*/
|
|
mono_add_internal_call ("Sample::GetMessage", getMessage);
|
|
|
|
/*
|
|
* Open the executable, and run the Main method declared
|
|
* in the executable
|
|
*/
|
|
MonoAssembly *assembly = mono_domain_assembly_open (domain, "startup.exe");
|
|
|
|
if (!assembly)
|
|
exit (2);
|
|
/*
|
|
* mono_jit_exec() will run the Main() method in the assembly.
|
|
* The return value needs to be looked up from
|
|
* System.Environment.ExitCode.
|
|
*/
|
|
mono_jit_exec (domain, assembly, argc, argv);
|
|
}
|
|
|
|
/* The C# signature for this method is: string GetMessage () in class Sample */
|
|
MonoString*
|
|
getMessage ()
|
|
{
|
|
return mono_string_new (mono_domain_get (), "Hello, world");
|
|
}
|
|
</pre>
|
|
|
|
<h4><a name="api:mono_jit_init">mono_jit_init</a></h4>
|
|
<h4><a name="api:mono_jit_exec">mono_jit_exec</a></h4>
|
|
<h4><a name="api:mono_set_dirs">mono_set_dirs</a></h4>
|
|
<h4><a name="api:mono_main">mono_main</a></h4>
|
|
<h4><a name="api:mono_parse_default_optimizations">mono_parse_default_optimizations</a></h4>
|
|
|
|
<h4><a name="api:mono_jit_cleanup">mono_jit_cleanup</a></h4>
|
|
<h4><a name="api:mono_set_defaults">mono_set_defaults</a></h4>
|
|
|
|
<h3>Internal Calls</h3>
|
|
|
|
<p>The Mono runtime provides two mechanisms to expose C code
|
|
to the CIL universe: internal calls and native C
|
|
code. Internal calls are tightly integrated with the runtime,
|
|
and have the least overhead, as they use the same data types
|
|
that the runtime uses.
|
|
|
|
<p>The other option is to use the Platform Invoke (P/Invoke)
|
|
to call C code from the CIL universe, using the standard
|
|
<a href="http://www.mono-project.com/Interop_with_Native_Libraries">P/Invoke</a>
|
|
mechanisms.
|
|
|
|
<p>To register an internal call, use this call you use the
|
|
<a href="#api:mono_add_internal_call"><tt>mono_add_internal_call</tt>
|
|
routine.
|
|
|
|
<h4><a name="api:mono_add_internal_call">mono_add_internal_call</a></h4>
|
|
|
|
<h3>P/Invoke with embedded applications</h3>
|
|
|
|
<p>Unlike internal calls, Platform/Invoke is easier to use and
|
|
more portable. It allows you to share code with Windows and
|
|
.NET that have a different setup for internal calls to their
|
|
own runtime.
|
|
|
|
<p>Usually P/Invoke declarations reference external libraries
|
|
like:
|
|
|
|
<pre>
|
|
[DllImport ("opengl")]
|
|
void glBegin (GLEnum mode)
|
|
</pre>
|
|
|
|
<p>Mono extends P/Invoke to support looking up symbols not in
|
|
an external library, but looking up those symbols into the
|
|
same address space as your program, to do this, use the
|
|
special library name "__Internal". This will direct Mono to
|
|
lookup the method in your own process.
|
|
|
|
<p>There are situations where the host operating system does
|
|
not support looking up symbols on the process address space.
|
|
For situations like this you can use
|
|
the <a href="#api:mono_dl_register_library">mono_dl_register_library</a>.
|
|
|
|
<h4><a name="api:mono_dl_register_library">mono_dl_register_library</h4>
|
|
|
|
<h3>Data Marshalling</h3>
|
|
|
|
<p>Managed objects are represented as <tt>MonoObject*</tt>
|
|
types. Those objects that the runtime consumes directly have
|
|
more specific C definitions (for example strings are of type
|
|
<tt>MonoString *</tt>, delegates are of type
|
|
<tt>MonoDelegate*</tt> but they are still <tt>MonoObject
|
|
*</tt>s).
|
|
|
|
<p>As of Mono 1.2.x types defined in mscorlib.dll do not have
|
|
their fields reordered in any way. But other libraries might
|
|
have their fields reordered. In these cases, Managed
|
|
structures and objects have the same layout in the C# code as
|
|
they do in the unmanaged world.
|
|
|
|
<p>Structures defined outside corlib must have a specific
|
|
StructLayout definition, and have it set as sequential if you
|
|
plan on accessing these fields directly from C code.
|
|
|
|
<p><b>Important</B> Internal calls do not provide support for
|
|
marshalling structures. This means that any API calls that
|
|
take a structure (excluding the system types like int32,
|
|
int64, etc) must be passed as a pointer, in C# this means
|
|
passing the value as a "ref" or "out" parameter.
|
|
|
|
<h3>Mono Runtime Configuration</h3>
|
|
|
|
<p>Certain features of the Mono runtime, like DLL mapping, are
|
|
available through a configuration file that is loaded at
|
|
runtime. The default Mono implementation loads the
|
|
configuration file from <tt>$sysconfig/mono/config</tt>
|
|
(typically this is <tt>/etc/mono/config</tt>).
|
|
|
|
<p>See the <tt>mono-config(5)</tt> man page for more details
|
|
on what goes in this file.
|
|
|
|
<p>The following APIs expose this functionality:
|
|
|
|
<h4><a name="api:mono_config_parse">mono_config_parse</a></h4>
|
|
<h4><a name="api:mono_config_parse_memory">mono_config_parse_memory</a></h4>
|
|
<h4><a name="api:mono_get_config_dir">mono_get_config_dir</a></h4>
|
|
|
|
<h3>Function Pointers</h3>
|
|
|
|
<p>To wrap a function pointer into something that the Mono
|
|
runtime can consume, you should use the mono_create_ftnptr.
|
|
This is only important if you plan on running on the IA64
|
|
architecture. Otherwise you can just use the function
|
|
pointer address.
|
|
|
|
<h4><a name="api:mono_create_ftnptr">mono_create_ftnptr</a></h4>
|
|
|
|
<h3>Advanced Execution Setups</h3>
|
|
|
|
<p>These are not recommended ways of initializing Mono, they
|
|
are done internally by mono_jit_init, but are here to explain
|
|
what happens internally.
|
|
|
|
<h4><a name="api:mono_runtime_exec_managed_code">mono_runtime_exec_managed_code</a></h4>
|
|
<h4><a name="api:mono_runtime_exec_main">mono_runtime_exec_main</a></h4>
|
|
<h4><a name="api:mono_init_from_assembly">mono_init_from_assembly</a></h4>
|
|
<h4><a name="api:mono_init">mono_init</a></h4>
|
|
<h4><a name="api:mono_init_version">mono_init_version</a></h4>
|