struct Point1
{
public int x, y;
public Point1(int p1, int p2)
{
x = p1;
y = p2;
}
}
...
Point1 myPoint = new Point1(1,2);
byte[] buffer = new byte[sizeof(Point1)];
fixed (byte* pBuffer = buffer)
{
*((Point1*) pBuffer) = myPoint;
udpClient.Send (pBuffer, buffer.Length); //Error - pBuffer not byte[]
}
--
William Stacey, MCSE
Windows Server MVP
--
Greg
http://www.claritycon.com/
"William Stacey [MVP]" <sta...@mvps.org> wrote in message
news:ekto0bqCCHA.2576@tkmsftngp04...
--
William Stacey, MCSE
Windows Server MVP
"Greg Ewing" <gewing@_NO_SPAM_claritycon.com> wrote in message
news:#lh$aqqCCHA.2072@tkmsftngp02...
public Point1(int p1, int p2)
{
x = p1;
y = p2;
}
public byte [] GetBytes( )
{
byte [] retArray = new byte[ size ];
Buffer.BlockCopy( BitConverter.GetBytes(x), 0, retArray, 0, 4 );
Buffer.BlockCopy( BitConverter.GetBytes(y), 0, retArray, 4, 4 );
// For larger structures, keep on adding to the struct.
return retArray;
}
}
...
Point1 myPoint = new Point1(1,2);
// Note the use of Point1.size, not myPoint.size
udpClient.Send( myPoint.GetBytes(), Point1.size );
Hope this helps,
Chris R
"William Stacey [MVP]" <sta...@mvps.org> wrote in message
news:ekto0bqCCHA.2576@tkmsftngp04...
Note, if the struct contains any array[],
unsafe/fixed will get error
"Cannot take the address or size of a variable of a managed type"
then you could use Marshal & GCHandle like:
// ===============================================================
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
struct YourStruct
{
[MarshalAs( UnmanagedType.ByValArray, SizeConst=8 )]
public char[] txt;
public int num;
}
...
// usage:
YourStruct s;
s.txt = "TestText".ToCharArray();
s.num = 77;
// serialize:
byte[] a = RawSerializeEx( s );
// reverse = deserialize:
YourStruct sr = (YourStruct) RawDeserializeEx( a, typeof(YourStruct) );
public static object RawDeserializeEx( byte[] rawdatas, Type anytype )
{
int rawsize = Marshal.SizeOf( anytype );
if( rawsize > rawdatas.Length )
return null;
GCHandle handle = GCHandle.Alloc( rawdatas, GCHandleType.Pinned );
IntPtr buffer = handle.AddrOfPinnedObject();
object retobj = Marshal.PtrToStructure( buffer, anytype );
handle.Free();
return retobj;
}
public static byte[] RawSerializeEx( object anything )
{
int rawsize = Marshal.SizeOf( anything );
byte[] rawdatas = new byte[ rawsize ];
GCHandle handle = GCHandle.Alloc( rawdatas, GCHandleType.Pinned );
IntPtr buffer = handle.AddrOfPinnedObject();
Marshal.StructureToPtr( anything, buffer, false );
handle.Free();
return rawdatas;
}
// ===============================================================
--
NETMaster (Thomas Scheidegger)
http://www.cetus-links.org/oo_csharp.html
"William Stacey [MVP]" <sta...@mvps.org> wrote in message news:ekto0bqCCHA.2576@tkmsftngp04...
> This has been vexing me for a while. I want to take a struct (could be any
> struct with char[]'s etc, but use following as an easy example) and easily
> and quickly turn that into a byte array to pass to something like
> udpClient.Send. Obviously the fastest would be to not copy to a byte[] at
> all, but to somehow cast a byte[] onto the struct and just read from same
> place in memory without a copy - If this is possible, please let me know.
> The following is close, but does not work because the udpClient.Send does
> not take a pointer to a byte. I would rather not manually build the byte[]
> using some kind of stream because I think that would complicate the code and
> be slower (but if only way, that is ok too) This has to be a common task
> needed in almost any network program - is there a fast and standard way to
> do this? TIA
> struct Point1
> {
> public int x, y;
>
> public Point1(int p1, int p2)
> {
> x = p1;
> y = p2;
> }
> }
--
William Stacey, MCSE
Windows Server MVP
"Chris R" <sot...@bellsouth.net> wrote in message
news:lnDK8.1402$_p6....@e3500-atl1.usenetserver.com...
What is a formatted class? This is only referenced in the Marshal class
unless my search is not picking it up elsewhere.
--
William Stacey, MCSE
Windows Server MVP
"NETMaster" <spam.ne...@swissonline.ch> wrote in message
news:e10$YitCCHA.2296@tkmsftngp05...
I guess formatted = 'fixed layout' :
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcopyingpinning.asp
and I assume must have [StructLayout].
--
NETMaster (Thomas Scheidegger)
http://www.cetus-links.org/oo_csharp.html
"William Stacey [MVP]" <sta...@mvps.org> wrote in message news:eaRxCkwCCHA.2628@tkmsftngp05...
.. and here the real description:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpcondefaultmarshalingforvaluetypes.asp
"A formatted type is a complex type that contains information that explicitly
controls the layout of its members in memory.
The member layout information is provided using the StructLayoutAttribute attribute.
The layout can be one of the following LayoutKind enumeration values:"...
--
NETMaster (Thomas Scheidegger)
http://www.cetus-links.org/oo_csharp.html
"William Stacey [MVP]" <sta...@mvps.org> wrote in message news:eaRxCkwCCHA.2628@tkmsftngp05...
--
Visit the C# product team at http://www.gotdotnet.com/team/csharp
This posting is provided "AS IS" with no warranties, and confers no rights.
"William Stacey [MVP]" <sta...@mvps.org> wrote in message
news:ekto0bqCCHA.2576@tkmsftngp04...
...
Point1 myPoint = new Point1(1,2);
sendPoint( ref myPoint );
...
private unsafe void sendPoint( ref Point1 myPoint )
{
fixed( Point1* pPoint1 = &myPoint )
{
byte* pBuffer = (byte*)pPoint1;
udpClient.Send (pBuffer, buffer.Length); //Error - pBuffer not
byte[]
}
}
Chris R.
"Eric Gunnerson [MS]" <eri...@online.microsoft.com> wrote in message
news:O5$sF7xCCHA.1732@tkmsftngp02...
or something similar.
"Chris R" <sot...@bellsouth.net> wrote in message
news:ZvOK8.4038$EW5.4...@e3500-atl2.usenetserver.com...
private unsafe void sendPoint( ref Point1 myPoint )
{
int pointSize = sizeof(Point1);
byte [] arBuffer = new byte[ pointSize ];
fixed( Point1* pPoint1 = &myPoint )
{
fixed( byte* pBuf = arBuffer )
{
Point1* ps = pPoint1;
Point1* pd = (Point1*)pBuf;
*((Point1*)pd) = *((Point1*)ps);
udpClient.Send( arBuffer, pointSize );
}
}
}
"Chris R" <sot...@bellsouth.net> wrote in message
news:ZvOK8.4038$EW5.4...@e3500-atl2.usenetserver.com...
I went ahead and put the struct size in the method, but that's not
necessary, of course, but it saved me from having to write another method or
a property and preserves the data in Point1 as being simply the information
it needs.
*****************************************
struct Point1
{
...
public unsafe byte [] GetBytes( out int structSize )
{
structSize = sizeof( Point1 );
byte [] retArray = new byte[ structSize ];
fixed( Point1* pPoint1 = &this )
{
fixed( byte* pBuf = retArray )
{
Point1* ps = pPoint1;
Point1* pd = (Point1*)pBuf;
*((Point1*)pd) = *((Point1*)ps);
}
}
return retArray;
}
}
*****************************************
Point1 myPoint = new Point1(1,2);
int pointSize;
udpClient.Send( myPoint.GetBytes( out pointSize ), pointSize );
Chris R.
"Chris R" <sot...@bellsouth.net> wrote in message
news:lnDK8.1402$_p6....@e3500-atl1.usenetserver.com...
--
William Stacey, MCSE
Microsoft MVP Windows 2000/NT Server
"Chris R" <sot...@bellsouth.net> wrote in message
news:GDPK8.4383$EW5.4...@e3500-atl2.usenetserver.com...
*pd = *ps;
since pd and ps are already Point1*. Redundant casting. I'd originally
written them as byte*, but that would have needed a for loop, so I changed
it to Point1*, but forgot to reevaluate that line. :o)
Chris R.
"William Stacey [MVP]" <sta...@mvps.org> wrote in message
news:#9fYRyzCCHA.2440@tkmsftngp05...
--
William Stacey, MCSE
Microsoft MVP Windows 2000/NT Server
"Eric Gunnerson [MS]" <eri...@online.microsoft.com> wrote in message
news:O5$sF7xCCHA.1732@tkmsftngp02...
In SpaceWar, take a look at PtrHolder.cs for a lot of pointer
methodology. I'm going to check it out now and see what I can learn.
Chris R.
"William Stacey [MVP]" <sta...@mvps.org> wrote in message
news:OAC5iY0CCHA.1880@tkmsftngp04...