title: C++ Get Module Base Address for 64bit Application
body: <p>I have been playing around with memory reading/editing recently and have run into a problem which I think is due to the 64bit application, I have also tried to compile under 64bit.
  I had no problem with this script using with 32bit apps, however when I try this on Solitaire it fails to get the base address, which then fails to workout the correct offsets ect.
  Here is the script:</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;Windows.h&gt;
  #include &lt;TlHelp32.h&gt;
  using namespace std;
  
  DWORD dwGetModuleBaseAddress(DWORD dwProcessID, TCHAR *lpszModuleName)
  {
      HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwProcessID);
      DWORD dwModuleBaseAddress = 0;
      if (hSnapshot != INVALID_HANDLE_VALUE)
      {
          MODULEENTRY32 ModuleEntry32 = { 0 };
          ModuleEntry32.dwSize = sizeof(MODULEENTRY32);
          if (Module32First(hSnapshot, &amp;ModuleEntry32))
          {
              do
              {
                  if (_tcscmp(ModuleEntry32.szModule, lpszModuleName) == 0)
                  {
                      dwModuleBaseAddress = (DWORD)ModuleEntry32.modBaseAddr;
                      break;
                  }
              } while (Module32Next(hSnapshot, &amp;ModuleEntry32));
          }
          CloseHandle(hSnapshot);
      }
      return dwModuleBaseAddress;
  }
  
  int main()
  {
      DWORD address = 0xBAFA8;
      HWND hwnd = FindWindow(0, L"Solitaire");
      DWORD pid;
      int data = 0;
      int newData = 0;
      if (hwnd)
      {
          GetWindowThreadProcessId(hwnd, &amp;pid);
          HANDLE phandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
          if (phandle)
          {
              DWORD base = dwGetModuleBaseAddress(pid, L"Solitaire.exe");
              cout &lt;&lt; "Base: " &lt;&lt; (void*)base &lt;&lt; endl;
  
              ReadProcessMemory(phandle, (LPCVOID)(base + address), &amp;data, sizeof(data), 0);
          }
          else {
              cout &lt;&lt; "Couldnt get handle" &lt;&lt; endl;
          }
  
      }
      else {
          cout &lt;&lt; "Couldn't find window" &lt;&lt; endl;
      }
      cin.get();
      return 0;
  }
  </code></pre>
  
  <p>The problem may be that the function I use uses MODULE32, however I have tried other functions (that uses EnumModules) which still fails to return address.</p>
  
  <p>Any ideas how to get base address of 64bit application or to get this script working?</p>
  
  <p>Thanks</p>