[Mono-list] Embedding Mono

Jon Chambers joncham at gmail.com
Mon Feb 5 22:38:07 EST 2007


Jeremy,
     Please remember to respond to list, not just me. I figured out your
problem. You are not marking your C# methods with the [MethodImplAttribute(
MethodImplOptions.InternalCall)] attribute.

class MonoEmbed
{
    [MethodImplAttribute(MethodImplOptions.InternalCall )]
    extern public static string gimme();
    [MethodImplAttribute(MethodImplOptions.InternalCall )] // you need this
    extern public static int gimme2();
}


Note that your code above does not even compile with mcs or gmcs; they
report an error while csc just gives a warning. You must be compiling
test.cs in VS. That is the bug; I filed one for this #80735. If you add the
correct attribute all your calls should be fine.

Thanks,
Jonathan

On 2/5/07, Jeremy <jswigart at gmail.com> wrote:
>
> Ok, taking the embed example in teste.c, which has this c bound function
>
> static MonoString*gimme ()
> {
>     return mono_string_new (mono_domain_get (), "All your monos are belong
> to us!");
> }
>
> static int gimme2 () // I added this
> {
>     return 10;
> }
>
> and below uses
>
> mono_add_internal_call ("MonoEmbed::gimme", gimme);
> mono_add_internal_call ("MonoEmbed::gimme2", gimme2); // I added this
>
> and here's my seperate test prog,
>
> // Class1.cs
> using System;
> using System.Collections.Generic;
> using System.Text;
> using System.Runtime.CompilerServices;
>
> class MonoEmbed
> {
>     [MethodImplAttribute(MethodImplOptions.InternalCall )]
>     extern public static string gimme();
>     extern public static int gimme2(); // I added this
> }
>
> namespace Test
> {
>     public class Class1
>     {
>         public static void Main(string[] args)
>         {
>             System.Console.WriteLine("Hello From Test App");
>
>             System.Console.WriteLine(MonoEmbed.gimme());
>             System.Console.WriteLine(MonoEmbed.gimme2());
>         }
>     }
> }
>
> This test app works as expected when just calling gimme(), which is what
> the example originally had, but I guess I'm doing something wrong with my
> additional function gimme2, because Mono crashes with the above line that
> calls gimme2().
>
> I'm taking a wild guess that I need to wrap even primitive types somehow,
> but I can't find any documentation for that. I also attempted to make gimme2
> like so
>
> static void gimme2 (MonoString*str), but it too crashed.
>
> Even setting gimme2 as a void function that takes no parameters crashes.
> It's like the only variation that works is the one it comes with. I must be
> doing something horribly wrong.
>
> I added the mono_add_internal_call ("MonoEmbed::gimme2", gimme2); like the
> default gimme function was set up, and I also changed the Embed lines in the
> C# code to match the function signature, ie
>
> class MonoEmbed
> {
>     [MethodImplAttribute(MethodImplOptions.InternalCall)]
>     extern public static string gimme();
>     extern public static void gimme2();
> }
>
> and still no luck.
>
> The error is a popup box with an ok button that says:
> ** ERROR **: file .\mono\metadata\loader.c: line 1969: assertion failed:
> (loc) aborting
>
> So in summary, I have the teste.c example included with mono, working fine
> by default. Add another bound function and it doesn't work anymore.
>
> Any help is greatly appreciated. I compiled libmono and teste.c example,
> as well as my simple test app is msvc 2005.
>
> On 2/5/07, Jon Chambers < joncham at gmail.com> wrote:
> >
> > Lots of people are embedding mono. Please post all of your code,
> > including the C#, for more help.
> >
> > Thanks,
> > Jonathan
> >
> > On 2/5/07, Jeremy < jswigart at gmail.com> wrote:
> >
> > > It's a static method I guess, exactly like the existing function in
> > > the example. I wish I could figure out why it's not working for anything
> > > other than the example function they provide.
> > >
> > > Is noone using the embedded mono stuff?
> > >
> > > On 2/3/07, Jonathan Pryor < jonpryor at vt.edu> wrote:
> > > >
> > > > On Fri, 2007-02-02 at 09:53 -0800, Jeremy wrote:
> > > > > I've gotten the example to work, however the example shows the
> > > > case of
> > > > > a returning mono string. I'm getting crashes in Mono when I
> > > > attempt to
> > > > > define additional functions for the assembly to call back into C
> > > > > with.
> > > > >
> > > > > The example shows this
> > > > >
> > > > > static MonoString *Sample ()
> > > > > {
> > > > >       return mono_string_new (mono_domain_get (), "Hello!");
> > > > > }
> > > > >
> > > > > which works fine, but I can't seem to get functions that take
> > > > arguments working.
> > > > >
> > > > > for example:
> > > > >
> > > > > static MonoString* gimme2 (MonoString*a)
> > > > > {
> > > > > return mono_string_new (mono_domain_get (), "Hello!");
> > > > > }
> > > > >
> > > > > even though I'm not even using the parameter, simply defining
> > > > this,
> > > > > adding it just like mono_add_internal_call, and likewise in the
> > > > > assembly, I get a crash in mono.
> > > > >
> > > > > Is it possible for assembly->c functions to take parameters?
> > > >
> > > > Just a guess, but is your C# method an instance method or a static
> > > > method?  If it's an instance method, you need a `this' pointer in
> > > > your C
> > > > code, e.g.
> > > >
> > > >         static MonoString*
> > > >         gimme2 (MonoObject *this, MonoString *a)
> > > >         {
> > > >                 return mono_string_new (mono_domain_get (),
> > > > "Hello!");
> > > >         }
> > > >
> > > > - Jon
> > > >
> > > >
> > > >
> > >
> > > _______________________________________________
> > > Mono-list maillist  -  Mono-list at lists.ximian.com
> > > http://lists.ximian.com/mailman/listinfo/mono-list
> > >
> > >
> > >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20070205/6ff25be2/attachment-0001.html 


More information about the Mono-list mailing list