I'm trying to make a program which writes and reads to the notepad memory directly. Unfortunately, I am a total newbie to the WIN32 API and frankly I don't know how to use VirtualProcessEx but I gave it my best guess. You can see what I'm trying to do, but just to summarize: 1. Get the notepad process handle 2. Read the text from the provided memory address which is in the notepad process. 3. Write some new text in there.
Without VirtualProcessEx I was getting an access denied code 998 error, but I read somewhere that I can give READWRITE access to the page which should solve this. I'm sort of on pins and needles here as there really isn't much about this online. I also own Programming Windows Fifth Edition by Charles Petzold and WriteProcessMemory is no where to be found in there either. One of the toughest parts for me is converting the code to support 64 bit. I have no idea if the PINT64 is how to do that, but the original instructions for this said to use a PBYTE but that can't be right. The machine is 64 bit as well as the notepad program. Please see code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 | #include <stdio.h>
#include <Windows.h>
int main()
{
BOOL retVal;
char* newValue = "Macho Man Randy Savage";
HWND hwnd = FindWindowA(NULL, "Untitled - Notepad");
if(hwnd == NULL)
{
printf("Cannot find window.\n");
Sleep(3000);
exit(-1);
}
else
{
DWORD procID;
GetWindowThreadProcessId(hwnd, &procID);
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, procID);
if(!procID)
{
printf("Cannot obtain process.\n");
Sleep(3000);
exit(-1);
}
else
{
int old = 0;
INT64 mem = 0x1DB17214AA0;
char container[50];
ReadProcessMemory(handle,(PINT64*)0x1DB1725D600,&container[0],sizeof(container),0);
container[50] = '\0';
printf("Message: %s\n", container);
VirtualProcessEx(handle,(PINT64*)mem,sizeof(mem),PAGE_EXECUTE_READWRITE, &old);
retVal = WriteProcessMemory(handle, (LPVOID)0x1DB17214AA0,&newValue,sizeof(newValue),0);
if(retVal == 0)
{
printf("Write failed. Error Code: \t %d",GetLastError());
}
}
}
return (EXIT_SUCCESS);
}
|