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 


Problem with memcpy(); & ZeroMemory();

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Sat Mar 14, 2009 10:13 am    Post subject: Problem with memcpy(); & ZeroMemory(); Reply with quote

Code:

#include "windows.h"
#include "conio.h"

BYTE GodmodeBytes1[16];
BYTE GodmodeBytes2[16];
BYTE GodmodeBytes3[16];

__checkReturn bool getCleanMem( void ){
   __try{
      memcpy( &GodmodeBytes1, (void*)0x913C10, sizeof(sizeof(BYTE)*16));
      memcpy( &GodmodeBytes2, (void*)0x913C80, sizeof(sizeof(BYTE)*16));
      memcpy( &GodmodeBytes3, (void*)0x913EE0, sizeof(sizeof(BYTE)*16));
      return true;
   } __except(EXCEPTION_EXECUTE_HANDLER){
      return false;
   }
}

__checkReturn __inline bool canSleep( __in int t = 10 ){
   __try{
      Sleep( t );
      return true;
   } __except(EXCEPTION_EXECUTE_HANDLER){
      return false;
   }
}

__checkReturn bool setHacks( __in int i, __in bool on ){
   //1 = Godmode
   __try{
      switch(i){

         case 1:
            if(on){
               ZeroMemory( (void*)0x913C10, sizeof(sizeof(BYTE)*16) );
               ZeroMemory( (void*)0x913C80, sizeof(sizeof(BYTE)*16) );
               ZeroMemory( (void*)0x913EE0, sizeof(sizeof(BYTE)*16) );
            } else {
               memset( (void*)0x913C10, (int)GodmodeBytes1, sizeof(sizeof(BYTE)*16) );
               memset( (void*)0x913C80, (int)GodmodeBytes2, sizeof(sizeof(BYTE)*16) );
               memset( (void*)0x913EE0, (int)GodmodeBytes3, sizeof(sizeof(BYTE)*16) );
            }
            break;

         default:
            break;
      }
      return true;
   }__except(EXCEPTION_EXECUTE_HANDLER){
      return false;
   }
}

int MainT( void ){
   if(!AllocConsole())
      return MessageBox( NULL, TEXT("Could not allocate console!"), TEXT("Error"), MB_ICONERROR );
   
   HANDLE hConsole = GetStdHandle( STD_INPUT_HANDLE );
   BOOL bExit = TRUE, bGodmode = FALSE;
   char buffer[MAX_PATH] = "";
   size_t nBuffer;

   SetConsoleTextAttribute( hConsole, FOREGROUND_BLUE );
   _cprintf_s( "Welcome to Mikey's Gunbound Trainer!\n" );
        canSleep( 5000 );
   while(bExit && canSleep()){
      __try{
         system("cls");
         _cprintf_s("Enter command -: ");
         ZeroMemory( &buffer, sizeof(buffer) );
         _cgets_s( buffer, sizeof(buffer), &nBuffer );

         if( strcmp( buffer, "exit" ) == 0 ){
            bExit = false;
         } else if( strcmp( buffer, "godmode" ) == 0 ){
            bGodmode ^= 1;
            if( !setHacks( 1, bGodmode ) ){
               return MessageBox( NULL, TEXT("There was a unhandled exception! Exiting!"), TEXT("Error"), MB_ICONERROR );
            }
         }
      } __except(EXCEPTION_EXECUTE_HANDLER){
         return MessageBox( NULL, TEXT("There was a unhandled exception! Exiting!"), TEXT("Error"), MB_ICONERROR );
      }
   }

   return 0;
}

int Initialize( __in_opt HMODULE hModule ){
   UNREFERENCED_PARAMETER(hModule);
   __try{
      if( !getCleanMem() )
         return MessageBox( NULL, TEXT("Could not get clean memory!"), TEXT("Error"), MB_ICONERROR );
      CreateThread( NULL, NULL, (LPTHREAD_START_ROUTINE)&MainT, NULL, NULL, NULL );
   }__except(EXCEPTION_EXECUTE_HANDLER){
      return MessageBox( NULL, TEXT("There was a unhandled exception! Exiting!"), TEXT("Error"), MB_ICONERROR );
   }
   return 0;
}

BOOL APIENTRY DllMain(__in HMODULE hModule, __in DWORD fdwReason, __in_opt LPVOID lpReserved)
{
   UNREFERENCED_PARAMETER(lpReserved);

   if( fdwReason == DLL_PROCESS_ATTACH )
   {
      Initialize( hModule );
   } else if( fdwReason == DLL_PROCESS_DETACH ){
      FreeConsole();
   }
   return TRUE;
}


I get an exception with memcpy(); I seem I can't copy memory bigger than 4 bytes? Is there a substitute for this problem?

P.S ZeroMemory(); does the same also.
Back to top
View user's profile Send private message
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Sat Mar 14, 2009 10:16 am    Post subject: Reply with quote

Take out the & before GodmodeBytes1,2,3.


On a side note:

Why are you multiplying it by the sizeof a byte, if its 1 ?
And if it's not, then your buffers aren't big enough.

Also..why are you using sizeof() within itself ? lol if you really want to do that, just make it like this:
Code:
sizeof(BYTE)*16
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Sat Mar 14, 2009 10:21 am    Post subject: Reply with quote

smartz993 wrote:
Take out the & before GodmodeBytes1,2,3.


On a side note:

Why are you multiplying it by the sizeof a byte, if its 1 ?
And if it's not, then your buffers aren't big enough.


Then I get a "cannot convert from BYTE to void*"... I like to use sizeof(should I typecast, perhaps?), because it's a habit. I also like to be sure.
Back to top
View user's profile Send private message
Zand
Master Cheater
Reputation: 0

Joined: 21 Jul 2006
Posts: 424

PostPosted: Sat Mar 14, 2009 10:31 am    Post subject: Re: Problem with memcpy(); & ZeroMemory(); Reply with quote

S3NS4 wrote:
Code:

#include "windows.h"
#include "conio.h"

BYTE GodmodeBytes1[16];
BYTE GodmodeBytes2[16];
BYTE GodmodeBytes3[16];

__checkReturn bool getCleanMem( void ){
   __try{
      memcpy( &GodmodeBytes1, (void*)0x913C10, sizeof(sizeof(BYTE)*16));
      memcpy( &GodmodeBytes2, (void*)0x913C80, sizeof(sizeof(BYTE)*16));
      memcpy( &GodmodeBytes3, (void*)0x913EE0, sizeof(sizeof(BYTE)*16));
      return true;
   } __except(EXCEPTION_EXECUTE_HANDLER){
      return false;
   }
}

__checkReturn __inline bool canSleep( __in int t = 10 ){
   __try{
      Sleep( t );
      return true;
   } __except(EXCEPTION_EXECUTE_HANDLER){
      return false;
   }
}

__checkReturn bool setHacks( __in int i, __in bool on ){
   //1 = Godmode
   __try{
      switch(i){

         case 1:
            if(on){
               ZeroMemory( (void*)0x913C10, sizeof(sizeof(BYTE)*16) );
               ZeroMemory( (void*)0x913C80, sizeof(sizeof(BYTE)*16) );
               ZeroMemory( (void*)0x913EE0, sizeof(sizeof(BYTE)*16) );
            } else {
               memset( (void*)0x913C10, (int)GodmodeBytes1, sizeof(sizeof(BYTE)*16) );
               memset( (void*)0x913C80, (int)GodmodeBytes2, sizeof(sizeof(BYTE)*16) );
               memset( (void*)0x913EE0, (int)GodmodeBytes3, sizeof(sizeof(BYTE)*16) );
            }
            break;

         default:
            break;
      }
      return true;
   }__except(EXCEPTION_EXECUTE_HANDLER){
      return false;
   }
}

int MainT( void ){
   if(!AllocConsole())
      return MessageBox( NULL, TEXT("Could not allocate console!"), TEXT("Error"), MB_ICONERROR );
   
   HANDLE hConsole = GetStdHandle( STD_INPUT_HANDLE );
   BOOL bExit = TRUE, bGodmode = FALSE;
   char buffer[MAX_PATH] = "";
   size_t nBuffer;

   SetConsoleTextAttribute( hConsole, FOREGROUND_BLUE );
   _cprintf_s( "Welcome to Mikey's Gunbound Trainer!\n" );
        canSleep( 5000 );
   while(bExit && canSleep()){
      __try{
         system("cls");
         _cprintf_s("Enter command -: ");
         ZeroMemory( &buffer, sizeof(buffer) );
         _cgets_s( buffer, sizeof(buffer), &nBuffer );

         if( strcmp( buffer, "exit" ) == 0 ){
            bExit = false;
         } else if( strcmp( buffer, "godmode" ) == 0 ){
            bGodmode ^= 1;
            if( !setHacks( 1, bGodmode ) ){
               return MessageBox( NULL, TEXT("There was a unhandled exception! Exiting!"), TEXT("Error"), MB_ICONERROR );
            }
         }
      } __except(EXCEPTION_EXECUTE_HANDLER){
         return MessageBox( NULL, TEXT("There was a unhandled exception! Exiting!"), TEXT("Error"), MB_ICONERROR );
      }
   }

   return 0;
}

int Initialize( __in_opt HMODULE hModule ){
   UNREFERENCED_PARAMETER(hModule);
   __try{
      if( !getCleanMem() )
         return MessageBox( NULL, TEXT("Could not get clean memory!"), TEXT("Error"), MB_ICONERROR );
      CreateThread( NULL, NULL, (LPTHREAD_START_ROUTINE)&MainT, NULL, NULL, NULL );
   }__except(EXCEPTION_EXECUTE_HANDLER){
      return MessageBox( NULL, TEXT("There was a unhandled exception! Exiting!"), TEXT("Error"), MB_ICONERROR );
   }
   return 0;
}

BOOL APIENTRY DllMain(__in HMODULE hModule, __in DWORD fdwReason, __in_opt LPVOID lpReserved)
{
   UNREFERENCED_PARAMETER(lpReserved);

   if( fdwReason == DLL_PROCESS_ATTACH )
   {
      Initialize( hModule );
   } else if( fdwReason == DLL_PROCESS_DETACH ){
      FreeConsole();
   }
   return TRUE;
}


I get an exception with memcpy(); I seem I can't copy memory bigger than 4 bytes? Is there a substitute for this problem?

P.S ZeroMemory(); does the same also.


Looks like you picked up random coding techniques/habits without actually understanding the need.

I'm not sure how &GodmodeBytes1 even compiled - remove the &. If you're going to be doing that (sizeof(sizeof()) etc, use _countof.
Back to top
View user's profile Send private message
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Sat Mar 14, 2009 10:32 am    Post subject: Reply with quote

S3NS4 wrote:
smartz993 wrote:
Take out the & before GodmodeBytes1,2,3.


On a side note:

Why are you multiplying it by the sizeof a byte, if its 1 ?
And if it's not, then your buffers aren't big enough.


Then I get a "cannot convert from BYTE to void*"... I like to use sizeof(should I typecast, perhaps?), because it's a habit. I also like to be sure.


So cast it as an void* or LPVOID.

You don't need to cast the size..its just a number. sizeof() is a function..just do this:

Code:
memcpy((void*)buffer,(void*)Address,16);
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Sat Mar 14, 2009 10:34 am    Post subject: Reply with quote

smartz993 wrote:
S3NS4 wrote:
smartz993 wrote:
Take out the & before GodmodeBytes1,2,3.


On a side note:

Why are you multiplying it by the sizeof a byte, if its 1 ?
And if it's not, then your buffers aren't big enough.


Then I get a "cannot convert from BYTE to void*"... I like to use sizeof(should I typecast, perhaps?), because it's a habit. I also like to be sure.


So cast it as an void* or LPVOID.

You don't need to cast the size..its just a number. sizeof() is a function..just do this:

Code:
memcpy((void*)buffer,(void*)Address,16);


memcpy( (void*)GodmodeBytes1, (void*)0x913C10, 16);

Still doesn't work Neutral
Back to top
View user's profile Send private message
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Sat Mar 14, 2009 10:37 am    Post subject: Reply with quote

What's the problem ?

You made a buffer, an array.

When arrays are defined the variable is a pointer, like an address.

You don't need the ampersand, so i don't know what's wrong. Why don't you try IsBadReadPtr on that address before copying from it?
(or VirtualQuery, that's a preferred method.)
Back to top
View user's profile Send private message
Zerith
Master Cheater
Reputation: 1

Joined: 07 Oct 2007
Posts: 468

PostPosted: Sat Mar 14, 2009 10:37 am    Post subject: Reply with quote

I suggest you read a book on C/++ perhaps.
Back to top
View user's profile Send private message MSN Messenger
Zand
Master Cheater
Reputation: 0

Joined: 21 Jul 2006
Posts: 424

PostPosted: Sat Mar 14, 2009 10:38 am    Post subject: Reply with quote

memcpy(GodmodeBytes1, (LPVOID)0x913C10, 16);
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Sat Mar 14, 2009 10:39 am    Post subject: Reply with quote

Ok the problem was the memory space wasn't initialized yet :X

Edit: I think using VirtualQuery is better than IsBadReadPtr?
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Sat Mar 14, 2009 1:33 pm    Post subject: Reply with quote

S3NS4 wrote:
Edit: I think using VirtualQuery is better than IsBadReadPtr?


Yes, VirtualQuery is much safer then using IsBadReadPtr as it is obsolete.

MSDN wrote:
Important This function is obsolete and should not be used. Despite its name, it does not guarantee that the pointer is valid or that the memory pointed to is safe to use. For more information, see Remarks on this page.

_________________
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Sat Mar 14, 2009 1:46 pm    Post subject: Reply with quote

Should I check if the memory is MEM_FREE?
Back to top
View user's profile Send private message
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Sat Mar 14, 2009 2:40 pm    Post subject: Reply with quote

Code:
int __stdcall __check_invalid_addr_range(const void *addr, size_t len)
{
  if (addr == NULL || len == 0)
    return EINVAL;

  MEMORY_BASIC_INFORMATION mbuf;
  const void *end;

  for (end = (char *)addr + len, addr = (void *)((DWORD)addr % getpagesize());
       addr < end;
       addr = (char *)addr + mbuf.RegionSize)
    {
      if (!VirtualQuery (addr, &mbuf, sizeof (mbuf))
          || mbuf.State != MEM_COMMIT
          || mbuf.AllocationProtect & (PAGE_NOACCESS|PAGE_GUARD))
        return EINVAL;
    }

  return 0;
}


From some cygwin site.
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
Page 1 of 1

 
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