Find address of array

  • Thread starter Thread starter SB
  • Start date Start date
S

SB

I have a struct with a fixed array of items (see below). How can I obtain
the physical address of the array? I don't need to do anything with it
other than display it to the end user (this is a small debugger-type of app
for a dll). Note that I do realize that the object may be moved later by
the GC...so I plan to pin it as needed.

public struct _Info
{
public int ID;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst = 256)]
public _Item[] items; // I need to locate the address of this array and
display it to the end user...ie $002E9A21
// ...snipped
}

TIA!
sb
 
SB said:
I have a struct with a fixed array of items (see below). How can I obtain
the physical address of the array? I don't need to do anything with it
other than display it to the end user (this is a small debugger-type of app
for a dll). Note that I do realize that the object may be moved later by
the GC...so I plan to pin it as needed.

public struct _Info
{
public int ID;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst = 256)]
public _Item[] items; // I need to locate the address of this array and
display it to the end user...ie $002E9A21
// ...snipped
}

TIA!
sb

Some unsafe code may do what you want.

_Info info = new _Info();
info.items = new _Item[256];
....
fixed(_Item* cptr = &info.items[0])
{
Console.WriteLine("${0:X8}", new IntPtr(cptr).ToInt32());
}

Willy.
 
Back
Top