Is there a better way to read memory than this?

Ok so im trying to get better at coding the most efficient way possible. I have a program where i have to read alot of memory addresses (around 30). Right now they are all just in a long list of if statements, i was wondering if there is a better way i could be doing this?

This is just one section that i have it broken up into for better organization.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        if(!ReadProcessMemory(hprocess,(void *)0x0000000,(void *)&p1,sizeof(p1),0)){
            p1 = 0;
        }
        if(!ReadProcessMemory(hprocess,(void *)0x0000000,(void *)&p2,sizeof(p2),0)){
            p2 = 0;
        }
        if(!ReadProcessMemory(hprocess,(void *)0x0000000,(void *)&p3,sizeof(p3),0)){
            p3 = 0;
        }
        if(!ReadProcessMemory(hprocess,(void *)0x0000000,(void *)&p4,sizeof(p4),0)){
            p4 = 0;
        }
        if(!ReadProcessMemory(hprocess,(void *)0x0000000,(void *)&p5,sizeof(p5),0)){
            p5 = 0;
        }
        if(!ReadProcessMemory(hprocess,(void *)0x0000000,(void *)&p6,sizeof(p6),0)){
            p6 = 0;
        }
        if(!ReadProcessMemory(hprocess,(void *)0x0000000,(void *)&p7,sizeof(p7),0)){
            p7 = 0;
        }
        if(!ReadProcessMemory(hprocess,(void *)0x0000000,(void *)&p8,sizeof(p8),0)){
            p8 = 0;
        }
Last edited on
If you can, try to read the memory into larger blocks so you don't have to call ReadProcessMemory as much as it will be pretty slow.

Or, you could do something like this:
1
2
3
4
5
6
7
bool ReadMulMem(HANDLE hProcess, DWORD_PTR Addy[], DWORD num_addys, size_t size, LPVOID* buffer) {
virtualprotect()...
for(DWORD i = 0; i < num_addys; ++i) {
if(!ReadProcessMemory(Addy[i], buffer[i])...)
return false;
virtualprotect()...
return true;
Out of interest, does your existing code, as posted, work?

It looks like you're trying to repeatedly read address 0x0 of the target process into assorted variables?

Andy

Ya it works perfectly fine, i just set all the addresses to 0x0, the actual values are different.
Well, I'm with implicit's comment on reading bigger blocks.

But whether that's a good idea depends on how the addresses are distributed, and how often you're reading them.
Topic archived. No new replies allowed.