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 


LoadBinary C++ Equivalent

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General Gamehacking
View previous topic :: View next topic  
Author Message
Jugyou
Newbie cheater
Reputation: 0

Joined: 18 May 2014
Posts: 11

PostPosted: Fri Aug 01, 2014 11:56 am    Post subject: LoadBinary C++ Equivalent Reply with quote

I'm currently converting my codes to C++, the Inline Assembler is a little awkward, but it's working quite well so far.

However I'm having trouble converting this code:
Code:
globalalloc(newmem,2048)
loadbinary(newmem,file.bin)

I've looked here (viewtopic.php?p=4687001), through a dozen stackoverflow questions and other sites, but to no avail.

Basically I want a function that allocates memory, loads the binary file and returns the address where the file is stored.

How can I achieve this in C++?
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Fri Aug 01, 2014 12:01 pm    Post subject: Reply with quote

There are a few different ways to go about this. The choice of how you want to do it is up to you.

To start, you will want to read in the file you are wanting to write to memory and obtain its size so you do not hard-code a file size that might chance based on the file being injected.

You can do this with:
- CreateFile
- ReadFile
- GetFileSize

Or you can use the file functions like:
- fopen
- fread
- fseek
- ftell

Once you have the file size, you can allocate a buffer locally to read into.
Afterward, you can then work with the memory remotely via:
- VirtualAllocEx (to allocate the memory block in the remote process.)
- WriteProcessMemory (to write your data from the file to the allocated memory.)

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Jugyou
Newbie cheater
Reputation: 0

Joined: 18 May 2014
Posts: 11

PostPosted: Fri Aug 01, 2014 2:52 pm    Post subject: Reply with quote

Thank you so much for the fast and extremely useful answer!

I came up with the following
Code:
#include <stdio.h>
#include <iostream>

LPVOID LoadBinary(char* filePath)
{
   FILE *file = fopen(filePath, "rb");
   
   long fileStart = ftell(file);

   fseek(file, 0, SEEK_END);
   
   long fileSize = ftell(file);

   fseek(file, fileStart, 0); //Back to the start

   BYTE *fileBuffer = new BYTE[fileSize];

   fread(fileBuffer, fileSize, 1, file);
   
   LPVOID newmem = VirtualAlloc(NULL, fileSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);

   unsigned long OldProtection;

   VirtualProtect(newmem, fileSize, PAGE_EXECUTE_READWRITE, &OldProtection);

   memcpy(newmem, fileBuffer, fileSize);

   VirtualProtect(newmem, fileSize, OldProtection, NULL);

   delete[]fileBuffer;
   
   fclose(file);

   return newmem;
}

And you can use it like this

Code:
LPVOID newmem = LoadBinary("C:\\file.bin");
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Fri Aug 01, 2014 7:52 pm    Post subject: Reply with quote

Keep in mind when you do that you are leaving a block of memory allocated, so be sure to clean it up when you are done with it! Smile
_________________
- 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 Gamehacking 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