I’m trying to embed mono in an application i’m writing, but mono_field_set_value doesn’t behave like I think it should. Here is an example:
monotest.c:
#include <mono/jit/jit.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/debug-helpers.h>
#include <stdio.h>
#define TRUE 1
int main(){
MonoDomain *domain;
MonoAssembly *assembly;
MonoImage *image;
MonoClass *klass;
MonoObject *object;
MonoClassField *field;
MonoMethodDesc *desc;
MonoMethod *method;
int val;
domain = mono_jit_init ("Domain");
assembly = mono_domain_assembly_open (domain, "T.DLL");;
image = mono_assembly_get_image(assembly);
klass = mono_class_from_name(image, "", "T");
mono_class_init (klass);
object = mono_object_new (domain, klass);
val=11;
field = mono_class_get_field_from_name(klass,"i");
mono_field_set_value(object,field,&val);
//mono_field_set_value((MonoObject*)((void*)object-8),field,&val);
printf("val = %d\n", val);
desc = mono_method_desc_new (":Write()",TRUE);
method = mono_method_desc_search_in_class (desc, klass);
mono_runtime_invoke(method,object,NULL,NULL);
return 0;
}
T.cs
public struct T
{
public int i;
public void Write()
{
System.Console.WriteLine(i);
}
}
When run, i expect the output:
val = 11
11
but the second number (printed by the mono runtime) is random. What am I doing wrong?
The argument to mono_runtime_invoke() is incorrect, it needs to be the unboxed pointer, since the type T is a struct.
From the documentation at http://docs.go-mono.com/index.aspx?link=xhtml%3Adeploy%2Fmono-api-methods.html: