Created
February 11, 2023 14:23
-
-
Save chrdek/01af69ed9021f79def1b5feaa794a0e7 to your computer and use it in GitHub Desktop.
Retrieve unm. memory address from .NET heap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Util | |
| { | |
| public static Tuple<string?, string?> GetMemoryAddressOfClass<T1, T2>(T1 o1, T2 o2) | |
| where T1 : class | |
| where T2 : class | |
| { | |
| //using generics to block structs, that would be boxed | |
| //so we would get address of a boxed object, not struct | |
| //works only for objects that do not contain references | |
| // to other objects | |
| string? address1 = null; | |
| string? address2 = null; | |
| GCHandle? handleO1 = null; | |
| GCHandle? handleO2 = null; | |
| if (o1 != null) | |
| { | |
| handleO1 = GCHandle.Alloc(o1, GCHandleType.Pinned); | |
| } | |
| if (o2 != null) | |
| { | |
| handleO2 = GCHandle.Alloc(o2, GCHandleType.Pinned); | |
| } | |
| if (handleO1 != null) | |
| { | |
| IntPtr pointer1 = handleO1.Value.AddrOfPinnedObject(); | |
| address1 = "0x" + pointer1.ToString("X"); | |
| } | |
| if (handleO2 != null) | |
| { | |
| IntPtr pointer2 = handleO2.Value.AddrOfPinnedObject(); | |
| address2 = "0x" + pointer2.ToString("X"); | |
| } | |
| if (handleO1 != null) | |
| { | |
| handleO1.Value.Free(); | |
| } | |
| if (handleO2 != null) | |
| { | |
| handleO2.Value.Free(); | |
| } | |
| Tuple<string?, string?> result = | |
| new Tuple<string?, string?>(address1, address2); | |
| return result; | |
| } | |
| public static unsafe string? GetMemoryAddressOfStruct<T1>(ref T1 o1) | |
| where T1 : unmanaged | |
| { | |
| //In order to satisfy this constraint "unmanaged" a type must be a struct | |
| //and all the fields of the type must be unmanaged | |
| //using ref, so I would not get a value copy | |
| string? result = null; | |
| fixed (void* pointer1 = (&o1)) | |
| { | |
| result = $"0x{(long)pointer1:X}"; | |
| } | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment