 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Sat Mar 14, 2009 10:13 am Post subject: Problem with memcpy(); & ZeroMemory(); |
|
|
| 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 |
|
 |
smartz993 I post too much
Reputation: 2
Joined: 20 Jun 2006 Posts: 2013 Location: USA
|
Posted: Sat Mar 14, 2009 10:16 am Post subject: |
|
|
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:
|
|
| Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Sat Mar 14, 2009 10:21 am Post subject: |
|
|
| 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 |
|
 |
Zand Master Cheater
Reputation: 0
Joined: 21 Jul 2006 Posts: 424
|
Posted: Sat Mar 14, 2009 10:31 am Post subject: Re: Problem with memcpy(); & ZeroMemory(); |
|
|
| 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 |
|
 |
smartz993 I post too much
Reputation: 2
Joined: 20 Jun 2006 Posts: 2013 Location: USA
|
Posted: Sat Mar 14, 2009 10:32 am Post subject: |
|
|
| 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 |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Sat Mar 14, 2009 10:34 am Post subject: |
|
|
| 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
|
|
| Back to top |
|
 |
smartz993 I post too much
Reputation: 2
Joined: 20 Jun 2006 Posts: 2013 Location: USA
|
Posted: Sat Mar 14, 2009 10:37 am Post subject: |
|
|
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 |
|
 |
Zerith Master Cheater
Reputation: 1
Joined: 07 Oct 2007 Posts: 468
|
Posted: Sat Mar 14, 2009 10:37 am Post subject: |
|
|
| I suggest you read a book on C/++ perhaps.
|
|
| Back to top |
|
 |
Zand Master Cheater
Reputation: 0
Joined: 21 Jul 2006 Posts: 424
|
Posted: Sat Mar 14, 2009 10:38 am Post subject: |
|
|
| memcpy(GodmodeBytes1, (LPVOID)0x913C10, 16);
|
|
| Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Sat Mar 14, 2009 10:39 am Post subject: |
|
|
Ok the problem was the memory space wasn't initialized yet :X
Edit: I think using VirtualQuery is better than IsBadReadPtr?
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sat Mar 14, 2009 1:33 pm Post subject: |
|
|
| 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 |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Sat Mar 14, 2009 1:46 pm Post subject: |
|
|
| Should I check if the memory is MEM_FREE?
|
|
| Back to top |
|
 |
smartz993 I post too much
Reputation: 2
Joined: 20 Jun 2006 Posts: 2013 Location: USA
|
Posted: Sat Mar 14, 2009 2:40 pm Post subject: |
|
|
| 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 |
|
 |
|
|
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
|
|