[Mono-devel-list] IntPtr and accessing structures

Carlos Alberto Cortez carlos at unixmexico.org
Thu Aug 12 00:38:02 EDT 2004


When wrapping, it looks that the best way is to keep the data structures
from the C world hidden, and offer functions to access its members
(that's the way Gtk# works, as far as I know). 

However, if for some reason you can't, you should try
System.Runtime.InteropServices.Marshal class methods, which include:
PtrToStringAnsi, PtrToStructure, and some other that can fit your needs.
There is also a static IntPtr.ToPointer method, which could help you.

Note that since that operation would have to pass a data structure from
unmanaged to managed world, it would represent an extra operation and
could impact the performance. 

I suggest you to read the Jonathan Pryor's "Managed Code Interop" doc in
Monkey Guide (monkeyguide in cvs), or at


El mié, 11-08-2004 a las 23:08, Jonathan Turner escribió:
> Hopefully this is the correct list.  I couldn't find a "mono-users" or
> "mono-beginner-developers" list.
> My question's pretty straight forward.
> I'm trying to connect to SDL using the Tao.Sdl.  I use:
> IntPtr mySurface = Sdl.SDL_SetVideoMode(640, 480, 16,
> Which gives me a window.  That's great, exactly what I wanted.
> Now I want to access mySurface, but it's an IntPtr, so I can't access
> any members that are SDL_Surface type members.  I'm a C/C++ guy, so I
> think let's cast it.
> I try something like:
> unsafe {
> 	Sdl.SDL_Surface *mySurfaceReal = (Sdl.SDL_Surface *)mySurface.ToPointer
> ();
> 	Console.WriteLine("Surface bytes per: {0}", mySurfaceReal->pitch);
> 	Console.WriteLine("Width: {0}", mySurfaceReal->w);
> 	Console.WriteLine("Height: {0}", mySurfaceReal->h);
> 	Console.WriteLine("My address for pixels: {0}", mySurfaceReal->pixels);
> }
> Which doesn't work.
> Any ideas?
> I'm trying to get to the pixels member, which is a pointer to the screen
> data.  Once I learn how to manipulate that I'm off and running, it's
> just these bits I can't seem to wrap my head around.
> Jonathan
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list

More information about the Mono-devel-list mailing list