473,480 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

marshal to new struct question.

The following code works, but I can't figure out why. I take a struct with
two members, a single byte and byte[]. I then marshal the whole struct to a
byte[]. I create a new struct (without init'ing any members) and marshal
the tmp array back to the new struct. The new struct shows the same data.
The single type is easy, that just gets copied. The byte[] ref type is the
interesting part. The address (i.e ref) of the original byte[] gets copied
to the new struct - unless marshal is changing this. But swba.ba[0] and
newswba.ba[0] point to different places. How is Marshal.PtrToStructure
picking up the byte[] values based on a pointer it gets from the byte[]?
public unsafe static void StructTest()
{
StructWithByteArray swba = new StructWithByteArray();
swba.b = 77; swba.ba = new byte[]{12, 24}; //Set some data.
byte[] tmpBA = new byte[Marshal.SizeOf(swba.GetType())];
fixed(byte * swbaPtr = &swba.ba[0]) //Get address to swba.ba[0];
{
fixed(byte * p = &tmpBA[0])
{
//Copy struct to tmpBA byte[].
Marshal.StructureToPtr(swba, (IntPtr)p, true);
//Print address swba.ba points to.
byte * tmpP = p;
tmpP++; //Move pointer one byte.
uint addOfArray = (uint)Marshal.ReadIntPtr((IntPtr)tmpP);
Console.WriteLine("Address that swba.ba points to:"+addOfArray);
Console.WriteLine("Address that swba.ba[0] points to:"+(uint)swbaPtr);
swba.b = 0; swba.ba = null; //empty to show we get new values from
byte[].

//Copy tmpBA byte[] to a new struct.
StructWithByteArray newswba; //declare a new struct.
newswba = (StructWithByteArray)Marshal.PtrToStructure((IntPt r)p,
swba.GetType());
//Print address (ptr) of first byte in new array.
fixed(byte * pp = &newswba.ba[0])
{
Console.WriteLine("Address that newswba.ba[0] points to:"+(uint)pp);
}
Console.WriteLine("newswba.b:"+newswba.b);
Console.WriteLine("newswba.ba[0] and
[1]:"+newswba.ba[0]+","+newswba.ba[1]);
}
}
}

[StructLayout(LayoutKind.Sequential, Pack=1)]
public unsafe struct StructWithByteArray
{
public byte b; // 1 byte.
public byte[] ba; // 4 bytes. Total 5.
}

Output:
StructToArray Size:5
Address that swba.ba points to:1710008
Address that swba.ba[0] points to:78244152
Address that newswba.ba[0] points to:78244884
newswba.b:77
newswba.ba[0] and [1]:12,24

--
William Stacey, MVP

Nov 15 '05 #1
0 1871

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

Similar topics

4
3797
by: William Stacey | last post by:
Using the following struct def, how can I tell (using reflection) if "ba" has the marshal attribute and get the "ByValArray" and maybe even the size? In the bigger picture, given a struct (or a...
6
10791
by: SB | last post by:
I feel dumb to ask because I bet this is a simple question... Looking at the code below, can someone please explain why I get two different values in my Marshal.SizeOf calls (see the commented...
1
4185
by: dhornyak | last post by:
I have been banging my head against the wall for a while now, and can't seem to id the problem. I've been through a ton of posts and the code doesn't seem any different. Can anybody see it? When...
1
4750
by: jurot | last post by:
Hi. I have struct in C++: struct MY_STRUCT { int x; int y; char** arrNames; //array of strings }
2
3166
by: RYoung | last post by:
Given this native struct: typedef struct vendor { char name; } VENDOR I want to make managed equivalent, so I did this: public value struct Vendor
1
1290
by: Goran | last post by:
Hi all! I need to pass managed String from to C-style APIs. I see I can use Marshal::StringToXXX functions. Is this the best we have? I understand this will allocate a new string and create copy...
4
4687
by: cleanrabbit | last post by:
Hello! I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this...
2
7176
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
2094
by: Charming12 | last post by:
Hi All, I have a strange problem and due to my inefficiency with IntPtr i am unable to figure it out. I have an structure something like: public struct Detail { public int age; public...
0
7046
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
7048
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,...
1
6741
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
6956
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
5342
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,...
0
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
183
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.