TCP/IP endian conversion with ICustomMarshaler?

M

Marquee

Hello,

I have a class that I would like to serialize to non .NET TCP/IP
computers. Therefore the serial data has to endian aware i.e. has to be
converted from Intel little endian to network format (big endian).

Is the ICustomMarshaler the way to go here? I found a dummy
ICustomMarshaler implementation but this seems not to work, because I
get the following error: "Type PT_DATAFRAME can not be marshaled as an
unmanaged structure; no meaningful size or offset can be computed."
when I invoke Marshal.Sizeof( myobject ).

Does anybody know an example of struct serialization and custom
marshaling?
Are other (nice .NET!) methods to for endian neutral programming?
Of course I can shuffle the bytes in the generated stream (yuck).

Thanks!

***The class looks something like this:
[Serializable]
[StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack = 1)]
struct PT_DATAFRAME
{
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef =
typeof(PassThru.DummyMarshaler))]
public int myValue;
};

***The custom marshaler:
public class DummyMarshaler : ICustomMarshaler
{
DummyMarshaler()
{

}
public static ICustomMarshaler GetInstance( string cookie )
{
return new DummyMarshaler();
}

public unsafe object MarshalNativeToManaged( System.IntPtr
pNativeData )
{
//Console.WriteLine( "DummyMarshaler.MarshalNativeToManaged" );
return new System.Int32();
}

public unsafe IntPtr MarshalManagedToNative( object ManagedObj )
{
//Console.WriteLine( "DummyMarshaler.MarshalManatedToNative" );

throw new Exception( "should never get here" );
}

public unsafe void CleanUpNativeData( IntPtr pNativeData )
{
//Console.WriteLine( "DummyMarshaler.CleanUpNativeData" );
return;
}

public int GetNativeDataSize()
{
//Console.WriteLine( "DummyMarshaler.GetNativeData" );

//throw new Exception( "should never get here" );
return 4;
}

public void CleanUpManagedData( object ManagedObj )
{
//Console.WriteLine( "DummyMarshaler.CleanUpManagedData" );
return;
}
}
 
W

William Stacey [MVP]

IPAddress.NetworkToHostOrder
IPAddress.HostToNetworkOrder

Static methods that can convert any short or int to and from.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top