Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Pointers,Classes,Objects... II

Pointers,Classes,Objects... II

Scheduled Pinned Locked Moved C#
questiondebugginghelptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tambi Ashmoz
    wrote on last edited by
    #1

    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

    W 1 Reply Last reply
    0
    • T Tambi Ashmoz

      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

      W Offline
      W Offline
      Werdna
      wrote on last edited by
      #2

      You can use BindingFlags to get non-public members from an object. Type.GetField(name, BindingFlags.GetField | BindingFlags.NonPublic)

      T 1 Reply Last reply
      0
      • W Werdna

        You can use BindingFlags to get non-public members from an object. Type.GetField(name, BindingFlags.GetField | BindingFlags.NonPublic)

        T Offline
        T Offline
        Tambi Ashmoz
        wrote on last edited by
        #3

        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.

        W 1 Reply Last reply
        0
        • T Tambi Ashmoz

          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.

          W Offline
          W Offline
          Werdna
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups