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

Robert Jordan robertj at gmx.net
Wed Aug 13 04:35:23 EDT 2008


Hi,

Cameron Villers wrote:
> 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
> mono_runtime_object_init(obj);
> ctor = findClassMethod(_class, "foo (gentity_t ent)");
> gentity_t testEnt;
> // ...setup of testEnt...
> gpointer args[1];
> args[0] = testEnt;

args[0] = &testEnt;

Value types arguments must be passed by reference.

> 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).

Robert

> 
> - Cameron



More information about the Mono-devel-list mailing list