473,785 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TCP/IP endian conversion with ICustomMarshale r?

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 ICustomMarshale r the way to go here? I found a dummy
ICustomMarshale r 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.Sequ ential, CharSet=CharSet .Ansi, Pack = 1)]
struct PT_DATAFRAME
{
[MarshalAs(Unman agedType.Custom Marshaler, MarshalTypeRef =
typeof(PassThru .DummyMarshaler ))]
public int myValue;
};

***The custom marshaler:
public class DummyMarshaler : ICustomMarshale r
{
DummyMarshaler( )
{

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

public unsafe object MarshalNativeTo Managed( System.IntPtr
pNativeData )
{
//Console.WriteLi ne( "DummyMarshaler .MarshalNativeT oManaged" );
return new System.Int32();
}

public unsafe IntPtr MarshalManagedT oNative( object ManagedObj )
{
//Console.WriteLi ne( "DummyMarshaler .MarshalManated ToNative" );

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

public unsafe void CleanUpNativeDa ta( IntPtr pNativeData )
{
//Console.WriteLi ne( "DummyMarshaler .CleanUpNativeD ata" );
return;
}

public int GetNativeDataSi ze()
{
//Console.WriteLi ne( "DummyMarshaler .GetNativeData" );

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

public void CleanUpManagedD ata( object ManagedObj )
{
//Console.WriteLi ne( "DummyMarshaler .CleanUpManaged Data" );
return;
}
}

Nov 16 '05 #1
1 3826
IPAddress.Netwo rkToHostOrder
IPAddress.HostT oNetworkOrder

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

--
William Stacey, MVP

"Marquee" <al*********@ho tmail.com> wrote in message
news:ce******** @odak26.prod.go ogle.com...
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 ICustomMarshale r the way to go here? I found a dummy
ICustomMarshale r 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.Sequ ential, CharSet=CharSet .Ansi, Pack = 1)]
struct PT_DATAFRAME
{
[MarshalAs(Unman agedType.Custom Marshaler, MarshalTypeRef =
typeof(PassThru .DummyMarshaler ))]
public int myValue;
};

***The custom marshaler:
public class DummyMarshaler : ICustomMarshale r
{
DummyMarshaler( )
{

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

public unsafe object MarshalNativeTo Managed( System.IntPtr
pNativeData )
{
//Console.WriteLi ne( "DummyMarshaler .MarshalNativeT oManaged" );
return new System.Int32();
}

public unsafe IntPtr MarshalManagedT oNative( object ManagedObj )
{
//Console.WriteLi ne( "DummyMarshaler .MarshalManated ToNative" );

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

public unsafe void CleanUpNativeDa ta( IntPtr pNativeData )
{
//Console.WriteLi ne( "DummyMarshaler .CleanUpNativeD ata" );
return;
}

public int GetNativeDataSi ze()
{
//Console.WriteLi ne( "DummyMarshaler .GetNativeData" );

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

public void CleanUpManagedD ata( object ManagedObj )
{
//Console.WriteLi ne( "DummyMarshaler .CleanUpManaged Data" );
return;
}
}


Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
27465
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
5
10222
by: glueless | last post by:
I have to read files from UNIX systems on my PC. The problem is that these binary files are in big endian and I need to convert them. I saw that there are functions ntohl for my visual C++ (4.0), but I don't know in what librarieses these are. I want to kep the code as simple as possible, since I also want to compile it on UNIX systems. Does anyone know of existing functions that do the conversion as I am a bit lazy to program thi myself?
2
9777
by: Mehta Shailendrakumar | last post by:
Hi, I am sending this question again as new question rather than reply to old question Please refer below: struct raw_data { unsigned char x; unsigned char y; }; union full_data
0
1307
by: Carlos Guzmán Álvarez | last post by:
Hello: I'm trying to implement a custom marshaler for a field of an struct, the field is an array of struct type, but it's not working, i want to know if this is possible ?? The structure: public struct XSQLDA
0
1227
by: Marquee | last post by:
Hello, When I try to implement a custom marshaler I get an exception when the Marshal.SizeOf() method is invoked. The message is: "Type MyNS.PT_DATAFRAME can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed." Is custom marshaling possible with a struct? Perhaps a better question is help, what am I doing wrong here?
5
4003
by: Marc Gravell | last post by:
Short version: is it possible to control the endian-ness of BitConverter? - or are there any /framework/ methods like BigEndianBitConverter.GetBytes(long) and .ToInt64()? (I am not after equivalent code; I already have that) === Background: I'm dealing with Sql-Server timestamp fields ; in some circumstances (e.g. OpenXml) they need to be representated using the bigint sql-datatype (equiv. to the c# long); the
11
1936
by: kolmogolov | last post by:
hi, it's not really an endian problem. I think I must be missing something else ... The problem can be reduced to different results of the following two segments of codes: (cut and pasted verbatim) 1.
8
5325
by: ma740988 | last post by:
Data stored on a storage device is byte swapped. The data is big endian and my PC is little. At issue: There's a composite type ( a header ) at the front of the files that I'm trying to read in. I'm trying to _simulate_ the endian conversion in code below but I'm just wondering if there's an ideal way to do this besides what's shown? Padding produces some interesting results. Notice how the parameter d is different in the print outs...
6
3180
by: Javier | last post by:
Hello people, I'm recoding a library that made a few months ago, and now that I'm reading what I wrote I have some questions. My program reads black and white images from a bitmap (BMP 24bpp without compression). It has it's words and dwords stored in little- endian, so I do a conversion to big-endian when reading full words or dwords. I have done this because my system is big-endian. But now... what if one compiles the library in a...
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9957
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.