Direct Memory Access Help

O

O.B.

Given the two following structures:

[StructLayout(LayoutKind.Explicit, Size=16)]
public struct Articulation {
[FieldOffset(0)] public byte typeDesignator; // 1 byte
[FieldOffset(1)] public byte changeIndicator; // 1 byte
[FieldOffset(2)] public Unsigned16 attachmentID; // 2 bytes
[FieldOffset(4)] public Unsigned32 type; // 4 bytes
[FieldOffset(8)] public Bits64 value; // 8 bytes
}

[StructLayout(LayoutKind.Explicit, Size=1392)]
public struct EntityState {
public static byte TYPE_ENTITY_STATE = 1;
[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 Unsigned32 appearance; // 4 bytes
[FieldOffset(88)] public DeadReckoning deadReckoning; // 40 bytes
[FieldOffset(128)] public EntityMarking marking; // 12 bytes
[FieldOffset(140)] public Unsigned32 capabilities; // 4 bytes
// In C#, you cannot have "public fixed Articulation
// articulations[78]" because "fixed" does not support
// reference types. Use memory pointers?
[FieldOffset(144)] public Articulation articulations;
}

As you can see, I hit a small problem where C# does not support "fixed"
with a custom type. So, I'd like to add an operation to the EntityState
structure that allows a user to load an articulation given an index.

// A workaround for this.articulations[index]
public Articulation getArticulationAt(int index);

In C#, how does one go about directly accessing memory to perform this
behavior? Thanks in advance.
 
W

Willy Denoyette [MVP]

| Given the two following structures:
|
| [StructLayout(LayoutKind.Explicit, Size=16)]
| public struct Articulation {
| [FieldOffset(0)] public byte typeDesignator; // 1 byte
| [FieldOffset(1)] public byte changeIndicator; // 1 byte
| [FieldOffset(2)] public Unsigned16 attachmentID; // 2 bytes
| [FieldOffset(4)] public Unsigned32 type; // 4 bytes
| [FieldOffset(8)] public Bits64 value; // 8 bytes
| }
|
| [StructLayout(LayoutKind.Explicit, Size=1392)]
| public struct EntityState {
| public static byte TYPE_ENTITY_STATE = 1;
| [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 Unsigned32 appearance; // 4 bytes
| [FieldOffset(88)] public DeadReckoning deadReckoning; // 40 bytes
| [FieldOffset(128)] public EntityMarking marking; // 12 bytes
| [FieldOffset(140)] public Unsigned32 capabilities; // 4 bytes
| // In C#, you cannot have "public fixed Articulation
| // articulations[78]" because "fixed" does not support
| // reference types. Use memory pointers?
| [FieldOffset(144)] public Articulation articulations;
| }
|
| As you can see, I hit a small problem where C# does not support "fixed"
| with a custom type. So, I'd like to add an operation to the EntityState
| structure that allows a user to load an articulation given an index.
|
| // A workaround for this.articulations[index]
| public Articulation getArticulationAt(int index);
|
| In C#, how does one go about directly accessing memory to perform this
| behavior? Thanks in advance.

What you should do, is define the array of struct as a ByValArray of struct
Articulation with a size equal to the size of the array of structs, that is
the number of elements.


public struct EntityState {
..
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 78)]
public Articulation [] articulations;
}

Beware that the struct array must be aligned on machine word boundary!

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top