Bug 315892 (MONO64231) - [Embedding Mono] Accessing fields of structs
Summary: [Embedding Mono] Accessing fields of structs
Status: RESOLVED INVALID
Alias: MONO64231
Product: Mono: Runtime
Classification: Mono
Component: misc (show other bugs)
Version: unspecified
Hardware: Other All
: P3 - Medium : Normal
Target Milestone: ---
Assignee: Mono Bugs
QA Contact: Mono Bugs
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-08-27 03:05 UTC by Friedrich Priewasser
Modified: 2007-09-15 21:24 UTC (History)
0 users

See Also:
Found By: ---
Services Priority:
Business Priority:
Blocker: ---
Marketing QA Status: ---
IT Deployment: ---


Attachments
T.cs (the C# struct) (99 bytes, text/plain)
2004-08-27 03:08 UTC, Thomas Wiest
Details
T.c (the program which tries to access the C# struct) (955 bytes, text/plain)
2004-08-27 03:12 UTC, Thomas Wiest
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Wiest 2007-09-15 18:52:09 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)