What is wrong with my function?

This is a discussion on What is wrong with my function? within the C++ Programming forums, part of the General Programming Boards category; im making a small anti-cheat, and i want to check if functions are patched. Code: bool cAntiCheat::IsFuncValid( DWORD dwAddress, char* ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    What is wrong with my function?

    im making a small anti-cheat, and i want to check if functions are patched.

    Code:
    bool cAntiCheat::IsFuncValid( DWORD dwAddress, char* szMemMatch, int iSize )
    {
    	BYTE* szMemOrig;
    	DWORD dwOldProtection, dwDummy;
    	VirtualProtect( ( PVOID )dwAddress, iSize, PAGE_EXECUTE_READWRITE, &dwOldProtection );
    	memcpy( ( BYTE* )szMemOrig, ( BYTE* )dwAddress, iSize );
    	VirtualProtect( ( PVOID )dwAddress, iSize, dwOldProtection, &dwDummy );
    	if( !memcmp( ( BYTE* )szMemOrig, ( BYTE* )szMemMatch , iSize ))return true;
    	return false;
    }
    you use it like this:
    Code:
    			if( !cAC.IsFuncValid( 0x7428E6, "\xFF\x4E\x0C\x8B\x46\x08", 6 ) )
    				KICKTYPE = KICK_AMMO;
    but it always crashes (also by this point i should mention im making an anti-cheat without source code access ( hacky ) )

    thanks.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Posts
    30
    Quote Originally Posted by s0beit
    Code:
    	BYTE* szMemOrig;
    	memcpy( ( BYTE* )szMemOrig, ( BYTE* )dwAddress, iSize );
    szMemOrig is an uninitialized pointer, and then you try to copy something to the place it points to

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    It is best to initalize pointers to NULL if you do not directly assign them after declaration. This can help avoid dangling pointers
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    Code:
    bool cAntiCheat::IsFuncValid( PVOID dwAddress, char* szMemMatch, int iSize )
    {
    	char* szMemOrig = 0;
    	DWORD dwOldProtection, dwDummy;
    	VirtualProtect( dwAddress, iSize, PAGE_EXECUTE_READWRITE, &dwOldProtection );
    	memcpy( ( char* )szMemOrig, ( char* )dwAddress, iSize );
    	VirtualProtect( dwAddress, iSize, dwOldProtection, &dwDummy );
    	if( !memcmp( ( char* )szMemOrig, ( char* )szMemMatch , iSize ))return true;
    	return false;
    }
    still crashes, ive done stuff like this before and its never been a problem.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    30
    Quote Originally Posted by s0beit
    Code:
    bool cAntiCheat::IsFuncValid( PVOID dwAddress, char* szMemMatch, int iSize )
    {
    	char* szMemOrig = 0;
    	DWORD dwOldProtection, dwDummy;
    	VirtualProtect( dwAddress, iSize, PAGE_EXECUTE_READWRITE, &dwOldProtection );
    	memcpy( ( char* )szMemOrig, ( char* )dwAddress, iSize );
    	VirtualProtect( dwAddress, iSize, dwOldProtection, &dwDummy );
    	if( !memcmp( ( char* )szMemOrig, ( char* )szMemMatch , iSize ))return true;
    	return false;
    }
    still crashes, ive done stuff like this before and its never been a problem.
    you did not fix the problem: you need to allocate memory before you can copy things into it

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    Code:
    bool cAntiCheat::IsFuncValid( PVOID dwAddress, char* szMemMatch, int iSize )
    {
    	char* szMemOrig = ( char* )malloc( iSize );
    	DWORD dwOldProtection, dwDummy = 0x0;
    	VirtualProtect( dwAddress, iSize, PAGE_EXECUTE_READWRITE, &dwOldProtection );
    	memcpy( szMemOrig, dwAddress, iSize );
    	VirtualProtect( dwAddress, iSize, dwOldProtection, &dwDummy );
    	//if( !memcmp( ( char* )szMemOrig, ( char* )szMemMatch , iSize ))return true;
    	return true;
    }
    i still need to do the compare code, but it doesnt crash.

    thanks for the hints.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you should also free the memory after you finish with it...
    Otherwise you got a memory leak

    And because it is C++ I think using new/delete is better than malloc/free
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21