Pointers,Classes,Objects... II
-
How can I get a variable pointer (as IntPtr) ? I am trying to compare between two objects, the objects are not the same but they could be from some place ... Example - let say we have two classes one derived from another ... public class A { protected h=0; public virtual func() { h=0; } } public class B:A { public override func() { h=1; } } Now ... I'am creating the objects ... obj1=Activator.CreateInstance(t1); //Type t1; class A c.Invoke(obj1,null); //ConstructorInfo c; getting constructor from class A the same with the other class... obj1=Activator.CreateInstance(t2); //Type t2; class B c.Invoke(obj2,null); //ConstructorInfo c; getting constructor from class B From this point you can see in the debugger obj1,obj2 (with the variable h=0 in both) Now I'am invoking my function... //MethodInfo m1,m2; getting my virtual AND override functions each from it's class ... and after that running functions , with null as parametter... object []parametersForFunction=new object[m1.GetParameters().Length]; //m1 or m2 doesn't matter they have the same parameters... virtual,override m1.Invoke(obj1,parametersForFunction); m2.Invoke(obj2,parametersForFunction); I can't check "h" with the GetFields() couse its not public().. My problem is here... Now if wee go back to the debugger we can see that in obj1.h=0 and obj2.h=1 Somthing like this obj1.A.h=0 obj2.B.A.h=1 My idea was to try the Marshal.ReadIntPtr(IntPtr,offset) so I can read obj2 after some offset, So I needed my variable Address. (somehow even in unsafe we cant do object *p=obj1; on objetcs...) I tried Marshel.ReadByte and it's not working cause my object type could be anything... Anyone have some idea ??? Thanks
-
How can I get a variable pointer (as IntPtr) ? I am trying to compare between two objects, the objects are not the same but they could be from some place ... Example - let say we have two classes one derived from another ... public class A { protected h=0; public virtual func() { h=0; } } public class B:A { public override func() { h=1; } } Now ... I'am creating the objects ... obj1=Activator.CreateInstance(t1); //Type t1; class A c.Invoke(obj1,null); //ConstructorInfo c; getting constructor from class A the same with the other class... obj1=Activator.CreateInstance(t2); //Type t2; class B c.Invoke(obj2,null); //ConstructorInfo c; getting constructor from class B From this point you can see in the debugger obj1,obj2 (with the variable h=0 in both) Now I'am invoking my function... //MethodInfo m1,m2; getting my virtual AND override functions each from it's class ... and after that running functions , with null as parametter... object []parametersForFunction=new object[m1.GetParameters().Length]; //m1 or m2 doesn't matter they have the same parameters... virtual,override m1.Invoke(obj1,parametersForFunction); m2.Invoke(obj2,parametersForFunction); I can't check "h" with the GetFields() couse its not public().. My problem is here... Now if wee go back to the debugger we can see that in obj1.h=0 and obj2.h=1 Somthing like this obj1.A.h=0 obj2.B.A.h=1 My idea was to try the Marshal.ReadIntPtr(IntPtr,offset) so I can read obj2 after some offset, So I needed my variable Address. (somehow even in unsafe we cant do object *p=obj1; on objetcs...) I tried Marshel.ReadByte and it's not working cause my object type could be anything... Anyone have some idea ??? Thanks
-
You can use BindingFlags to get non-public members from an object. Type.GetField(name, BindingFlags.GetField | BindingFlags.NonPublic)
Yes, but I still wanted to do this by comparing objects. I want to know how to get a variable pointer as IntPtr and how to read from unknown type object from a specific place.
-
Yes, but I still wanted to do this by comparing objects. I want to know how to get a variable pointer as IntPtr and how to read from unknown type object from a specific place.
I don't think the framework would allow you to do that. You could mess up all the class info stuff including vtable. Plus you would have to know internal layout of the class data structure, and that might be system dependend (might work differently on mono or .netgnu) You can download rotor and see how they lay it out. If you try to get address of any managed type, you'll get error: Cannot take the address or size of a variable of a managed type.