god, thats awful code..
NOT closing the process handles, crap buffer sizes, 12 bytes max in 1 write... not exiting with a return value (for processing to see if the stuff actually wrote)...crap crap crap
considerably better code can be found on the net, in c or asm or delphi or even visual basic...
please, if you're going to put source code for people to use, at least do some work on it, and document the code properly
Code:
//=========================
#include <iostream.h>
#include <windows.h>
//=========================
static volatile HANDLE ProcessHandle = (HANDLE) INVALID_HANDLE_VALUE;
static volatile BOOL EngineInUse = FALSE;
//=========================
BOOL engine_close_process() {
if (EngineInUse) {
// are we in use?
if (CloseHandle(ProcessHandle)) {
// yup, so close the process handle
EngineInUse = FALSE;
return TRUE;
}
}
return FALSE;
}
//=========================
BOOL engine_open_process(char * processwindowtitle) {
HWND TargetWindowHandle = (HWND) -1;
DWORD ProcessId = -1;
HANDLE WindowProcessId = (HANDLE) INVALID_HANDLE_VALUE;
if (EngineInUse) {
// we are already in use...
return FALSE;
}
TargetWindowHandle = FindWindow(NULL, processwindowtitle); // see if it exist
if (TargetWindowHandle) {
// got the window handle...
Process_Id = GetWindowThreadProcessId(TargetWindowHandle, &ProcessId); //get a PROCESS number
if (Process_Id) {
// we have a valid process id, now to open it...
ProcessHandle = OpenProcess(PROCESS_ALL_ACCESS,NULL, ProcessId);
if (ProcessHandle) {
// process succesfully opened
EngineInUse = TRUE;
return TRUE;
}
}
return FALSE;
}
//=========================
BOOL engine_write_process(LPVOID lpBaseAddress, LPVOID lpBuffer, DWORD nSize) {
BOOL SuccessCode = FALSE;
if (EngineInUse) {
// are we in use
if (ProcessHandle) {
// do we have a process handle
DWORD BytesWritten = 0;
BOOL ProcessSuspended = FALSE;
if (SuspendThread(ProcessHandle) != (DWORD) -1) {
// suspend the thread - its safer
ProcessSuspended = TRUE;
}
if ((WriteProcessMemory(ProcessHandle, lpBaseAddress, lpBuffer, nSize, &BytesWritten) && BytesWritten == nSize) {
// write was successful
// flush the instruction cache (for safety)
FlushInstructionCache(ProcessHandle, lpBaseAddress, nSize);
SuccessCode = TRUE;
}
// resume the process if we suspended it
if (ProcessSuspended) {
ResumeThread(ProcessHandle);
}
}
}
return SuccessCode;
}
//=========================
BOOL engine_read_process(LPVOID lpBaseAddress, LPVOID lpBuffer, DWORD nSize) {
BOOL SuccessCode = FALSE;
if (EngineInUse) {
if (ProcessHandle) {
DWORD BytesRead = 0;
BOOL ProcessSuspended = FALSE;
if (SuspendThread(ProcessHandle) != (DWORD) -1) {
ProcessSuspended = TRUE;
}
if ((ReadProcessMemory(ProcessHandle, lpBaseAddress, lpBuffer, nSize, &BytesWritten) && BytesRead == nSize) {
SuccessCode = TRUE;
}
if (ProcessSuspended) {
ResumeThread(ProcessHandle);
}
}
}
return SuccessCode;
}
//=========================
BOOL engine_kill_process_and_close() {
if (EngineInUse) {
if (TerminateProcess(ProcessHandle, (UINT) 0x0D1ED1E)) {
CloseHandle(ProcessHandle);
EngineInUse = FALSE;
return TRUE;
}
}
return FALSE;
}
//=========================