[Mono-list] Help with embedded mono: mono_runtime_object_init

Rod rodney.foley at lumension.com
Wed May 18 12:26:13 EDT 2011


Hi, Robert – thanks for the information. As it happens the managed class I am
trying to instantiate is what I posted, it looks like this verbatim:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EmbeddedMonoExampleAssembly
{
        public class ExampleClass
        {
                public ExampleClass()
                {
                }

                public int ExampleMethod(string exampleString)
                {
                        return 42;
                }
        }
}

It seems like the stars are aligned and everything should work but no dice. 

I’ve done some more experiments but I’m still dead-ended. I adjusted the C#
code to add another .ctor that takes a string:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EmbeddedMonoExampleAssembly
{
       public class ExampleClass
       {
              public ExampleClass()
              {
              }

              public ExampleClass(string exampleString)
              {
              }

              public int ExampleMethod(string exampleString)
              {
                     return 42;
              }
       }
}

And I altered the C code to locate the 1-param .ctor and attempted to call
it using “mono_runtime_invoke”, but still no dice:

#include "stdafx.h"
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include &lt;boost/filesystem.hpp&gt;
#include &lt;mono/jit/jit.h&gt;  
#include &lt;mono/metadata/assembly.h&gt;
#include "StlUtfConverter.h"

      
// Main entry point
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);

       unsigned char useDefaultCtor = 0;
       if (useDefaultCtor)
       {
              ::mono_runtime_object_init (instance); // <--- fail here
       }
       else
       {
              // Find the .ctor
              void* iter;
              MonoMethod * m;
              MonoMethod * ctor = NULL;
              iter = NULL;
              while ((m = ::mono_class_get_methods (klass, &iter))) 
              {
                     const char * method_name = ::mono_method_get_name (m);
                     if (strcmp (method_name, ".ctor") == 0) 
                     {
                           MonoMethodSignature * sig =
::mono_method_signature (m);
                           if (::mono_signature_get_param_count (sig) == 1) 
                           {
                                  ctor = m;
                                  break;
                           }
                     }
              }

              // Call the .ctor
              void *args [1];
              args [0] = ::mono_string_new (domain, "take this");
              ::mono_runtime_invoke (ctor, instance, args, NULL); // <---
fail here
       }

       // Cleanup    
       ::mono_jit_cleanup (domain);
       return 0;
}

When I call “mono_runtime_invoke” the process still hard closes without
warning. I have validated in the code that “MonoMethod * ctor” and I can
even walk through the “mono_class_get_methods’ loop and watch it find every
method, including the .ctors. I’m so close, it SEES the darn class and .ctor
but I can’t call them. Any thoughts? Do you think there’s a possibility that
the 2.11 build of mono I’m using might actually be broken in this respect
(at least on windows)?

Thanks in advance.


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


More information about the Mono-list mailing list