Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++] Help.
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Sun Dec 27, 2009 12:18 pm    Post subject: Reply with quote

slovach wrote:
lurc wrote:
1. You're initializing a new variable every single time it loops (which is a lot of times.)

Solution: Place variable definitions outside of loop.

SIZE_T ret;
bool Read;

do { ... } while ( ... );


no, you don't need to worry about this.
this is not what is happening in actuality.


Really? Thats pretty cool.
I'm guessing that it'll be optimized automatically anyways?

Good coding practice to do so anyways?

_________________
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sun Dec 27, 2009 12:51 pm    Post subject: Reply with quote

lurc wrote:
slovach wrote:
lurc wrote:
1. You're initializing a new variable every single time it loops (which is a lot of times.)

Solution: Place variable definitions outside of loop.

SIZE_T ret;
bool Read;

do { ... } while ( ... );


no, you don't need to worry about this.
this is not what is happening in actuality.


Really? Thats pretty cool.
I'm guessing that it'll be optimized automatically anyways?

Good coding practice to do so anyways?

yes and yes. it's essentially hoisting of loop invariant
Back to top
View user's profile Send private message
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Sun Dec 27, 2009 12:52 pm    Post subject: Reply with quote

lurc wrote:

Really? Thats pretty cool.
I'm guessing that it'll be optimized automatically anyways?

Good coding practice to do so anyways?

Yes (in Visual C++) it's optimized assuming you use the default settings. But when building in debug mode they might not be optimized.
It would be a good coding practice I guess because not all compilers will optimize it.
Back to top
View user's profile Send private message
iPromise
Grandmaster Cheater
Reputation: -1

Joined: 27 Jun 2009
Posts: 529
Location: Canada

PostPosted: Sun Dec 27, 2009 2:57 pm    Post subject: Reply with quote

EDIT

Mixed my own code with Slugs' to get a good scan, didn't work..

Code:

               if (!strcmp("Byte", GetType))
               {
                  int Value = atoi(GetValue);

                  MEMORY_BASIC_INFORMATION mbi = {0};
                  SYSTEM_INFO si;

                  GetSystemInfo(&si);

                  DWORD min = (DWORD) si.lpMinimumApplicationAddress;
                  DWORD max = (DWORD) si.lpMaximumApplicationAddress;

                  DWORD dwEndAddr;

                  bool read;

                  VirtualQuery((VOID *) ( (int) mbi.BaseAddress + mbi.RegionSize), &mbi, sizeof(MEMORY_BASIC_INFORMATION));
                  VirtualQuery((VOID *) ( (int) mbi.BaseAddress + mbi.RegionSize), &mbi, sizeof(MEMORY_BASIC_INFORMATION));

                  dwEndAddr = ( DWORD )mbi.BaseAddress + mbi.RegionSize - 1 - ( !Value ? 0 : Value * 2 );

                  cout << "Base: " << mbi.BaseAddress << endl;
                  cout << "End Address: " << dwEndAddr << endl;

                  DWORD i = (DWORD) mbi.BaseAddress;

                  if (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_EXECUTE_READWRITE)
                  {
                     do
                     {                        
                        read = ReadByte(min, Value);

                        if (read == true)
                        {
                           SendMessageX(ListBox1, LB_ADDSTRING, 0, (LPARAM) ConvertAddress((LPVOID) min).c_str());
                        }         

                        ++i;
                     } while (i <= dwEndAddr);
                  }                  

                  MessageBoxA(hWnd, "Done Scan, now adding the results!", "OK", MB_OK);               

                  GetResults();
               }
            }
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Dec 27, 2009 4:03 pm    Post subject: Reply with quote

why are you not making use of C++ if you are using C++? srsly.


lurc wrote:
slovach wrote:
lurc wrote:
1. You're initializing a new variable every single time it loops (which is a lot of times.)

Solution: Place variable definitions outside of loop.

SIZE_T ret;
bool Read;

do { ... } while ( ... );


no, you don't need to worry about this.
this is not what is happening in actuality.


Really? Thats pretty cool.
I'm guessing that it'll be optimized automatically anyways?

Good coding practice to do so anyways?


the variables are created when you enter the loop scope.

Code:
int butt;
while(1)
{      
   butt = 1;
   std::cout << butt;
}


Code:
   {      
      butt = 1;
013F13C7  mov         dword ptr [butt],1
      std::cout << butt;
013F13CE  mov         esi,esp
013F13D0  mov         eax,dword ptr [butt]
013F13D3  push        eax 
013F13D4  mov         ecx,dword ptr [__imp_std::cout (13F8288h)]
013F13DA  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F828Ch)]
013F13E0  cmp         esi,esp
013F13E2  call        @ILT+315(__RTC_CheckEsp) (13F1140h)
   }
013F13E7  jmp         main+1Eh (13F13BEh)



same as

Code:
while(1)
{      
   int butt = 1;
   std::cout << butt;
}



Code:
      {
int butt = 1;
002F13C7  mov         dword ptr [butt],1
      std::cout << butt;
002F13CE  mov         esi,esp
002F13D0  mov         eax,dword ptr [butt]
002F13D3  push        eax 
002F13D4  mov         ecx,dword ptr [__imp_std::cout (2F8288h)]
002F13DA  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (2F828Ch)]
002F13E0  cmp         esi,esp
002F13E2  call        @ILT+315(__RTC_CheckEsp) (2F1140h)
   }
002F13E7  jmp         main+1Eh (2F13BEh)


Last edited by hcavolsdsadgadsg on Sun Dec 27, 2009 6:56 pm; edited 1 time in total
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sun Dec 27, 2009 6:20 pm    Post subject: Reply with quote

iPromise wrote:
EDIT

Mixed my own code with Slugs' to get a good scan, didn't work..

Code:

               if (!strcmp("Byte", GetType))
               {
                  int Value = atoi(GetValue);

                  MEMORY_BASIC_INFORMATION mbi = {0};
                  SYSTEM_INFO si;

                  GetSystemInfo(&si);

                  DWORD min = (DWORD) si.lpMinimumApplicationAddress;
                  DWORD max = (DWORD) si.lpMaximumApplicationAddress;

                  DWORD dwEndAddr;

                  bool read;

                  VirtualQuery((VOID *) ( (int) mbi.BaseAddress + mbi.RegionSize), &mbi, sizeof(MEMORY_BASIC_INFORMATION));
                  VirtualQuery((VOID *) ( (int) mbi.BaseAddress + mbi.RegionSize), &mbi, sizeof(MEMORY_BASIC_INFORMATION));

                  dwEndAddr = ( DWORD )mbi.BaseAddress + mbi.RegionSize - 1 - ( !Value ? 0 : Value * 2 );

                  cout << "Base: " << mbi.BaseAddress << endl;
                  cout << "End Address: " << dwEndAddr << endl;

                  DWORD i = (DWORD) mbi.BaseAddress;

                  if (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_EXECUTE_READWRITE)
                  {
                     do
                     {                        
                        read = ReadByte(min, Value);

                        if (read == true)
                        {
                           SendMessageX(ListBox1, LB_ADDSTRING, 0, (LPARAM) ConvertAddress((LPVOID) min).c_str());
                        }         

                        ++i;
                     } while (i <= dwEndAddr);
                  }                  

                  MessageBoxA(hWnd, "Done Scan, now adding the results!", "OK", MB_OK);               

                  GetResults();
               }
            }

lolllll you have completely misunderstood my code. My nScanType was an indication of what size to scan
Back to top
View user's profile Send private message
iPromise
Grandmaster Cheater
Reputation: -1

Joined: 27 Jun 2009
Posts: 529
Location: Canada

PostPosted: Sun Dec 27, 2009 7:05 pm    Post subject: Reply with quote

@Slugsnack, lol, well I found a sorta better solution:

Code:

case Button2_ID:
            {
               // declare the type and value variables
               char GetValue[500] = {0};
               char GetType[500] = {0};

               // get the value and type
               GetWindowTextA(TextBox1, (LPSTR) GetValue, MAX_PATH);
               GetWindowTextA(ComboBox1, (LPSTR) GetType, MAX_PATH);               

               if (!strcmp("Byte", GetType))
               {                  
                  bool Read;
                  SIZE_T Size;

                  MEMORY_BASIC_INFORMATION ProcessInformation = {0};
                  SYSTEM_INFO SystemInformation;
                  GetSystemInfo(&SystemInformation);                                 

                  
                  for (DWORD i = (DWORD) SystemInformation.lpMinimumApplicationAddress; i <= (DWORD) SystemInformation.lpMaximumApplicationAddress; i ++)
                  {
                     Size = VirtualQuery((LPCVOID) i, &ProcessInformation, sizeof(MEMORY_BASIC_INFORMATION));

                     if (ProcessInformation.State = PAGE_EXECUTE_READ || PAGE_READWRITE || PAGE_EXECUTE_READWRITE)
                     {
                        Read = ReadByte(i, 1);

                        if (Read == true)
                        {
                           cout << (LPVOID) i << endl;
                           SendMessageX(ListBox1, LB_ADDSTRING, 0, (LPARAM) ConvertAddress((LPVOID) i).c_str());
                        }
                     }
                  }
                  
               }
            }

            }

            break;


code still needs work.

EDIT

Found a better solution?

Code:

if (!strcmp("Byte", GetType))
               {   

                  bool Read;                  

                  MEMORY_BASIC_INFORMATION ProcessInformation = {0};
                  SYSTEM_INFO SystemInformation;
                  GetSystemInfo(&SystemInformation);                                 

                  
                  while (VirtualQuery((LPCVOID) SystemInformation.lpMinimumApplicationAddress, &ProcessInformation, sizeof(MEMORY_BASIC_INFORMATION)))
                  {
                     for (DWORD i = (DWORD) SystemInformation.lpMinimumApplicationAddress; i <= (DWORD) SystemInformation.lpMaximumApplicationAddress; i++)
                     {
                        if (ProcessInformation.Type == PAGE_EXECUTE_READ || PAGE_EXECUTE_READWRITE || PAGE_READWRITE)
                        {
                           Read = ReadByte(i, 1);

                           if (Read == true)
                           {
                              cout << "Address Found: " << (LPVOID) i << endl;
                           }
                        }
                     }
                  }
                  
               }


But my while loop stays in the loop after scanning. so dont know what to do because I depend on that loop for VirtualQuery.

Even Better

Code:
if (!strcmp("Byte", GetType))
               {   

                  bool Read;                  

                  MEMORY_BASIC_INFORMATION ProcessInformation = {0};
                  SYSTEM_INFO SystemInformation;
                  GetSystemInfo(&SystemInformation);                                 

                  
                  
                  for (DWORD i = (DWORD) SystemInformation.lpMinimumApplicationAddress; i <= (DWORD) 0x04000000; i++)
                  {
                     VirtualQuery((LPCVOID) SystemInformation.lpMinimumApplicationAddress, &ProcessInformation, sizeof(MEMORY_BASIC_INFORMATION));

                     if (ProcessInformation.Type == PAGE_EXECUTE_READ || PAGE_EXECUTE_READWRITE || PAGE_READWRITE)
                     {
                        Read = ReadByte(i, 1);

                        if (Read == true)
                        {
                           cout << "Address: " << (LPVOID) i << endl;
                           SendMessageX(ListBox1, LB_ADDSTRING, 0, (LPARAM) ConvertAddress((LPVOID) i).c_str());
                        }
                     }
                  }

                  GetResults();
                  
                  
               }
            }


Still is slow but better..
Back to top
View user's profile Send private message MSN Messenger
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Mon Dec 28, 2009 7:20 am    Post subject: Reply with quote

you use VirtualQuert completely wrong... thats why your scan is slow -.-
And the speed of the scan as well depends on the amount of results (how often SendMessageX is called)
Back to top
View user's profile Send private message
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Mon Dec 28, 2009 10:54 am    Post subject: Reply with quote

What does 'ReadByte' do? If it uses ReadProcessMemory to read a single byte, then there's your problem. It has been said many times in this thread: minimize the calls to windows api's.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Mon Dec 28, 2009 10:57 am    Post subject: Reply with quote

cout for every match will also slow you down a lot too. I would really suggest properly learning programming some time..
Back to top
View user's profile Send private message
iPromise
Grandmaster Cheater
Reputation: -1

Joined: 27 Jun 2009
Posts: 529
Location: Canada

PostPosted: Mon Dec 28, 2009 1:03 pm    Post subject: Reply with quote

@Anden100 I changed it up to this:

Code:

if (!strcmp(GetType, "Byte"))
               {
                  int Value = atoi(GetValue);

                  MessageBoxA(hWnd, "Scan Started!", "Scanning...", MB_OK);

                  bool Read;

                  MEMORY_BASIC_INFORMATION pInfo;
                  SYSTEM_INFO sInfo;

                  GetSystemInfo(&sInfo);

                  DWORD bAddress, sAddress;

                  bAddress = (DWORD) sInfo.lpMinimumApplicationAddress;

                  do
                  {
                     VirtualQuery((LPCVOID) bAddress, &pInfo, sizeof(MEMORY_BASIC_INFORMATION));

                     sAddress = ((DWORD) pInfo.BaseAddress + pInfo.RegionSize);                  

                     if ((pInfo.State = MEM_COMMIT) && (pInfo.Type = MEM_PRIVATE) && (pInfo.RegionSize > 0))
                     {
                        Read = ReadByte(bAddress, Value);

                        if (Read == true)
                        {
                           cout << "Address: " << (LPVOID) bAddress << endl;
                           SendMessageX(ListBox1, LB_ADDSTRING, 0, (LPARAM) ConvertAddress((LPVOID) bAddress).c_str());
                        }
                     }

                     ++bAddress;                     

                  } while (bAddress <= sAddress);

                  GetResults();
               }
            }


@tombana

ReadByte is my own custom function:

Code:

   bool ReadByte(DWORD Address, int Value)
   {
      DWORD Protect;

      if (!ProtectMemoryX((LPVOID) Address, 4, PAGE_EXECUTE_READWRITE, &Protect))
      {
         return false;
      }
      else
      {
         BYTE Read = *(LPBYTE) Address;

         int Return = (int) Read;

         if (Read == Value)
         {
            return true;
         }
         else
         {
            return false;
         }
      }
   }


@Slugsnack, not really helpful..
Back to top
View user's profile Send private message MSN Messenger
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Mon Dec 28, 2009 4:10 pm    Post subject: Reply with quote

iPromise wrote:
Code:

   bool ReadByte(DWORD Address, int Value)
   {
      DWORD Protect;

      if (!ProtectMemoryX((LPVOID) Address, 4, PAGE_EXECUTE_READWRITE, &Protect))
      {
         return false;
      }
      else
      {
         BYTE Read = *(LPBYTE) Address;

         int Return = (int) Read;

         if (Read == Value)
         {
            return true;
         }
         else
         {
            return false;
         }
      }
   }


@Slugsnack, not really helpful..

Ignoring what your 'ProtectMemoryX' call, you can simplify that to :
Code:
bool ReadByte(DWORD Address, int Value) {
  DWORD Protect;

  if ( ProtectMemoryX((LPVOID) Address, 4, PAGE_EXECUTE_READWRITE, &Protect) ) {
    BYTE Read = *(LPBYTE) Address;
    return Value == ( int ) Read;
  }

  return FALSE;
}


Whether your ProtectMemoryX is a copy of VirtualProtect or VirtualQuery, you're using it wrong as you've been told multiple times

Also.. lol @ comparing an int to a byte for a byte scan. Hmmmmm

http://home.att.net/~jackklein/c/inttypes.html#int

Finally, in your first 'code' snippet, you demonstrate completely incorrect use or comprehension of hungarian notation. Reading my last post would be really helpful.
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon Dec 28, 2009 5:32 pm    Post subject: Reply with quote

flooding the list box and console is going to be slow, you may want to think of a way to avoid that.


also instead of using atoi and the like why not a more elegant way?

Code:
template <typename T>
T convert(const std::string& str)
{
   T out;
   std::istringstream iss(str);
   iss >> out;
   return out;
}


just use an ostringstream for converting to a string.


templates as a whole here might work out quite nicely, actually.

you could probably save a lot of space / time since your functions are probably largely similar between data types.
Back to top
View user's profile Send private message
iPromise
Grandmaster Cheater
Reputation: -1

Joined: 27 Jun 2009
Posts: 529
Location: Canada

PostPosted: Mon Dec 28, 2009 5:43 pm    Post subject: Reply with quote

@Slugsnack

Code:

return FALSE;


Should be:

Code:

return false;


Because my function is bool not BOOL, you'll get a warning if you dont do that.

And when your:

Code:

return Value == ( int ) Read;


Value can be in any 8-bit range from 0 - 255 if you get 0 its considered false, and if you get 1 its considered true, and if you get above 1 you get a error.

ProtectMemoryX is a hookhop from the function VirtualProtect() which is locally hooked by Hacksheild and GameGuard, so if I were to use it on any game that has one of those anti-hacking systems installed, my memory scanner will get detected.

BTW

Am I using VirtualQuery correctly now?

After a lot of research I came up to something like this:

Code:

if (!strcmp(GetType, "Byte"))
               {
                  cout << GetValue << endl;
                  cout << GetType << endl;

                  bool Read;                  
                  int Value = atoi(GetValue);
                  MEMORY_BASIC_INFORMATION pInfo = {0};
                  SYSTEM_INFO sInfo;
                  GetSystemInfo(&sInfo);                  
                  SIZE_T s;
                  
                  int min = (int) sInfo.lpMinimumApplicationAddress;
                  int max = (int) 0x00FFFFFF;

                  MessageBoxA(0, ConvertAddress((LPVOID) (DWORD) min).c_str(), 0, MB_OK);

                  for (int i = min; i < max; i++)
                  {
                     s = VirtualQuery((LPCVOID) i, &pInfo, sizeof(pInfo));
                     
                     if ((s == sizeof(MEMORY_BASIC_INFORMATION)) && (pInfo.Type == MEM_PRIVATE) && (pInfo.State == MEM_COMMIT) && (pInfo.RegionSize > 0))
                     {                     
                        Read = ReadByte((DWORD) i, Value);
                           
                        if (Read == true)
                        {
                           cout << (LPVOID) i << endl;

                           SendMessageX(ListBox1, LB_ADDSTRING, 0, (LPARAM) ConvertAddress((LPVOID) i).c_str());
                        }                        
                     }                     
                  }

                  GetResults();
               }
            }


@slovach

That won't really matter (your typecasting method), atoi() is good enough. I decided not to send couts to the console, only to the listbox. I also just use the console for testing purposes.
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon Dec 28, 2009 5:53 pm    Post subject: Reply with quote

Neutral Neutral

how many versions of the code above do you have, one for every data type? i'm trying to nudge you into realizing that you could probably compact it down to "one"... or maybe inspire you a little at the very least.

if you're gonna write C++, it helps to use some of its features


Last edited by hcavolsdsadgadsg on Mon Dec 28, 2009 5:56 pm; edited 2 times in total
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites