René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

CreateProcess, WaitForSingleObject and ReadProcessMemory

Processes are created using CreateProcess.
#include <windows.h>

#include <iostream>

using namespace std;


int main() {
  STARTUPINFO         si;
  PROCESS_INFORMATION pi;

  ZeroMemory (&si, sizeof(si));
  si.cb=sizeof (si);


  if (! <b>CreateProcess</b>(
    "target.exe",
    0,                    // command line
    0,                    // process attributes
    0,                    // thread attributes
    0,                    // inherit handles
    CREATE_NEW_CONSOLE,   // creation flags
    0,                    // environment
    0,                    // cwd
    &si,
    &pi
    )
  )  {
    cout << "could not start process" << endl;
  }

  <b>WaitForSingleObject</b>(pi.hProcess,INFINITE); return 0;
}

Reading the other process' memory with ReadProcessMemory

The Master Programm: it creates target.exe and waits for an address and then reads in the target process using ReadProcessMemory
#include <windows.h>

#include <iostream>

using namespace std;

int main() {
  STARTUPINFO         si;
  PROCESS_INFORMATION pi;

  ZeroMemory (&si, sizeof(si));
  si.cb=sizeof (si);


  if (! CreateProcess(
    "target.exe",
    0,                    // command line
    0,                    // process attributes
    0,                    // thread attributes
    0,                    // inherit handles
    CREATE_NEW_CONSOLE,   // creation flags
    0,                    // environment
    0,                    // cwd
    &si,
    &pi
    )
  )  {
    cout << "could not start process" << endl;
  }


  int buf;
  int addr;

  cout << "Enter the target's address: " << flush;
  cin >> addr;

  DWORD dummy;

  if (!
    <b>ReadProcessMemory</b>(
      pi.hProcess,
      (void*) addr,
      (void*) &buf,
      4,
      &dummy)
    )  {
    cout << "failed to read process" << endl;
  }

  cout << buf << endl;

  <b>WaitForSingleObject</b>(pi.hProcess,INFINITE);

  return 0;
}
The target:
#include <iostream>

using namespace std;

int main() {
  int i;
  int y;
  cout << "Target started" << endl;
  cout << "Enter a number: ";

  cin >> i;

  cout << "The address is: " << (int) &i << endl;
  cout << "Enter the address in the master" << endl;

  cin >> y;

  return 0;
}