473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Cannot take the address or size of a variable of a managed type

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;
}
}
}
Nov 15 '05 #1
0 2072

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jack | last post by:
Hi, I got a simple form where one needs to input data. The data is being processed in an asp page. However, I cannot figure out why couple of values I am typing in the form is not being retrived...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
22
by: Smutny30 | last post by:
Hello, I am preparing a database that will store 10 n * GBs - 100 n * GBs of data. I calculated to have 1,2 GB of bufferpools. I run the DB2 v. 8.2.1 alone on 4 GB box. I obtain : ...
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
1
by: Jón Sveinsson | last post by:
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 ...
5
by: Dominiek | last post by:
Hi, I have a strange problem. during the developement of my program, while progressively writing more and more code, the callstack of my program gets deeper and deeper (7 levels). Then, all...
3
by: anuragpj | last post by:
i have designed a login page like this: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Electrical Engineering Dept...
0
by: jianxin9 | last post by:
Hi everyone, I don't have a lot of experience with ASP and I was hoping someone could help me. I want to use our ASP form along with some javascript code to create a form where our patrons can...
3
by: pinko1204 | last post by:
My Update function cannot successful update to sql table even don't have any error. Please help to check .....thx PHP1 <?php require_once 'header.php'; ?> <style type="text/css"> <!--
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
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,...

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.