Cannot take the address or size of managed type

  • Thread starter Thread starter =?iso-8859-1?Q?J=F3n_Sveinsson?=
  • Start date Start date
?

=?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;
}


}
}
 
Hi,

1) If you want to do this with Interop & Marshal, you must create a
unmanaged buffer

[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct test
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=3)]
public int[] local0;
public int local1;
public int local2;
}

static byte[] YourStructToBytes( test s )
{
int size = Marshal.SizeOf( s );
byte[] retArr = new byte[ size ];
IntPtr buf = Marshal.AllocHGlobal( size ); // create unmanaged memory
Marshal.StructureToPtr ( s, buf, false ); // copy struct

for (int i=0; i<size; ++i)
{
retArr = Marshal.ReadByte(buf, i); // read unmanaged bytes
}
Marshal.FreeHGlobal( buf );
return retArr;
}

static test YourBytesToStruct( byte[] arr )
{
test retVal = new test();
int size = Marshal.SizeOf(retVal);
//if ( arr.Length < size ) // Throw
IntPtr buf = Marshal.AllocHGlobal( size );
for (int i=0; i<size; ++i)
{
Marshal.WriteByte(buf, i, arr);
}

retVal = (test) Marshal.PtrToStructure( buf, typeof(test) );
Marshal.FreeHGlobal( 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 = YourStructToBytes(a);
test b = YourBytesToStruct(bytes);
Console.WriteLine ("{0} {1}", b.local1, b.local2);
}

2) Use Serialization with Reflection and a MemoryStream
using System.Runtime.Serialization.Formatters.Binary;

[Serializable]
public struct test
{
public int[] local0;
public int local1;
public int local2;
}

private byte[] YourStructToBytes2(test s)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize (ms, s);
return ms.GetBuffer();
}

private test YourBytesToStruct2( 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 = YourStructToBytes2(a);
test b = YourBytesToStruct2( bytes2 );
Console.WriteLine ("{0} {1}", b.local1, b.local2);
}


HTH,
Greetings

Jón Sveinsson said:
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;
}


}
}
 
Back
Top