title: Memory Hacking/Modifying in C++
body: <p>So, I'm trying to write a program in C++, to modify a value in another program. In my case, the Windows' Calculator. Here's the code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;windows.h&gt;
  
  using namespace std;
  
  int main(void) {
  
      int nVal = 2000;
  
      HWND hWnd = FindWindowA(0, "Calculator");
      if(hWnd == 0){
          cerr &lt;&lt; "Could not find window." &lt;&lt; endl;
      } else {
          DWORD PID;
          GetWindowThreadProcessId(hWnd, &amp;PID);
          HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, false, PID);
  
          if(!hProc) {
              cerr &lt;&lt; "Cannot open process." &lt;&lt; endl;
          } else {
              int stat = WriteProcessMemory(hProc, (LPVOID)0xC6A0EB922C, &amp;nVal, (DWORD)sizeof(nVal), NULL);
  
              if(stat &gt; 0){
                  clog &lt;&lt; "Memory written to process." &lt;&lt; endl;
              } else {
                  cerr &lt;&lt; "Memory couldn't be written to process." &lt;&lt; endl;
              }
  
              CloseHandle(hProc);
  
              cin.get();
  
          }
  
      }
  
      return 0;
  }
  </code></pre>
  
  <p>The program tries to overwrite one value, stored with the 'MS' button in the Calculator. The problem is that the program can't do this. I tried to run the executable as administrator, but nothing changes. I found this code on a YouTube video, the guy was using XP, I am using Windows 8.</p>
  
  <p>I found the <em>0xC6A0EB922C</em> address using Cheat Engine, and also tried to modify the value inside and it worked perfectly!</p>
  
  <p>If anyone can help me out, please do. Thanks!</p>