[Mono-list] Re: Marshaling unmanaged structure

Javier Diaz javierdiazm at yahoo.com.mx
Mon Aug 29 19:44:57 EDT 2005


Ok, I understand :)

Another question, how should I get access to the nested structures?,
some unmanaged functions need access to the structures..

---------------------------------------
int levels_callback (ev_levels input, ev_levels output){
	......
}
int netstat_callback (ev_netstats stats){
	......
}
int event_handler (event_struct e){
	switch(e.type){
		case EVENT_LEVELS:
			return levels_callback(e.ev.levels.input, 						e.ev.levels.output);
		case EVENT_NETSTAT:
			return netstat_callback(e.ev.netstats);
		default:
			return 0;
	}	
}
---------------------------------------

and the managed structure, may looks like this:

---------------------------------------
  [StructLayout (LayoutKind.Explicit)]
  public struct event_struct {
		[FieldOffset(0)] public IntPtr next;
		[FieldOffset(4)] public int type;

		// ev_levels Union...
		[FieldOffset(8)] public int ev_levels_input;
		[FieldOffset(12)] public int ev_levels_output;

		.
		.
		.

		// ev_netstats Union...
		[FieldOffset(8)] public int ev_netstats_No;
		[FieldOffset(12)] public int ev_netstats_rtt;
  }
---------------------------------------

can I create something like this ?

---------------------------------------
public struct new_event_struct : event_struct {
	public ev_levels levels {
		get { return ....; }
	}
}
---------------------------------------

btw.... thanks for the help 


On Mon, 2005-08-29 at 02:56, Robert Jordan wrote:
> Javier Diaz wrote:
> > Hey all
> > 
> > I have an unmanaged structure that looks like this:
> > 
> > ------------------------------
> > typedef struct event_struct {
> >   struct event_struct *next;
> >     int type;
> >     union {
> >        struct   levels;
> >        struct ev_text text;
> >        struct ev_call_state call;
> >        struct ev_netstats  netstats;
> >        struct ev_url url;
> >        struct ev_video video;
> >        struct ev_registration reg;
> >     } ev;
> > } event;
> > ------------------------------
> > 
> > I don't know how to wrap this structure, I know that the only posible
> > way us by using the attribute [StructLayout (LayoutKind.Explicit]]
> > and the attribute [FieldOffset (..)]
> > 
> > the structures that are inside the union are easy to wrap, they look like this:
> > ------------------------------
> > struct iaxc_ev_levels {
> >   float input;
> >   float output;
> > };
> > ------------------------------
> > 
> > Any ideas?
> 
> The default marshaller cannot handle nested structs or unions.
> You have to break the struct into something the framework
> is able to handle:
> 
> [StructLayout (LayoutKind.Explicit)]
> public struct event_struct
> {
>    [FieldOffset(0)] public IntPtr next;
>    [FieldOffset(4)] public int type;
>    // first union
>    [FieldOffset(8)] public float input;
>    [FieldOffset(12)] public float output;
>    // next union ...
>    [FieldOffset(8)] public ...
> }
> 
> The "next" ptr must be manually marshalled using Marshal.PtrToStructure.
> 
> Rob
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
-- 

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis! 
Regístrate ya - http://correo.yahoo.com.mx/ 


More information about the Mono-list mailing list