Oject memory adress.
-
Hello, I'm looking for a way to get the memory adress of a C# object. Any idea is welcome :). Thanks, R. LOPES Just programmer.
This is specifically hidden to the user as object address change continuously due to the GC. If you debug a program with CorDbg, these addresses will be shown, but like said, unlike C/C++ they are likely to move during each garbage collection. Cheers :)
-
This is specifically hidden to the user as object address change continuously due to the GC. If you debug a program with CorDbg, these addresses will be shown, but like said, unlike C/C++ they are likely to move during each garbage collection. Cheers :)
-
Hi Leppie, I was expecting something like that. It makes totally sense. I'm now sure. Thanks, R. LOPES Just programmer.
You wont believe it, but I'm stuck with this issue as well (building a VM emulating the CLR VM on top of .NET). If you are intereseted look at the ld.XXXa IL instructions. There seems to be no highlevel functions for this. Maybe something like this would work (no it wont dammit). Maybe... load an address on stack and then return that as IntPtr, but you have to carefull... As you can see I'm bit lost too! What I do know (think rather) is that be default GetHashCode() returns the signed addr of a non Value /String object. I could be wrong.
-
You wont believe it, but I'm stuck with this issue as well (building a VM emulating the CLR VM on top of .NET). If you are intereseted look at the ld.XXXa IL instructions. There seems to be no highlevel functions for this. Maybe something like this would work (no it wont dammit). Maybe... load an address on stack and then return that as IntPtr, but you have to carefull... As you can see I'm bit lost too! What I do know (think rather) is that be default GetHashCode() returns the signed addr of a non Value /String object. I could be wrong.
Hi Leppie, What is this crazy idea of yours to build a VM on top of the VM of .NET :) ? Nevertheless, thank you for sharing with me your researches. By the way, for your nBass project, the BASS library has just been updated to 1.8a. Cheers, R. LOPES Just programmer.
-
Hi Leppie, What is this crazy idea of yours to build a VM on top of the VM of .NET :) ? Nevertheless, thank you for sharing with me your researches. By the way, for your nBass project, the BASS library has just been updated to 1.8a. Cheers, R. LOPES Just programmer.
This does the job perhaps:
.method private hidebysig static void*
GetAddr(object& o) cil managed
{
.maxstack 1
ldarg.0
ret
}You need to pass a ref object, so cast the 'o' to object. GriffonRL wrote: What is this crazy idea of yours to build a VM on top of the VM of .NET ? Just what you say: Crazy! But the ideas change all the time! The new idea is to have a Side by Side VM, with a different IL language (read more simplified). GriffonRL wrote: By the way, for your nBass project, the BASS library has just been updated to 1.8a. Thnx, I havent worked with it for ages... maybe when I'm bored... X|
-
Hi Leppie, What is this crazy idea of yours to build a VM on top of the VM of .NET :) ? Nevertheless, thank you for sharing with me your researches. By the way, for your nBass project, the BASS library has just been updated to 1.8a. Cheers, R. LOPES Just programmer.
Actually from that previous IL method it appears that when passing any object by ref, the CLR creates a pointer that holds the object, still not what you are looking for though.
-
Hi Leppie, I was expecting something like that. It makes totally sense. I'm now sure. Thanks, R. LOPES Just programmer.
OK here is what works:
.method private hidebysig static void*
GetAddr(object& o) cil managed
{
.maxstack 1
ldarg.0 //loads address
conv.u //make sure its a M*
ret //return the M*
}Output from Cordbg (after calling meth, but passing only c1, NOT ref c1): object c1 =(0x00c41910) void* h = 0x00C41910 I cant see a way to do this via C# though.
-
Hello, I'm looking for a way to get the memory adress of a C# object. Any idea is welcome :). Thanks, R. LOPES Just programmer.
Here is a full sample, see the comments:
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
.ver 1:0:3300:0
}
.assembly Testing
{
.hash algorithm 0x00008004
.ver 1:0:1272:38245
}
.module Testing.exe
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x00400000
.subsystem 0x00000003
.file alignment 512
.corflags 0x00000001.namespace Testing
{
.class private auto ansi beforefieldinit Class1
extends [mscorlib]System.Object
{
.method private hidebysig static void
Main(string[] args) cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
.maxstack 1
.locals ([0] object c1,
[1] void* h,
[2] int32 i)
newobj instance void Testing.Class1::.ctor()
stloc.0
ldloc.s c1 //ldloca will box ref type
call void* Testing.Class1::GetAddr(object&)
stloc.1
ldc.i4.5
stloc.2
ldloca 2 //note ldloca for value types
call void* Testing.Class1::GetAddr(object&)
stloc.1
ret
}.method private hidebysig static void\* GetAddr(object& o) cil managed { .maxstack 1 ldarg.0 conv.u ret } .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { .maxstack 1 ldarg.0 call instance void \[mscorlib\]System.Object::.ctor() ret }
}
}I dont know how one would call a ref object without adding the ref keyword in C#??? leppie::AllocCPArticle(Generic DFA State Machine for .NET);
-
Here is a full sample, see the comments:
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
.ver 1:0:3300:0
}
.assembly Testing
{
.hash algorithm 0x00008004
.ver 1:0:1272:38245
}
.module Testing.exe
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x00400000
.subsystem 0x00000003
.file alignment 512
.corflags 0x00000001.namespace Testing
{
.class private auto ansi beforefieldinit Class1
extends [mscorlib]System.Object
{
.method private hidebysig static void
Main(string[] args) cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
.maxstack 1
.locals ([0] object c1,
[1] void* h,
[2] int32 i)
newobj instance void Testing.Class1::.ctor()
stloc.0
ldloc.s c1 //ldloca will box ref type
call void* Testing.Class1::GetAddr(object&)
stloc.1
ldc.i4.5
stloc.2
ldloca 2 //note ldloca for value types
call void* Testing.Class1::GetAddr(object&)
stloc.1
ret
}.method private hidebysig static void\* GetAddr(object& o) cil managed { .maxstack 1 ldarg.0 conv.u ret } .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { .maxstack 1 ldarg.0 call instance void \[mscorlib\]System.Object::.ctor() ret }
}
}I dont know how one would call a ref object without adding the ref keyword in C#??? leppie::AllocCPArticle(Generic DFA State Machine for .NET);