[Mono-dev] Proper way to construct objects with mono_runtime_invoke

Cameron Villers icefragment at gmail.com
Tue Aug 12 22:29:37 EDT 2008

Hello all,

I'm in the initial stages of embedding Mono into my application. I
have a C struct like so (simplified):

typedef struct
	float origin[3];
	const char *name;
} gentity_t;

I would like to be able to pass a pointer to an object of this type
(allocated globally on the stack) to my managed code constructor,
which will then store it for internal use:

class foo
    foo(gentity_t ent)
        _ent = ent;
        // etc

The Mono docs state that the way to do this is to get a pointer to the
method and call mono_runtime_invoke (for brevity, I skip asserts on
obj, ctor, and the like, which all pass in my actual code):

MonoObject *obj;
obj = mono_object_new(domain, _class);    // already asserted _class
!= NULL by this point btw
ctor = findClassMethod(_class, "foo (gentity_t ent)");
gentity_t testEnt;
// ...setup of testEnt...
gpointer args[1];
args[0] = testEnt;
mono_runtime_invoke(ctor, obj, args, NULL);

All this results in is a System.NullReferenceException upon trying to
access ent in the managed code. Have I missed something obvious in my
setup? I'm using 1.9.1 release (on amd64 Linux).

Thanks in advance for any help.

- Cameron

More information about the Mono-devel-list mailing list