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. Reading the memory of a process

Reading the memory of a process

Scheduled Pinned Locked Moved C#
performancehelptutorialquestion
8 Posts 4 Posters 2 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.
  • W Offline
    W Offline
    Wjousts
    wrote on last edited by
    #1

    I am trying to read the memory being used by a process but I can't quite figure out how to do it (or if it's even possible). I can get a reference to the process using Process.GetProcessesByName and I can get the base address using Process.MainModule.BaseAddress (which returns a IntPtr). I thought that by using IntPtr.ToPointer() and casting to a char* I would be able to read the memory as a stream of chars but it doesn't work because it always throws a NullReferenceException when I try and dereference the pointer. Can anybody help me out here? Thanksclass Class1 { [STAThread] static unsafe void Main(string[] args) { Process[] p = Process.GetProcessesByName("notepad"); ProcessModule pm = p[0].MainModule; Console.WriteLine(pm.BaseAddress); char* ptr = (char*) pm.BaseAddress.ToPointer(); char c = *ptr; // Throws System.NullReferenceException Console.WriteLine(c); Console.ReadLine(); } }


    "Where do we go to get our good name back?...we go where we always go when a dramatic change is needed. We go to the ballot box" - Al Gore 5/26/04

    N R 2 Replies Last reply
    0
    • W Wjousts

      I am trying to read the memory being used by a process but I can't quite figure out how to do it (or if it's even possible). I can get a reference to the process using Process.GetProcessesByName and I can get the base address using Process.MainModule.BaseAddress (which returns a IntPtr). I thought that by using IntPtr.ToPointer() and casting to a char* I would be able to read the memory as a stream of chars but it doesn't work because it always throws a NullReferenceException when I try and dereference the pointer. Can anybody help me out here? Thanksclass Class1 { [STAThread] static unsafe void Main(string[] args) { Process[] p = Process.GetProcessesByName("notepad"); ProcessModule pm = p[0].MainModule; Console.WriteLine(pm.BaseAddress); char* ptr = (char*) pm.BaseAddress.ToPointer(); char c = *ptr; // Throws System.NullReferenceException Console.WriteLine(c); Console.ReadLine(); } }


      "Where do we go to get our good name back?...we go where we always go when a dramatic change is needed. We go to the ballot box" - Al Gore 5/26/04

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      What is the purpose of what you are trying to do? - Nick Parker
      My Blog | My Articles

      W 1 Reply Last reply
      0
      • N Nick Parker

        What is the purpose of what you are trying to do? - Nick Parker
        My Blog | My Articles

        W Offline
        W Offline
        Wjousts
        wrote on last edited by
        #3

        Writing a trainer


        "Where do we go to get our good name back?...we go where we always go when a dramatic change is needed. We go to the ballot box" - Al Gore 5/26/04

        N 1 Reply Last reply
        0
        • W Wjousts

          Writing a trainer


          "Where do we go to get our good name back?...we go where we always go when a dramatic change is needed. We go to the ballot box" - Al Gore 5/26/04

          N Offline
          N Offline
          Nick Parker
          wrote on last edited by
          #4

          That still doesn't explain what you are trying to do with your example above. - Nick Parker
          My Blog | My Articles

          L 1 Reply Last reply
          0
          • W Wjousts

            I am trying to read the memory being used by a process but I can't quite figure out how to do it (or if it's even possible). I can get a reference to the process using Process.GetProcessesByName and I can get the base address using Process.MainModule.BaseAddress (which returns a IntPtr). I thought that by using IntPtr.ToPointer() and casting to a char* I would be able to read the memory as a stream of chars but it doesn't work because it always throws a NullReferenceException when I try and dereference the pointer. Can anybody help me out here? Thanksclass Class1 { [STAThread] static unsafe void Main(string[] args) { Process[] p = Process.GetProcessesByName("notepad"); ProcessModule pm = p[0].MainModule; Console.WriteLine(pm.BaseAddress); char* ptr = (char*) pm.BaseAddress.ToPointer(); char c = *ptr; // Throws System.NullReferenceException Console.WriteLine(c); Console.ReadLine(); } }


            "Where do we go to get our good name back?...we go where we always go when a dramatic change is needed. We go to the ballot box" - Al Gore 5/26/04

            R Offline
            R Offline
            Roman Rodov
            wrote on last edited by
            #5

            There is an excellent article on this very website on doing what you trying to achieve. Minesweeper, Behind the Scenes[^]

            W 1 Reply Last reply
            0
            • N Nick Parker

              That still doesn't explain what you are trying to do with your example above. - Nick Parker
              My Blog | My Articles

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              I thinks he means a GAME trainer, to modify inprocess memory. top secret xacc-ide 0.0.1

              W 1 Reply Last reply
              0
              • L leppie

                I thinks he means a GAME trainer, to modify inprocess memory. top secret xacc-ide 0.0.1

                W Offline
                W Offline
                Wjousts
                wrote on last edited by
                #7

                Yes, thank you, that is what I meant. Sorry I should have been clearer.


                "Where do we go to get our good name back?...we go where we always go when a dramatic change is needed. We go to the ballot box" - Al Gore 5/26/04

                1 Reply Last reply
                0
                • R Roman Rodov

                  There is an excellent article on this very website on doing what you trying to achieve. Minesweeper, Behind the Scenes[^]

                  W Offline
                  W Offline
                  Wjousts
                  wrote on last edited by
                  #8

                  Thanks, that's a great article. I have got as far as using PInvoke with the OpenProcess and ReadProcessMemory functions, but the article doesn't use the WriteProcessMemory which I'm having trouble with. I got it working and got it to change the area of memory which I am sure is the right place, but it hung the game :( I'm not sure if I'm just messing with the wrong place or if I'm doing something wrong. Oh well. I was only doing it for a bit of fun.


                  "Where do we go to get our good name back?...we go where we always go when a dramatic change is needed. We go to the ballot box" - Al Gore 5/26/04

                  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