I hate errors

Posts 1–15 of 31 · Page 1 of 3
I hate errors
Hello, I'm trying to scan memory here but I keep getting an error. Here's the code.
Code:
#include <windows.h>
#include <conio.h>
#include <iostream>

using namespace std;

DWORD pid;
HWND hwnd;
HANDLE handle;

DWORD base = 0x0000000;
DWORD end = 0x7FFFFFFF;
DWORD bSize = 0x5D1745D;
LPBYTE buffer = new byte[bSize];
int value = 10;

int main()
{
    hwnd = FindWindow(0,"Minesweeper");
    if(!hwnd)
    {
             cout <<"Window not found!";
             getch();
             return 0;
    }
    
    GetWindowThreadProcessId(hwnd,&pid);
    handle = OpenProcess(PROCESS_ALL_ACCESS,0,pid);
    if(!handle)
    {
               cout <<"Could not get window handle!";
               getch();
               return 0;
    }
    
    while ( base != end)
    {
          ReadProcessMemory(handle,(LPVOID)base,buffer,bSize,0);
          

          if( (end-base) > bSize)
          {
              base += bSize;
          } else if( (end-base) < bSize)
          {
                 base += (end-base);
          }
          delete[] buffer;
    }
    cout <<"Done";
    getch();
}
It compiles and it does what it's suposed to, but when I add:
Code:
for(int i = 0; i < bSize; i++ )
          {
                  if(buffer[i] == 10)
                  {
                               cout << hex << base + i << endl;
                  }
          }
To check if any of the bytes are equal to 10 I just get an error. Here's the full code.
Code:
#include <windows.h>
#include <conio.h>
#include <iostream>

using namespace std;

DWORD pid;
HWND hwnd;
HANDLE handle;

DWORD base = 0x0000000;
DWORD end = 0x7FFFFFFF;
DWORD bSize = 0x5D1745D;
LPBYTE buffer = new byte[bSize];
int value = 10;

int main()
{
    hwnd = FindWindow(0,"Minesweeper");
    if(!hwnd)
    {
             cout <<"Window not found!";
             getch();
             return 0;
    }
    
    GetWindowThreadProcessId(hwnd,&pid);
    handle = OpenProcess(PROCESS_ALL_ACCESS,0,pid);
    if(!handle)
    {
               cout <<"Could not get window handle!";
               getch();
               return 0;
    }
    
    while ( base != end)
    {
          ReadProcessMemory(handle,(LPVOID)base,buffer,bSize,0);
          for(int i = 0; i < bSize; i++ )
          {
                  if(buffer[i] == value)
                  {
                               cout << hex << base + i << endl;
                  }
          } 

          if( (end-base) > bSize)
          {
              base += bSize;
          } else if( (end-base) < bSize)
          {
                 base += (end-base);
          }
          delete[] buffer;
    }
    cout <<"Done";
    getch();
}
It tells me i'm getting an unhandled win32 exception.
Thanks.
Hmm The problem seems to be that even thought you used readprocessmemory, you are still getting an access violation. I've never actually gotten the read and write process memory functions to work thought. I'm quite intrigued by this though. I'll play around and see what happens
I fixed it. I deleted the buffer after the while loop and it doesn't give me that error. But now it doesn't find any addresses with the value of 10.

I keep running into new problems.
It's not DMA.

Starting the search at 0x0000 doesn't work. I have to find the base address I think. Not sure if there's any function for that but if there is, let me know. Thanks.

If anyone would be kind enough to guide me through this little memory scan project of mine, PM me.

Thank you.
Hmmmm.... I wish BA was here. I'm not familiar with the Windows API. =/
Quote Originally Posted by why06 View Post
Hmmmm.... I wish BA was here. I'm not familiar with the Windows API. =/
Where's BA? Is he dead? Do I get to go to his funeral? Will there be cookies?
Quote Originally Posted by Lolland View Post
Where's BA? Is he dead? Do I get to go to his funeral? Will there be cookies?
You'll get flowers and pretty dresses. Frolicking in the sunflower fields. Doesn't that sound exciting?
If it wasn't for errors, there'd be no one to correct you and tell you where you went wrong. :P Just saying, LOL. Can't be any help since I'm starting C++. >.>
Does it really matter if i started scanning at an address that has no memory? Or does it affect the scan in some way? Cause right now, I have no idea what to do. I'm completely stuck.
Who's BA?
Is he some sort of programming God?
I Don't know.. must be some crazy guy...

Anyway.. going off your latest posted code (so I didn't do a full rewrite the way I would do it )
a) First off you have a buffer that's oh... 93mb in size.. let's not do that and
knock bSize down to something a bit more sensible like 0xFFFF; (64kb)

b) You then proceed to delete that buffer INSIDE a reading loop, so the 2nd time it hits you're trying to write data to a garbage location => Crash

c) YOu should check the returned 'bytes read' from the ReadProcessMemory incase you don't read the full size, which would throw up as well if that were to happen

Here's a snippet that'll get you going:
Code:
DWORD bSize = 0xFFFF; 
DWORD* buffer = new DWORD[bSize];

..... bla bla bla ...

while ( base != end)
	{
		ULONG bytesRead = 0;
		if(ReadProcessMemory(handle,(LPVOID)base,buffer,bSize,&bytesRead) != 0)
		{
			for(DWORD i = 0; i < bytesRead; i++ )
			{
				if(buffer[i] == value)
				{
					cout << hex << base + i << endl;
				}
			} 
		}
		if( (end-base) > bSize)
		{
			base += bSize;
		} else if( (end-base) < bSize)
		{
			base += (end-base);
		}
		
	}
	delete[] buffer;
I love you.
I was right, programming God for sure.

Thanks so much. Although i'm almost 100% sure I will run into another problem concerning this topic.

Quick question, is it comparing each single byte to 10? If so, how do I compare 4 bytes to 10?

Sorry, noob question.
BA You are absolutely awesome =).
He is indeed, but like I said, I was almost 100% sure I would run into another problem, and I did.

I scanned using cheat engine and found about 400 addresses and when I used this method i got over 1600.

Was there a reason you changed byte to dword? I have no idea if this even affects the scan at all. But I don't think i'm getting the results i'm looking for with this scanner.

Thanks.
Posts 1–15 of 31 · Page 1 of 3
This thread is closed for replies.

Similar Threads

Tags for this Thread

Need help?