icemens
06-28-2008, 10:34 AM
Okay, I have a problem with ReadProcessMemory. It looks like I don't have the access rights to read the memory, weird.
I have tried to gain them in different ways as you can see, but neither of them worked.
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#define HEALTH_LABEL 101
#define MANA_LABEL 102
char szClassName[ ] = "D2Drinker";
int ReadMem(char *window, int dAddr)
{
int buffer;
//DWORD prevProtection;
HWND hWnd = FindWindow(0, window);
if (!hWnd) MessageBox(0, "Error finding window", 0, MB_OK);
DWORD proc_id;
GetWindowThreadProcessId(hWnd, &proc_id);
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, proc_id);
//VirtualProtectEx(hProcess, (LPVOID)dAddr, 4, PAGE_EXECUTE_READWRITE, &prevProtection);
//VirtualUnlock((LPVOID)dAddr, 2);
if (!ReadProcessMemory(hProcess, (LPCVOID)dAddr, &buffer, 2, NULL))
MessageBox(0, "Error doing memory reading", 0, MB_OK);
//VirtualProtectEx(hProcess, (LPVOID)dAddr, 4, prevProtection, &prevProtection);
return buffer;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
HWND healthLabel = CreateWindow("STATIC", "Health is UNKNOWN", WS_CHILD, 5, 5, 200, 25,
hwnd, (HMENU)HEALTH_LABEL, GetModuleHandle(0), 0);
ShowWindow (healthLabel, SW_SHOW);
HWND manaLabel = CreateWindow("STATIC", "Mana is UNKNOWN", WS_CHILD, 5, 30, 200, 25,
hwnd, (HMENU)MANA_LABEL, GetModuleHandle(0), 0);
ShowWindow (manaLabel, SW_SHOW);
int health = ReadMem("Diablo II", 0x6fbb2c21);
char buffer[33];
itoa(health, buffer, 10);
SetDlgItemText(hwnd, HEALTH_LABEL, buffer);
}
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"D2 Drinker", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
ShowWindow (hwnd, nFunsterStil);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
I have tried to gain them in different ways as you can see, but neither of them worked.
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#define HEALTH_LABEL 101
#define MANA_LABEL 102
char szClassName[ ] = "D2Drinker";
int ReadMem(char *window, int dAddr)
{
int buffer;
//DWORD prevProtection;
HWND hWnd = FindWindow(0, window);
if (!hWnd) MessageBox(0, "Error finding window", 0, MB_OK);
DWORD proc_id;
GetWindowThreadProcessId(hWnd, &proc_id);
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, proc_id);
//VirtualProtectEx(hProcess, (LPVOID)dAddr, 4, PAGE_EXECUTE_READWRITE, &prevProtection);
//VirtualUnlock((LPVOID)dAddr, 2);
if (!ReadProcessMemory(hProcess, (LPCVOID)dAddr, &buffer, 2, NULL))
MessageBox(0, "Error doing memory reading", 0, MB_OK);
//VirtualProtectEx(hProcess, (LPVOID)dAddr, 4, prevProtection, &prevProtection);
return buffer;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
HWND healthLabel = CreateWindow("STATIC", "Health is UNKNOWN", WS_CHILD, 5, 5, 200, 25,
hwnd, (HMENU)HEALTH_LABEL, GetModuleHandle(0), 0);
ShowWindow (healthLabel, SW_SHOW);
HWND manaLabel = CreateWindow("STATIC", "Mana is UNKNOWN", WS_CHILD, 5, 30, 200, 25,
hwnd, (HMENU)MANA_LABEL, GetModuleHandle(0), 0);
ShowWindow (manaLabel, SW_SHOW);
int health = ReadMem("Diablo II", 0x6fbb2c21);
char buffer[33];
itoa(health, buffer, 10);
SetDlgItemText(hwnd, HEALTH_LABEL, buffer);
}
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"D2 Drinker", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
ShowWindow (hwnd, nFunsterStil);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}