title: ReadProcessMemory() returns 0 for static addresses
body: <p>What I try to do is to read a static address that's pointing to a dynamic adress that holds some value. But if I try to read the static address it always returns 0. The only way for it to be read is if I attach a debugger to the dynamic address in cheat engine. However I have no problem reading it with only reading from the dynamic address.</p>
  
  <pre><code>DWORD address = 0x74EA46D8;
  int value = 0;
  int new_address = 0;
  DWORD pid;
  HWND hwnd;
  hwnd = FindWindow(NULL,L"HackMe.exe");
  if(!hwnd) {
      cout &lt;&lt;"Window not found!\n";
      cin.get();
  } else {
      GetWindowThreadProcessId(hwnd,&amp;pid);
  
      HANDLE phandle = OpenProcess(PROCESS_VM_READ,0,pid);
      if(!phandle) {
          cout &lt;&lt;"Could not get handle!\n";
          cin.get();
      } else {
          while(1) {
              ReadProcessMemory(phandle,(void*)address,&amp;new_address,sizeof(new_address),0);
              cout &lt;&lt; new_address &lt;&lt; "\n"; //Always print 0
              //int new_address = 0x2ECA40B0; //This works if I uncomment this
              ReadProcessMemory(phandle,(void*)new_address,&amp;value,sizeof(value),0);
  </code></pre>
  
  <p>I even tried getting debug privelege, but that didn't do anything. I have no clue on what the problem is since I'm very new to C++. Any help is appreciated.</p>
  
  <p>Thank you.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>GetLastError() returns 0 at first, then it returns 299</p>
  
  <p><strong>Edit 2</strong></p>
  
  <pre><code>BOOL x = ReadProcessMemory(phandle,(void*)address,&amp;new_address,sizeof(new_address),0);
  cout &lt;&lt; x &lt;&lt; " " &lt;&lt; GetLastError() &lt;&lt; "\n";
  </code></pre>
  
  <p>returns</p>
  
  <pre><code>1 0
  1 299
  1 299
  1 299
  </code></pre>
  
  <p>and so on</p>
  
  <p><strong>Edit 3</strong>
  Bytes read is 4.</p>
  
  <p><strong>Edit 4</strong></p>
  
  <p>Just to clarify.</p>
  
  <p>Reading directly from 0x74EA46D8 with ReadProcessMemory() returns 0.</p>
  
  <p>If I open up cheat engine and add the address 0x74EA46D8 points to to the address list. Then right click on it and press "Find out what access this address" it can be read all of a sudden. Enabling SeDebugPrivelege does nothing. </p>
  
  <p>The dynamic address can be read as normal, without having debug privelege(as long as I manually type the address for it or cheat engine debugs the address so the static address can be read)</p>
  
  <p>It's structured in this way:</p>
  
  <p>static address pointing to the address I try to read, this return 0 as the "new address" unless see above.</p>
  
  <p>dynamic address, containing the value I'm trying to read. This reads just fine if I define the dynamic address manually. But if I don't it fails since new_address is 0, unless see above.</p>
  
  <p><strong>Edit 5</strong></p>
  
  <p>Finally I found out the problem, the previous address was wrong. That address was part of cheat engine and the real address was 0x013CD878 with an offset of 0x4B0. That was the reason why it didn't work unless I debugged it.</p>
  
  <p>But I hope others will learn from my mistake :P</p>