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++] Copying a file into memory

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

Joined: 20 Nov 2006
Posts: 658
Location: You lost the game.

PostPosted: Sun May 03, 2009 6:51 pm    Post subject: [C++] Copying a file into memory Reply with quote

I'm trying to read a file, and copy it into memory that I allocated with VirtualAlloc, but every time I call the ReadFile API my program just crashes. After adding a SEH, the call to GetLastError() told me that I had a "Invalid Window Handle," although I don't think that is related to ReadFile. Code is as follows:

Code:
   BOOL   bRead;
   HANDLE  hCEMFile;
   LPVOID  lpvCRCBase;
   LPDWORD lpBytesRead = 0;

   hCEMFile = CreateFile(
      "kMS.CEM",
      GENERIC_READ,
      FILE_SHARE_READ,
      NULL,
      OPEN_EXISTING,
      FILE_ATTRIBUTE_NORMAL,
      NULL
   );

   if( hCEMFile == INVALID_HANDLE_VALUE )
   {
      MessageBox( NULL, "Error opening CEM File! CRC Bypass not enabled!", "Warning!", MB_ICONWARNING | MB_OK );
      return;
   }

   lpvCRCBase = VirtualAlloc( NULL, 6291456, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
   if( lpvCRCBase == NULL )
   {
      MessageBox( NULL, "Error allocating memory! CRC Bypass not enabled!", "Warning!", MB_ICONWARNING | MB_OK );
      return;
   }

   bRead = ReadFile( hCEMFile, lpvCRCBase, 6291456, lpBytesRead, NULL ); // crash here
   if( !bRead || lpBytesRead != (LPDWORD)6291456 )
   {
      MessageBox( NULL, "Error reading CEM File! CRC Bypass not enabled!", "Warning!", MB_ICONWARNING | MB_OK );
      return;
   }
Back to top
View user's profile Send private message MSN Messenger
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Sun May 03, 2009 7:03 pm    Post subject: Reply with quote

The pointer is not initialized.

Change:

Code:
LPDWORD lpBytesRead = 0;


To either:

Code:
LPDWORD lpBytesRead = new DWORD;


or

Code:
DWORD lpBytesRead;
...
ReadFile(hCEMFile, lpvCRCBase, 6291456, &lpBytesRead, NULL);

_________________
Back to top
View user's profile Send private message
`unknown
Grandmaster Cheater
Reputation: 0

Joined: 20 Nov 2006
Posts: 658
Location: You lost the game.

PostPosted: Sun May 03, 2009 7:08 pm    Post subject: Reply with quote

Oh wow, that's what I get for using a data type I never messed around with before Embarassed.
Back to top
View user's profile Send private message MSN Messenger
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