473,796 Members | 2,788 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 3827
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
27466
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
9780
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
1308
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
1230
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
3181
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
9685
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
10461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10190
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
10019
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...
0
9057
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7555
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
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.