Get the data point by the address of an IntPtr? How?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I had a scenario here.

[StructLayout(LayoutKind.Sequential)]
public struct StructA
{
public byte avalue;
public byte bvalue;
public IntPtr InData;

public StructA(byte a, byte b)
{
this.avalue = a;
this.bvalue = b;
this.InData = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(System.Byte)));
}
}

....

byte[] someByteArray = new byte[5] {0x00, 0x01, 0x02, 0x03, 0x04};
StructA myStructA = new StructA(0,0);

myStructA.avalue = 0x09;
myStructA.bvalue = 0xf0;

Marshal.Copy(someByteArray, 0, myStructA.InData,
Marshal.SizeOf(typeof(System.Byte)));

The question is i assume that myStructA, will store the address and where it
points the byte array. Okay, just assume

1)
i want to see what is myStructA.InData is pointing at with Visual Studio .NET

and

2)
how do i write c# codes to do the same thing?

Any help please? Thanks.
 
Hi,

Chua Wen Ching said:
I had a scenario here.

[StructLayout(LayoutKind.Sequential)]
public struct StructA
{
public byte avalue;
public byte bvalue;
public IntPtr InData;

public StructA(byte a, byte b)
{
this.avalue = a;
this.bvalue = b;
this.InData = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(System.Byte)));
}
}

...

byte[] someByteArray = new byte[5] {0x00, 0x01, 0x02, 0x03, 0x04};
StructA myStructA = new StructA(0,0);

myStructA.avalue = 0x09;
myStructA.bvalue = 0xf0;

Marshal.Copy(someByteArray, 0, myStructA.InData,
Marshal.SizeOf(typeof(System.Byte)));

The question is i assume that myStructA, will store the address and where it
points the byte array.

Not really. myStructA.InData will point to a copy of the first element in
the byte array. You allocated space for one byte and you copy one byte.

If you want to copy the bytes to unmanaged memory do:

byte[] someByteArray = new byte[5] {0x00, 0x01, 0x02, 0x03, 0x04};
myStructA.InData = MarshalAllocHGlobal(
Marshal.SizeOf(typeof(byte))*someByteArray.Length);
Marshal.Copy(someByteArray, 0, myStructA.InData, someByteArray.Length);
//not byte size! but number of elements!
// now InData points to the copied array elements
Marshal.FreeHGlobal( myStructA.InData ); // cleanup


Since a byte array is blittable, you can also get a direct pointer:

GCHandle gArr = GCHandle.Alloc( someByteArray, GCHandleType.Pinned );
myStructA.InData = gArr.AddrOfPinnedObject();
// now InData points to the pinned byte array
gArr.Free(); // cleanup


Okay, just assume

1)
i want to see what is myStructA.InData is pointing at with Visual Studio
..NET

You can only see the bytes (you should know the length) that InData is
pointing at, by setting a breakpoint after you get the intptr, then in the
local window read the value, open the memory window and type in the address.
and

2)
how do i write c# codes to do the same thing?

You should know what it points at, and with this info, you can use
Marshal.Copy,Marshal.PtrToStruct or Marshal.PtrToString* to get a managed
type from the pointer.

HTH,
greetings
 
Back
Top