Bugzilla – Bug 315892
[Embedding Mono] Accessing fields of structs
Last modified: 2007-09-15 21:24:46 UTC
---- Reported by priewasser@gmx.at 2004-08-26 20:05:47 MST ---- Description of Problem: When accessing a field of a (C#-)struct from C, the methods mono_field_set_value and mono_field_get_valus access the wrong momory position. When calling the methods with an address (address of the MonoObject) reduced by 8 (size of a MonoObject?) the methods work. I've attached a program that shows the the problem with mono_field_set_value. In this program I tried to set "i" to 11 and write out "i" afterwards. Steps to reproduce the problem: 1. compile T.cs (mcs /t:library -out:T.dll T.cs) 2. compile T.c (gcc -o T T.c `pkg-config --cflags --libs mono`) 3. run T (./T) Actual Results: any number Expected Results: 11 How often does this happen? always Additional Information: There is no problem accessing a field of a class or a static field of a struct. ---- Additional Comments From priewasser@gmx.at 2004-08-26 20:08:58 MST ---- Created an attachment (id=166704) T.cs (the C# struct) ---- Additional Comments From priewasser@gmx.at 2004-08-26 20:12:05 MST ---- Created an attachment (id=166705) T.c (the program which tries to access the C# struct) ---- Additional Comments From grompf@sublimeintervention.com 2004-08-27 18:16:46 MST ---- Friedrich, I think the problem is, you're loading a struct as a class, structs are directly allocated on the stack and are passed by value. If you change T to a class; your code works as it should; ---- Additional Comments From bmaurer@users.sf.net 2004-08-30 16:58:56 MST ---- The problem is that you are calling a value type method with the `this' argument as an object. The value type methods expect to have a pointer to the value type as an object. You need to say: mono_runtime_invoke (method, mono_object_unbox (object), NULL, NULL)); ---- Additional Comments From priewasser@gmx.at 2004-08-30 19:49:14 MST ---- If this isn't a bug, shouldn't there be a posibility to access the public fields of a struct, if [StructLayout (LayoutKind.Sequential)] isn't set, anyway? Or is there a posibility to use mono_object_unbox without LayoutKind. Sequential? ---- Additional Comments From bmaurer@users.sf.net 2004-08-30 20:05:34 MST ---- /* accessors for fields and properties */ void mono_field_set_value (MonoObject *obj, MonoClassField *field, void *value); void mono_field_static_set_value (MonoVTable *vt, MonoClassField *field, void *value); void mono_field_get_value (MonoObject *obj, MonoClassField *field, void *value); void mono_field_static_get_value (MonoVTable *vt, MonoClassField *field, void *value); [in object.h] NB: if you want to access fields from unmanaged, using a sequential type is the best way to do it. That way you can access stuff with -> in C, which is much cleaner than method calls. ---- Additional Comments From priewasser@gmx.at 2004-08-31 12:31:29 MST ---- I'm using (must use) reflection to find the fields of a struct, so I can't find the correct declaration order (if the .Net 2.0 Beta documentation is correct - or have I missed something?). Because of that I use mono_object_new to create C# structs and need to use a method to access the fields of the struct. But till now I know no clean way to access those fields. (If you could post a little program which would do the same then T.c it might help) Imported an attachment (id=166704) Imported an attachment (id=166705)