ReadProcessMemory dont work for me.

hello guys,
i try to access some process that runs on my PC and wanted to read a memory from
some adress of the process.

the problem is that ReadProcessMemory func return 0 and GetLastError show me 5.
i dont know the reason for that, pls help me if you can ^^

(i am begginer in programing and my english is like my programing skills so sry for that :D )

here the code:

my main.cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "MemReader.h"

int main()
{
	MemReader MemPtr;
	
	GetPID(&MemPtr, "l2.bin");
	Open(&MemPtr);
	Read(&MemPtr, 0x001E10F0, 4);
        Close(&MemPtr);
        system("PAUSE");
	return 0;
}



my MemReader.h file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#pragma once
#include <Windows.h>
#include <TlHelp32.h>
#include <iostream>
#include <winternl.h>


typedef struct MemReader
{
	int m_buffer;             // byte  = unsigned char
	size_t m_bufferSize = 4;
	char* m_pName;
	HANDLE m_hProc;
	DWORD m_pID;                // DWORD = unsigned long
	

}MemReader;

void GetPID (MemReader* MemPtr, char* pName);

void Open   (MemReader* MemPtr, DWORD accessRights = MAXIMUM_ALLOWED);
void Close  (MemReader* MemPtr);

void Read   (MemReader* MemPtr, DWORD from, size_t size);



my MemReader.cpp file:
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "MemReader.h"



//=========================================================================


void GetPID(MemReader* MemPtr, char* pName)
{
	
	HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	PROCESSENTRY32 pInfo;
	pInfo.dwSize = sizeof(PROCESSENTRY32);
	MemPtr->m_pName = pName;
	


	if (Process32First(snapshot, &pInfo))
	{
		while (Process32Next(snapshot, &pInfo))
		{
			if (_stricmp(MemPtr->m_pName, pInfo.szExeFile) == 0)
			{
				MemPtr->m_pID = pInfo.th32ProcessID;
				CloseHandle(snapshot);
				return;
			}
		}
	}

	CloseHandle(snapshot);
	MemPtr->m_pID = 0;
	return;
}


//=========================================================================

void Open(MemReader* MemPtr , DWORD accessRights)
{
	printf("Process ID: %X \n", MemPtr->m_pID);

	MemPtr->m_hProc = OpenProcess(accessRights, false, MemPtr->m_pID);
	
	if (MemPtr->m_hProc)
		printf("Process open \n");
	else
		printf("Error culdnot open process \n");
}


//=========================================================================

void Close(MemReader* MemPtr)
{
	CloseHandle(MemPtr->m_hProc);
}


//=========================================================================

void Read(MemReader* MemPtr, DWORD from, size_t size)
{
       int flag = 0;
	
       flag = ReadProcessMemory(MemPtr->m_hProc, (LPCVOID)from, &MemPtr->m_buffer, size, NULL);
	if (!flag)
		printf("The Read func NOT succsess \n");

	DWORD r = GetLastError();
	printf("%d \n", r); // show 5!

}





the result is:

ID: 15ED
Process open
The Read func NOT succsess
5
Last edited on
void Open (MemReader* MemPtr, DWORD accessRights = MAXIMUM_ALLOWED);
I've never seen MAXIMUM_ALLOWED macro. I think you should use PROCESS_ALL_ACCESS macro or PROCESS_VM_READ if you just need to read process memory.

The error code is ERROR_ACCESS_DENIED* so it's possible that the address does not belong to that process.

*system error codes: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
Last edited on
Topic archived. No new replies allowed.