?
=?iso-8859-1?Q?J=F3n_Sveinsson?=
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.JONHS-LAP\My
Documents\Visual Studio
Projects\StructTest\Class1.cs(100): Cannot take the
address or size of
a variable of a managed type ('StructTest.test')
Here is my code, what is I doing wrong here.
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
namespace StructTest
{
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct test
{
[MarshalAs(UnmanagedType.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",FileMode.Open);
int tr = Marshal.SizeOf(h);
byte[] array = new byte[tr];
array = YourStructToBytes(h);
test g = new test();
g = BytesToYourStruct(array);
fs.Read(array,0,tr);
}
static unsafe byte[] YourStructToBytes( 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 BytesToYourStruct( byte[] arr )
{
if( arr.Length < sizeof(test) )
throw new ArgumentException();
test s;
fixed( byte* parr = arr )
{ s = *((test*)parr); }
return s;
}
}
}
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.JONHS-LAP\My
Documents\Visual Studio
Projects\StructTest\Class1.cs(100): Cannot take the
address or size of
a variable of a managed type ('StructTest.test')
Here is my code, what is I doing wrong here.
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
namespace StructTest
{
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct test
{
[MarshalAs(UnmanagedType.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",FileMode.Open);
int tr = Marshal.SizeOf(h);
byte[] array = new byte[tr];
array = YourStructToBytes(h);
test g = new test();
g = BytesToYourStruct(array);
fs.Read(array,0,tr);
}
static unsafe byte[] YourStructToBytes( 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 BytesToYourStruct( byte[] arr )
{
if( arr.Length < sizeof(test) )
throw new ArgumentException();
test s;
fixed( byte* parr = arr )
{ s = *((test*)parr); }
return s;
}
}
}