[Mono-list] Help with embedded mono: mono_runtime_object_init

Rod rodney.foley at lumension.com
Tue May 17 13:20:25 EDT 2011


We are presently evaluating embedded mono for use in an existing C++ project.
The evaluation is taking place on windows, and with the help of some people
on this forum previously, we have been successful in building mono,
acquiring a .def file from git and generating a subsequent Visual Studio
2010 import library.

 

The first part of our investigation is bringing the moral equivalent of
“hello world” on-line for embedded mono so as such we have a simple Win32
console project that starts up, boots the mono runtime and then attempts to
load an assembly, locate a class in that assembly and then instantiate it
(essentially replicating the instructions on
http://www.mono-project.com/Embedding_Mono). 

 

So far this has all worked out pretty well but we’re at the point now were
when we call “mono_runtime_object_init” and it causes the process to close.
No exception, no console output, no debug messages – the console simply
exits with a 1. The code we’re using looks as follows:

 

int _tmain(int argc, _TCHAR* argv[])

{

        // Tell mono where to find its brain

        const char * monoAssemblyPath           = "C:\\Program Files
(x86)\\Mono-2.11\\lib";

        const char * monoConfigurationPath      = "C:\\Program Files
(x86)\\Mono-2.11\\etc";

        ::mono_set_dirs(monoAssemblyPath,monoConfigurationPath);

 

        // Initialize the JIT runtime

        const char * domainName = "EmbeddedMonoExample";

        MonoDomain *domain = ::mono_jit_init (domainName);

 

        // Load an assembly from the working directory

        boost::filesystem::path assemblyPath =
operator/(boost::filesystem::current_path(),
"EmbeddedMonoExampleAssembly.dll");

        MonoAssembly * assembly =
::mono_domain_assembly_open(domain,UtfConverter::ToUtf8(assemblyPath.c_str()).c_str());

        

        // Instantiate a class in .NET land

        MonoImage * image = ::mono_assembly_get_image(assembly);

        MonoClass * klass =
::mono_class_from_name(image,"EmbeddedMonoExampleAssembly","ExampleClass");

        MonoObject* instance = ::mono_object_new (domain, klass);

        ::mono_runtime_object_init (instance); <----- fail here

 

        // Cleanup      

        mono_jit_cleanup (domain);

        return 0;

}

 

Note that everything appears to work fine up until we call
“mono_runtime_object_init”. Each of the preceding methods
(mono_assembly_get_image, etc.) return non-null pointers, and we have
verified that the assembly paths are constructed properly. In fact we can
even deliberately sabotage the process by, say changing the test assembly
name to be incorrect and mono_domain_assembly_open starts to fail (returns
null), so we’re comfortable that the test assembly has been located. Any
advice on what we’re doing wrong? Also any advice on where to look when mono
embedded gets unhappy like this, does it write a log? Anything besides hard
downing the process? How do I figure out what steps to take next?

 

By the way, the test assembly "EmbeddedMonoExampleAssembly.dll" is
co-located in the same folder as the test console, and the code for it looks
like this:

 

namespace EmbeddedMonoExampleAssembly

{

        public class ExampleClass

        {

                public ExampleClass()

                {

                }

 

                public int ExamlpeMethod(string exampleString)

                {

                        return 42;

                }

        }

}



--
View this message in context: http://mono.1490590.n4.nabble.com/Help-with-embedded-mono-mono-runtime-object-init-tp3529932p3529932.html
Sent from the Mono - General mailing list archive at Nabble.com.


More information about the Mono-list mailing list