title: ReadProcessMemory in C++ by a C# programmer, failing consistantly
body: <p>I have a vague knowledge of C++, but I have some extensive experience in C#, though it's not so useful in this instance. I have some code in C#, and it works just fine. I have what I presume is very similar code in C++, and I can't seem to get it working nor debug it. So, here is the C# code I've written and tested quite thoroughly:</p>
  
  <pre><code>    [DllImport("kernel32.dll")]
      public static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
          [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
  
      public byte[] ReadBytes(IntPtr Handle, Int64 Address, uint BytesToRead)
      {
          IntPtr ptrBytesRead;
          byte[] buffer = new byte[BytesToRead];
          ReadProcessMemory(Handle, new IntPtr(Address), buffer, BytesToRead, out ptrBytesRead);
          return buffer;
      }
  
      public int ReadInt32(long Address, uint length = 4, IntPtr? Handle = null)
      {
          return BitConverter.ToInt32(ReadBytes(getIntPtr(Handle), Address, length), 0);
      }
  </code></pre>
  
  <p>So, this function ReadInt32 takes an address, adds it to the base address I've stored upon initialising my Util class, and reads the memory up to 4 bytes, using a handle which once again is acquired upon initialisation. I know 100% that this works provided I set the values correctly.</p>
  
  <p>This code is a little long, excuse my ignorance but I don't want to leave any of this to imagination, I can't begin to suggest what may be incorrect, as I'm not too well trained in the world of C++.</p>
  
  <pre><code>DWORD address = 0x3C02A8;
  int value = 0;
  DWORD pid;
  HWND hwnd;
  DWORD baseAddress;
  DWORD toread;
  SIZE_T bytesRead;
  
  // Get a process handle
  hwnd = FindWindow(NULL,L"Tibia - Knightski"); //Finds the Window called "Minesweeper"
  if(!hwnd) //If none, display an error
  {
      cout &lt;&lt;"Window not found!\n";
      cin.get();
  }
  else
  {
      cout &lt;&lt; "Window found: " &lt;&lt; hwnd &lt;&lt; endl;
      cin.get();
  }
  
  // Get a base address
  GetWindowThreadProcessId(hwnd,&amp;pid); //Get the process id and place it in pid
  HANDLE phandle = OpenProcess(PROCESS_VM_READ,0,pid); //Get permission to read
  if(!phandle) //Once again, if it fails, tell us
  {
      cout &lt;&lt;"Could not get handle!\n";
      cin.get();
  }
  else
  {
      cout &lt;&lt; "Handle obtained: " &lt;&lt; phandle &lt;&lt; endl;
      cin.get();
      baseAddress = (DWORD)phandle;
      cout &lt;&lt; "Base Address obtained: " &lt;&lt; baseAddress &lt;&lt; endl;
      cin.get();
  }
  
  toread = baseAddress + address;
  
  // Read memory at base + address
  if (ReadProcessMemory(phandle, (void*)address, &amp;value, 4, &amp;bytesRead))
  {
      cout &lt;&lt; value;
      cin.get();
  }
  else
  {
      cout &lt;&lt; "Failed to read memory" &lt;&lt; GetLastError() &lt;&lt; endl;
      cout &lt;&lt; "Bytes read: " &lt;&lt; bytesRead &lt;&lt; endl;
      cin.get();
  }
  </code></pre>
  
  <p>The memory address I'm trying to read is 0x3BE1E0, but I've added the base address (0x20C8) to this to get 0x3C02A8. I will assume that the readers on this site know of the handle and what not...</p>
  
  <p>Thank you for your time. Note that I'm hoping for an explanation of what I'm doing wrong more so than an answer, so please bare that in mind if you've the time free. An answer will do, as I can most likely research the result anyway.</p>
  
  <p>The output is this:</p>
  
  <pre><code>Window found: 00D5014C
  
  Handle obtained: 00000044
  
  Base Address obtained: 68
  
  Failed to read memory299
  Bytes read: 0
  </code></pre>