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 reading string from memory process
Goto page Previous  1, 2
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Dark Byte
Site Admin
Reputation: 475

Joined: 09 May 2003
Posts: 25979
Location: The netherlands

PostPosted: Tue Feb 28, 2012 1:47 pm    Post subject: Reply with quote

Are you looking for module data or allocated data?
If moduledata the the easiest is CreateToolhelp32Snapshot and module32first/module32next (search msdn)

If allocated data look into virtualquery

_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Shalvaid95
How do I cheat?
Reputation: 0

Joined: 26 Feb 2012
Posts: 9

PostPosted: Tue Feb 28, 2012 2:14 pm    Post subject: Reply with quote

I'm not completely sure about the differences between each one, but I think is the module data. Now I'm reading in msdn the api calls you told me.

For example, cheat engine starts always searching from x00000000? Or does it start from the base address of the process selected to make it faster?

I would like to do something like that, to improve the performance.
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 475

Joined: 09 May 2003
Posts: 25979
Location: The netherlands

PostPosted: Tue Feb 28, 2012 4:25 pm    Post subject: Reply with quote

Cheat Engine starts from 00000000, but it uses VirtualQueryEx to pinpoint memory regions that are unreadable and skip that

The main tip for performance is to never do RPM calls smaller than 4KB. Read first, scan later

_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Shalvaid95
How do I cheat?
Reputation: 0

Joined: 26 Feb 2012
Posts: 9

PostPosted: Tue Feb 28, 2012 5:43 pm    Post subject: Reply with quote

Well, you were right Dark Byte. Thank you so much... I know i suck
My loop took 5 or more minutes, now is taking seconds !!!!

I did something like you told me:

Code:
//////////////////////////////////////////////////////////////////
// ReadString from a certain Address and return as string type (4kb)
//////////////////////////////////////////////////////////////////
std::string ReadString(int Point)
{
   //char *value = (char*)malloc(Len+1) = {0};
   char value[4097] = {0}; //1KB (1024 BYTES) * 4 = 4KB (4096)
   SIZE_T numBytesRead;
   
   if (ReadProcessMemory(hProcess, (LPVOID)Point, value, 4096, &numBytesRead) == 0)
   {
      DWORD lastError = GetLastError();
      return "";
     // error - return an empty string
   }
   
   return std::string(value, numBytesRead);
}

//////////////////////////////////////////////////////////////////
// Search a Certain String in a process
//////////////////////////////////////////////////////////////////
int _stdcall SearchString(char *Text)
{
   size_t found;
   std::string Find;
   long int x;
   

   Find = std::string(Text,7);

   // 80000000 / 4096 = 19532,...
   for(x = 0; x < 19532; x++)
   {
      found=ReadString(x * 4096).find(Find);
      if (found!=std::string::npos )
      {
         return (x * 4096) + found; // Return the address we found                 
      }
   }

   return 0; //Error Return zero
}


What happens if my string gets in the middle of a search ? Hahaha I hope it never happens. Anyway is FAAASTEER !!! than my last code.

How I can search and do the same for int (4bytes) values (to search pointers). And also find and verify if it's the value which i'm looking for.
I guess in arrays, but i'm not sure.

Thank you booss!
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Tue Feb 28, 2012 9:14 pm    Post subject: Reply with quote

Here's an example using Unicode strings since its the only thing on my laptop I could think to use to test this with lol:

Code:


#include <Windows.h>
#include <iostream>
#include <string>

DWORD_PTR _scan_string( HANDLE hProcHandle, const wchar_t* pwszInput )
{
    if (hProcHandle == INVALID_HANDLE_VALUE)
        return -1;

    MEMORY_BASIC_INFORMATION mbi = { 0 };
    LPVOID lpStartAddress = NULL;

    SYSTEM_INFO sysInfo = { 0 };
    GetSystemInfo( &sysInfo );

    lpStartAddress = sysInfo.lpMinimumApplicationAddress;

    while( VirtualQueryEx( hProcHandle, lpStartAddress, &mbi, sizeof( mbi ) ) )
    {
        if ((mbi.State & MEM_COMMIT) && (mbi.Protect & ( PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY )))
        {
            std::cout << "region: " << std::hex << mbi.BaseAddress << std::endl;

            unsigned char szBuffer[ 4096 ] = { 0 };
            ReadProcessMemory( hProcHandle, mbi.BaseAddress, &szBuffer, 4096, NULL );

            for( int x = 0; x < 4096; x++ )
            {
                if (memcmp( (LPVOID)&szBuffer[ x ], pwszInput, wcslen( pwszInput ) ) == 0)
                    return ( ( DWORD_PTR )mbi.BaseAddress + x );
            }
        }

        lpStartAddress = ( LPVOID )( ( DWORD_PTR )( lpStartAddress ) + mbi.RegionSize );
    }

    return 0;
}


int __cdecl main( int argc, TCHAR* argv[] )
{
    HWND hWndWindow = FindWindow( NULL, L"Minesweeper" );
    if (hWndWindow == NULL)
        return ERROR_INVALID_HANDLE;

    DWORD dwProcessId = 0;
    GetWindowThreadProcessId( hWndWindow, &dwProcessId );

    HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION|PROCESS_VM_OPERATION|PROCESS_VM_READ, FALSE, dwProcessId );
    if (hProcess == NULL)
        return ERROR_INVALID_HANDLE;

    int nTest = _scan_string( hProcess, L"Minesweeper" );
    std::cout << "Scan results: " << nTest;

    CloseHandle( hProcess );
    return ERROR_SUCCESS;
}


For original Windows XP Minesweeper.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
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
Page 2 of 2

 
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