title: C++: reading memory of another process
body: <p>I'd like to have a function that allows me to read the memory of another process.
  I was thinking about something like this (pseudo code):</p>
  
  <pre><code>staticAddress = 0x026E0DC4
  processId = GetProcessIdByName(processName)
  processHandle = GetProcessHandle(processId)
  processBaseAddress = GetBaseAddress(processHandle)
  addressToRead = processBaseAddress+staticAddress
  readValueAsInt = ReadMemoryInt(processHandle, addressToRead)
  readValueAsFloat = ReadMemoryFloat(processHandle, addressToRead)
  readValueAsString = ReadMemoryString(processHandle, addressToRead)
  </code></pre>
  
  <p>Would that even be possible?
  Here is what I got so far:</p>
  
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;conio.h&gt;
  #include &lt;tlhelp32.h&gt;
  #include &lt;string&gt;
  #include &lt;psapi.h&gt;
  #pragma comment( lib, "psapi" )
  
  int GetProcessId(char* ProcName) {
      PROCESSENTRY32 pe32;
      HANDLE hSnapshot = NULL;
      pe32.dwSize = sizeof( PROCESSENTRY32 );
      hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
  
      if( Process32First( hSnapshot, &amp;pe32 ) ) {
          do {
              if( strcmp( pe32.szExeFile, ProcName ) == 0 )
                  break;
          } while( Process32Next( hSnapshot, &amp;pe32 ) );
      }
  
      if( hSnapshot != INVALID_HANDLE_VALUE )
          CloseHandle( hSnapshot );
  
      return pe32.th32ProcessID;  
  }
  
  int GetModuleBase(HANDLE processHandle, string &amp;sModuleName) 
  { 
     HMODULE *hModules; 
     char szBuf[50]; 
     DWORD cModules; 
     DWORD dwBase = -1; 
     //------ 
  
     EnumProcessModules(processHandle, hModules, 0, &amp;cModules); 
     hModules = new HMODULE[cModules/sizeof(HMODULE)]; 
  
     if(EnumProcessModules(processHandle, hModules, cModules/sizeof(HMODULE), &amp;cModules)) { 
        for(int i = 0; i &lt; cModules/sizeof(HMODULE); i++) { 
           if(GetModuleBaseName(processHandle, hModules[i], szBuf, sizeof(szBuf))) { 
              if(sModuleName.compare(szBuf) == 0) { 
                 dwBase = (DWORD)hModules[i]; 
                 break; 
              } 
           } 
        } 
     } 
  
     delete[] hModules; 
  
     return dwBase; 
  }
  
  
  int ReadMemoryInt(HANDLE processHandle, LPCVOID address) {
      //LPVOID buffer = ??;
      //SIZE_T size = ??;
      SIZE_T NumberOfBytesToRead = 4; //??
      ReadProcessMemory(processHandle, address, buffer, size, NumberOfBytesToRead)
      return buffer; //??
  }
  
  int ReadMemoryFloat(HANDLE processHandle, LPCVOID address) {
      //LPVOID buffer = ??;
      //SIZE_T size = ??;
      SIZE_T NumberOfBytesToRead = 8; //??
      ReadProcessMemory(processHandle, address, buffer, size, NumberOfBytesToRead)
      return buffer; //??
  }
  
  int ReadMemoryString(HANDLE processHandle, LPCVOID address) {
      //LPVOID buffer = ??;
      //SIZE_T size = ??;
      SIZE_T NumberOfBytesToRead = 999; //??
      ReadProcessMemory(processHandle, address, buffer, size, NumberOfBytesToRead)
      return buffer; //??
  }
  
  int main()
  {
      //read an integer from "Program.exe"+0x05D8A3C4
      int address = 0x05D8A3C4;
      char* processName = "Program.exe";
      int processId = GetProcessId(processName);
      HANDLE processHandle = OpenProcess(PROCESS_ALL_ACCESS, false, processId);
      int processBaseAddress = GetModuleBase(processHandle, (string)"Program.exe";
      LPCVOID actualAddress = processBaseAddress+address;
      int readValue = ReadMemory(processHandle, actualAddress);
      std::cout &lt;&lt; readValue &lt;&lt; std::endl;
      CloseHandle(processHandle);
      return 0;
  }
  </code></pre>
  
  <p>As you can see form the question marks in the code I'm really unsure about the "buffer" and "size" parameters of ReadProcessMemory. I'd really appreciate it if someone could help me figuring this out.</p>