Hi,
1) If you want to do this with Interop & Marshal, you must create a
unmanaged buffer
[StructLayout(La youtKind.Sequen tial, Pack=1)]
public struct test
{
[MarshalAs(Unman agedType.ByValA rray, SizeConst=3)]
public int[] local0;
public int local1;
public int local2;
}
static byte[] YourStructToByt es( test s )
{
int size = Marshal.SizeOf( s );
byte[] retArr = new byte[ size ];
IntPtr buf = Marshal.AllocHG lobal( size ); // create unmanaged memory
Marshal.Structu reToPtr ( s, buf, false ); // copy struct
for (int i=0; i<size; ++i)
{
retArr[i] = Marshal.ReadByt e(buf, i); // read unmanaged bytes
}
Marshal.FreeHGl obal( buf );
return retArr;
}
static test YourBytesToStru ct( byte[] arr )
{
test retVal = new test();
int size = Marshal.SizeOf( retVal);
//if ( arr.Length < size ) // Throw
IntPtr buf = Marshal.AllocHG lobal( size );
for (int i=0; i<size; ++i)
{
Marshal.WriteBy te(buf, i, arr[i]);
}
retVal = (test) Marshal.PtrToSt ructure( buf, typeof(test) );
Marshal.FreeHGl obal( buf );
return retVal;
}
public void Test()
{
test a;
a.local0 = new int[] { 10,10,10 };
a.local1 = 10;
a.local2 = 20;
// copy "a" to "b"
byte[] bytes = YourStructToByt es(a);
test b = YourBytesToStru ct(bytes);
Console.WriteLi ne ("{0} {1}", b.local1, b.local2);
}
2) Use Serialization with Reflection and a MemoryStream
using System.Runtime. Serialization.F ormatters.Binar y;
[Serializable]
public struct test
{
public int[] local0;
public int local1;
public int local2;
}
private byte[] YourStructToByt es2(test s)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter ();
bf.Serialize (ms, s);
return ms.GetBuffer();
}
private test YourBytesToStru ct2( byte[] b)
{
MemoryStream ms = new MemoryStream( b );
BinaryFormatter bf = new BinaryFormatter ();
return (test) bf.Deserialize (ms);
}
public void Test2()
{
// create "a"
test a;
a.local0 = new int[] { 10,10,10 };
a.local1 = 10;
a.local2 = 20;
// copy "a" to "b"
byte[] bytes = YourStructToByt es2(a);
test b = YourBytesToStru ct2( bytes2 );
Console.WriteLi ne ("{0} {1}", b.local1, b.local2);
}
HTH,
Greetings
"Jón Sveinsson" <ru******@hotma il.com> wrote in message
news:9b******** *************** *****@phx.gbl.. .
Hello everyone
I have been trying to read and write struct to binary
files, I'm using
to functions to convert the struct to bytes and bytes to
struct, I
always receive the following error
C:\Documents and Settings\jon.JO NHS-LAP\My
Documents\Visua l Studio
Projects\Struct Test\Class1.cs( 100): Cannot take the
address or size of
a variable of a managed type ('StructTest.te st')
Here is my code, what is I doing wrong here.
using System;
using System.IO;
using System.Runtime. Serialization.F ormatters.Binar y;
using System.Runtime. InteropServices ;
namespace StructTest
{
[StructLayout(La youtKind.Sequen tial, Pack=1)]
public struct test
{
[MarshalAs(Unman agedType.I4, SizeConst=3)]
public int[] local;
public int local1;
public int local2;
}
class Class1
{
[STAThread]
static unsafe void Main(string[] args)
{
int[] y = new int[3];
y.SetValue(12,0 );
y.SetValue(12,1 );
y.SetValue(12,2 );
test h = new test();
h.local = y;
h.local1 = 20;
h.local2 = 30;
FileStream fs = new FileStream
("Test5.txt",Fi leMode.Open);
int tr = Marshal.SizeOf( h);
byte[] array = new byte[tr];
array = YourStructToByt es(h);
test g = new test();
g = BytesToYourStru ct(array);
fs.Read(array,0 ,tr);
}
static unsafe byte[] YourStructToByt es( test s )
{
byte[] arr = new byte[ Marshal.SizeOf( s) ];
fixed( byte* parr = arr )
{
*((test*)parr) = s;//Cannot take the address
or size of a
variable of a managed type
}
return arr;
}
static unsafe test BytesToYourStru ct( byte[] arr )
{
if( arr.Length < sizeof(test) )
throw new ArgumentExcepti on();
test s;
fixed( byte* parr = arr )
{ s = *((test*)parr); }
return s;
}
}
}