472,371 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Marshal Not Copying all Data to Fixed Byte Array

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
Marshal.PtrToStructure to copy the all the data into the "other"
array?
[StructLayout(LayoutKind.Explicit, Size = 40)]
unsafe public struct DeadReckoning
{
[FieldOffset(0)]
public byte algorithm; // 1 byte

[FieldOffset(1)]
public fixed byte other[15]; // 15 bytes

[FieldOffset(16)]
public Vector linearAcceleration; // 12 bytes

[FieldOffset(28)]
public Vector angularVelocity; // 12 bytes
}
}

// Given rawData as a byte array
DeadReckoning dr = new DeadReckoning();
IntPtr pData = Marshal.AllocHGlobal(40);
Marshal.Copy(rawData, 28, pData, 40);
Marshal.PtrToStructure(pData, dr);
Marshal.FreeHGlobal(pData);

At this point, only dr.other[0] has the correct value; the values at
1-14 indexes of dr.other are not set.

Jul 11 '08 #1
2 6973
O.B. wrote:
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
Marshal.PtrToStructure to copy the all the data into the "other"
array?
[StructLayout(LayoutKind.Explicit, Size = 40)]
unsafe public struct DeadReckoning
{
[FieldOffset(0)]
public byte algorithm; // 1 byte

[FieldOffset(1)]
public fixed byte other[15]; // 15 bytes

[FieldOffset(16)]
public Vector linearAcceleration; // 12 bytes

[FieldOffset(28)]
public Vector angularVelocity; // 12 bytes
}
}
What's "Vector"? A structure of 12 bytes?
// Given rawData as a byte array
DeadReckoning dr = new DeadReckoning();
IntPtr pData = Marshal.AllocHGlobal(40);
Marshal.Copy(rawData, 28, pData, 40);
Because you're using an unsafe struct with explicit layout, there's actually
no need to use copying and marshalling at all. You can just write

DeadReckoning dr;
unsafe {
fixed (byte* pData = &rawData[28]) {
dr = *((DeadReckoning*) pData);
}
}

With this approach, however, you have to verify that "rawData" holds enough
bytes to populate the structure, or it'll copy garbage from the stack. If
you're lucky.
Marshal.PtrToStructure(pData, dr);
This shouldn't work, or at least, it doesn't for me. It throws
ArgumentException because "dr" is a structure. This overload is for classes;
for structures, the right syntax would be

DeadReckoning dr = (DeadReckoning) Marshal.PtrToStructure(pData,
typeof(DeadReckoning));

--
J.
Jul 11 '08 #2
On Jul 11, 3:40 pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
O.B. wrote:
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
Marshal.PtrToStructure to copy the all the data into the "other"
array?
[StructLayout(LayoutKind.Explicit, Size = 40)]
unsafe public struct DeadReckoning
{
[FieldOffset(0)]
public byte algorithm; // 1 byte
[FieldOffset(1)]
public fixed byte other[15]; // 15 bytes
[FieldOffset(16)]
public Vector linearAcceleration; // 12 bytes
[FieldOffset(28)]
public Vector angularVelocity; // 12 bytes
}
}

What's "Vector"? A structure of 12 bytes?
// Given rawData as a byte array
DeadReckoning dr = new DeadReckoning();
IntPtr pData = Marshal.AllocHGlobal(40);
Marshal.Copy(rawData, 28, pData, 40);

Because you're using an unsafe struct with explicit layout, there's actually
no need to use copying and marshalling at all. You can just write

DeadReckoning dr;
unsafe {
fixed (byte* pData = &rawData[28]) {
dr = *((DeadReckoning*) pData);
}
}

With this approach, however, you have to verify that "rawData" holds enough
bytes to populate the structure, or it'll copy garbage from the stack. If
you're lucky.
Marshal.PtrToStructure(pData, dr);

This shouldn't work, or at least, it doesn't for me. It throws
ArgumentException because "dr" is a structure. This overload is for classes;
for structures, the right syntax would be

DeadReckoning dr = (DeadReckoning) Marshal.PtrToStructure(pData,
typeof(DeadReckoning));

--
J.
My bad. I was trying to trim down the post. The DeadReckoning
structure is declared within a ExplicitLayout class (see below). The
explicit constructor outlines the call to marshalling data from an
array to the class structure.

namespace DIS
{
[StructLayout(LayoutKind.Explicit)]
public class EntityState
{
[FieldOffset(0)]
public Header header; // 12 bytes

[FieldOffset(12)]
public EntityIdentifier entityID; // 6 bytes

[FieldOffset(18)]
public ForceIdentifier forceID; // 1 byte

[FieldOffset(19)]
public byte numArticulations; // 1 byte

[FieldOffset(20)]
public EntityType type; // 8 bytes

[FieldOffset(28)]
public EntityType altType; // 8 bytes

[FieldOffset(36)]
public Vector linearVelocity; // 12 bytes

[FieldOffset(48)]
public WorldCoordinate location; // 24 bytes

[FieldOffset(72)]
public EulerAngles orientation; // 12 bytes

[FieldOffset(84)]
public Appearance appearance; // 4 bytes

[FieldOffset(88)]
public DeadReckoning deadReckoning = new DeadReckoning(); //
40 bytes

[FieldOffset(128)]
public EntityMarking marking; // 12 bytes

[FieldOffset(140)]
public Unsigned32 capabilities; // 4 bytes

[MarshalAs(UnmanagedType.ByValArray, ArraySubType =
UnmanagedType.Struct)]
[FieldOffset(144)]
public Articulation[] articulations; // 16 bytes each

public EntityState() { }

public EntityState(byte[] rawData)
{
// Copy the first 144 bytes to determine if there are
articulations.
unsafe
{
if (rawData.Length <= 144)
{
fixed (byte* pData = rawData)
{
Marshal.PtrToStructure((IntPtr) pData, this);
}
}
else
{
int dataSize = rawData.Length 144 ? 144 :
rawData.Length;
IntPtr pData = Marshal.AllocHGlobal(dataSize);
Marshal.Copy(rawData, 0, pData, dataSize);
Marshal.PtrToStructure(pData, this); // Throws
exception
Marshal.FreeHGlobal(pData);
}
}

// If there are articulations, allocate more memory for
this class
// and copy remaining data into articulations array.
if (rawData.Length 144 && numArticulations 0)
{
// Allocate memory for articulations
articulations = new Articulation[numArticulations];

// Copy values for each articulation
byte[] tempArray = new byte[16];
for (int i = 0; i < numArticulations; i++)
{
Array.Copy(rawData, 144 + i * 16, tempArray, 0,
16);
unsafe
{
fixed (byte* pData = tempArray)
{
articulations[i] =
(Articulation)Marshal.PtrToStructure((IntPtr)pData ,
typeof(Articulation));
}
}
}
}

header.length = (ushort)rawData.Length;
}

public byte[] ToRaw()
{
int pduSize = (int) header.length;
byte[] byteArray = new byte[pduSize];
IntPtr pointer = Marshal.AllocHGlobal(pduSize);
Marshal.StructureToPtr(this, pointer, false);
Marshal.Copy(pointer, byteArray, 0, pduSize);
Marshal.FreeHGlobal(pointer);
return byteArray;
}
}
}

Jul 11 '08 #3

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

Similar topics

0
by: William Stacey | last post by:
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...
9
by: Angel | last post by:
Hi again, I'm trying to call functions from a proprietary DLL but it's turned out to be more difficult than I thought. I have this W32.DLL which was written in C by USPS. They don't provide the...
1
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...
2
by: twawsico | last post by:
I have a piece of code that needs to read the contents of a binary file (that I've created with another app) into an array of structures. The binary data in the file represents just a series of...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
4
by: Rainer Queck | last post by:
Hi NG, is there a way to copy a buffer pointed to by a IntPtr directly into a two dimensional byte-array? I tried this, what obviously doesn't work: byte image = new byte; IntPtr p =...
2
by: O.B. | last post by:
I have a structure named EntityState with an explicit layout. The following two operations exist within the class to return a byte array representing the current object. Upon executing them each a...
1
by: nicewenyan | last post by:
I want to pass a managed c# byte (8 bit) array into a unmanaged c++ function: extern "C" void AddData(unsigned int* data); I use P/Invoke on managed side to do the marshaling as following: ...
2
by: O.B. | last post by:
I have operation within a class that marshals the data into a byte array. Below are three different ways that work. Are there any downsides to using one over the the other? public virtual byte...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.