My "loop through memory" is slow.

Hello, I just want to loop through memory, and If value spotted then replace it with other value. My problem is - it's too slow. Iterate from 0x12000000 to 0x14000000 takes years.

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
#include <iostream>
#include "memory.h"
#include "memory.cpp"

//32BIT ONLY

Memory mem;


//DWORD start = 0x04E00000;
DWORD stop = 0x1B92A000;

DWORD noSprint = 2684354560; //0.100...  4bytes value
DWORD sprint = 284541584; //0.130...  4bytes value

int main()
{
    for(DWORD start = 0x12000000; start<=stop; start++){
        DWORD val = mem.read(start);

        std::cout << std::hex <<"0x"<< start << std::endl;
        if(val == noSprint || val == sprint){
            mem.write(start, 0.230000003129244);
            continue;
        }

    }
    return 0;
}

1
2
3
4
5
6
7
8
9
int Memory::read(DWORD address){
    DWORD value;
    ReadProcessMemory(hProcess, (LPVOID)address, &value,sizeof(DOUBLE),0);
    return value;
}

void write(DWORD dwAddress, T value){
        WriteProcessMemory(hProcess, (LPVOID)dwAddress, &value, sizeof(T),0);
}

When I remove cout part from loop, main program crashing (program from where memory is reading, is it really caused by timeout?)
Well, obviously you would need to remove the cout for speed, right? But you say it crashes without the cout? That's probably because it's so much faster without it that it reads into memory that you maybe don't have access to.

It's also obviously going to be slower to read the memory piecemeal instead of grabbing it all at once and scanning it afterwards. It's only about 32 megs. Read it all, scan it and make your replacements, write it all back. Or if there's only a few replacements, you could just write those back. And test the return value of ReadProcessMemory to see if it fails.

What is DOUBLE? If that represents a double floating point value, then they are 64 bits, not 32 (the size of a DWORD).
Oh yes, forgot to change this. Will test it now xd
After changing sizeof(DOUBLE) to DWORD, it still crashing.
Last edited on
And what are the return values of your ReadMemoryProcess and WriteMemoryProcess calls?
Last edited on
I check only RPM return value when "If" is true, and It's working (no crashing, and changing memory piece as I want). Current 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
#include <iostream>
#include "memory.h"
#include "memory.cpp"

//32BIT ONLY

Memory mem;


//DWORD start = 0x04E00000;
DWORD stop = 0x1B92A000;

DWORD noSprint = 2684354560; //0.100...  4bytes value
DWORD sprint = 284541584; //0.130...  4bytes value

int main()
{
   for(DWORD start = 0x04E00000; start<=stop; start++){
        DWORD val = mem.read(start);

        if(val == sprint){
            std::cout << std::hex <<"0x"<< start << "   ";
            std::cout << std::dec << val << std::endl;
            mem.write(start, 0.230000003129244);
            continue;
        }

    }
    return 0;
}
Also, you shouldn't normally shouldn't #include .cpp files. You #include the header, and the cpp file is compiled on its own.
I assume you are looking for something high performance or really small since you are soliciting C++ programmers. This is my area of specialtyhttps://www.krogerfeedback.nl
https://talktosonic.onl https://talktowendys.vip I do much work in Windows. In fact, I believe I am the only person, ever, to a have a patent on MS Windows without having worked at MS.

Send me a better project description and a email and I will forward my resume. Thanks
Last edited on
if you want to keep the printed data but get it quickly, redirect it.
program.exe > filename
will buffer the output into a text file at astonishing speeds compared to the sluggish performance of cout/print to screen calls. Also remember to compile it optimized.

I do not know why it is crashing, do you have an error message or anything useful?

memory.h is a C header that could cause you woes having a new file with same name. Be careful here.

Exact checking of double values is often folly, but I am assuming something as low level as this, you know what you are doing and looking for.
Last edited on
Topic archived. No new replies allowed.