title: Calling a function from another process
body: <p>I apologize in advance for my English :D</p>
  
  <p>How can I call a function from a process, knowing the pointer to it?</p>
  
  <p>I tried this:</p>
  
  <p>Process.cpp:</p>
  
  <pre><code>DWORD pid;
  HWND hwnd;
  HANDLE phandle;
  
  void Attach() {
      hwnd = FindWindow(NULL, L"GTA:SA:MP");
      if (!hwnd) {
          cout &lt;&lt; "Process is not found" &lt;&lt; endl;
          system("pause");
      }
      else if (hwnd) {
          cout &lt;&lt; "Process was successfully loaded" &lt;&lt; endl;
          GetWindowThreadProcessId(hwnd, &amp;pid);
          phandle = OpenProcess(PROCESS_VM_READ, 0, pid);
      }
      else
      {
          cout &lt;&lt; "Error 0x01" &lt;&lt; endl;
          system("pause");
      }
  }
  
  void GameText(const char* szText, int iTime, signed int iStyle)
  {
      typedef void(__stdcall* GameText_t)(const char*, int, signed int);
      GameText_t pGameText = (GameText_t)((char*)phandle + 0x69F2B0);
      return pGameText(szText, iTime, iStyle);
  }
  </code></pre>
  
  <p>main.cpp:</p>
  
  <pre><code>int main()
  {
  
      std::cout &lt;&lt; "Hello World!\n"; 
  
      Attach();
  
      GameText("~r~Test!", 1000, 5);
  
  }
  </code></pre>
  
  <p>And I get the following exception:</p>
  
  <blockquote>
    <p>An exception was thrown at the address 0x006EF7B6 in wh_mta.exe:
    0xC0000005: access violation during execution at 0x006EF7B6.</p>
  </blockquote>
  
  <p>Why is this happening? How can I call a function by its pointer through HANDLE?</p>
  
  <p>P.S </p>
  
  <p>Reading an integer through HANDLE works great.</p>
  
  <pre><code>int Read_Int(int address) {
      int value;
  
      ReadProcessMemory(phandle, (void*)address, &amp;value, sizeof(value), 0);
  
      return value;
  }
  </code></pre>