[Mono-list] accessing struct instance from managed code

Jonathan Pryor jonpryor@vt.edu
Sat, 26 Feb 2005 09:32:32 -0500


On Fri, 2005-02-25 at 14:02 -0500, Nigel Benns wrote:
> ok... this is the lowdown :)
> 
> in C:
> 
> struct try {
> 
>    int *blah;
> 
> }

In C#:

	struct try {
		public System.IntPtr blah;
	}

> int main() {
> 
>    object *meh;
>    int *thing;
> 
>    meh = try_new();
> 
>    thing = meh->blah; //this is what I want to do from C#
> 
> }

In C#:

	class Test {
		[DllImport ()]
		private static extern IntPtr try_new ();

		public static void Main ()
		{
			System.IntPtr meh = try_new ();
			try t = (try) 
			  System.Runtime.InteropServices.Marshal
			  .PtrToStructure (meh, typeof(try));

			System.IntPtr thing = t.blah;
			int thing_value = 
			  System.Runtime.InteropServices.Marshal
			  .ReadInt32 (thing);
		}
	}

Note: I haven't actually compiled this, but it should work.

Also, I'm not sure what an `object*' is supposed to be in your C code.
If you're trying to use GTK+-style OO programming, things will be very
different, and a glue library should instead be used.

 - Jon