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#] How to create an application to generate exe file?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Dr.Disrespect
Master Cheater
Reputation: 1

Joined: 17 Feb 2016
Posts: 483

PostPosted: Mon Jul 10, 2017 3:13 pm    Post subject: [C#] How to create an application to generate exe file? Reply with quote

I would like make an application that can do the following:

1. There are file A and file B, A is the newer version of B.

2. The application has a button. When you click the button, it reads some data from A and creates an exe file.

3. By clicking the exe file, which is created in the previous step, the data will be written into B. Therefore, B is updated after this step.

So, basically, the exe file acts like a patch for update purpose. But how to make such an application in C#?

Thanks in advance.

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 136

Joined: 25 Jan 2006
Posts: 7183
Location: 127.0.0.1

PostPosted: Tue Jul 11, 2017 1:29 am    Post subject: Reply with quote

Care to give info on what part of this you are having trouble with? You laid out steps that are basic for file patching, and all of which can be done easily with stuff built into the .NET framework to read and write to existing files.

For reading info from file A, I would suggest using a BinaryReader.

For writing info back into file B, I would suggest loading the original file into a byte[] array and patching the according data within that array as needed. Then save the data back to the file, or create a new one using any of the stream writing options that .NET offers. (Or just use File.WriteAllBytes)

Without more detail though, no one can really go into any more specifics than that.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
STN
I post too much
Reputation: 32

Joined: 09 Nov 2005
Posts: 2477

PostPosted: Tue Jul 11, 2017 4:28 am    Post subject: Reply with quote

Seems like a simple file reading/writing operation. I haven't used C# much to know which functions but you can google and easily get a list of all I/O functions C# has.
_________________
Cheat Requests/Tables- Fearless Cheat Engine
http://fearlessrevolution.com
Back to top
View user's profile Send private message
Dr.Disrespect
Master Cheater
Reputation: 1

Joined: 17 Feb 2016
Posts: 483

PostPosted: Tue Jul 11, 2017 10:22 pm    Post subject: Reply with quote

atom0s wrote:
Care to give info on what part of this you are having trouble with? You laid out steps that are basic for file patching, and all of which can be done easily with stuff built into the .NET framework to read and write to existing files.

For reading info from file A, I would suggest using a BinaryReader.

For writing info back into file B, I would suggest loading the original file into a byte[] array and patching the according data within that array as needed. Then save the data back to the file, or create a new one using any of the stream writing options that .NET offers. (Or just use File.WriteAllBytes)

Without more detail though, no one can really go into any more specifics than that.


@atom0s @STN

My problem is how to make an application than can generate an .exe file, which can read and write other files. Does that make sense?

I know how to make an application to read and write files, but how to make an application that can generate another .exe file, and that .exe file can perform IO functions?

Say,
I have made an application "A" in VS.
Then, the function of "A" is to generate an .exe file, called "B".
By using "B", an older version of a file can be updated.

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 136

Joined: 25 Jan 2006
Posts: 7183
Location: 127.0.0.1

PostPosted: Wed Jul 12, 2017 1:32 am    Post subject: Reply with quote

Your explanation is not that clear of what is going to be done so it is a bit rough to help you more specifically.

- Where is 'A' getting information from to create file 'B'?
- What is 'B' supposed to do when it is executed?

Care to show some examples of what you are trying to do and what information you are using to do this?

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Dr.Disrespect
Master Cheater
Reputation: 1

Joined: 17 Feb 2016
Posts: 483

PostPosted: Wed Jul 12, 2017 10:33 am    Post subject: Reply with quote

atom0s wrote:
Your explanation is not that clear of what is going to be done so it is a bit rough to help you more specifically.

- Where is 'A' getting information from to create file 'B'?
- What is 'B' supposed to do when it is executed?

Care to show some examples of what you are trying to do and what information you are using to do this?


There are 4 things that are relevant:
1. File one;
2. File two;
3. Application "A";
4. the .exe file, "B".

"File two" is an older version of "File one".
"A" extracts some bytes from "File one", and generates "B".
Then when "B" is executed, it will paste all the bytes into "File two" in the corresponding address.
The addresses are the same in "File one" and "File two".

I try to make a scenario editor for a game, it's a Japanese game. Some game data, such as NPC's stats or a value of an item, can be shared among different scenarios, I want the editor be able to extract relevant data and paste them to other scenarios. It's a convenient function for the users.

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 136

Joined: 25 Jan 2006
Posts: 7183
Location: 127.0.0.1

PostPosted: Wed Jul 12, 2017 11:23 am    Post subject: Reply with quote

The easiest way to do this, in my opinion, would be to make file 'B' a generic patcher. In this case, it would look for overlay data in itself and convert that information into a patch to be used with the end result. Entries could be appended in a fashion such as:

Code:
struct patchentry_t
{
    unsigned int   AddressOrFileOffset;
    unsigned int   PatchSize;
    unsigned char* PatchData;
};


File 'A' would collect the needed information to do this, then create a copy of file 'B'. It would then create the patch entries and append an overlay to the copy of file 'B' and then save it as file 'C'.

Doing this would allow file 'B' to be reused as a generic patcher that file 'A' uses to generate a patch file 'C'. It also allows you to easily create new patches and such without having to recode file 'B' every time since it will be using an overlay to do its work.

Another method would be to inject a resource into file 'B' that contains the patching information. However, altering a PE to inject a section is more work than just injecting an overlay which is literally just appending new data to the end of the file.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Dr.Disrespect
Master Cheater
Reputation: 1

Joined: 17 Feb 2016
Posts: 483

PostPosted: Sun Jul 16, 2017 1:09 pm    Post subject: Reply with quote

atom0s wrote:
The easiest way to do this, in my opinion, would be to make file 'B' a generic patcher. In this case, it would look for overlay data in itself and convert that information into a patch to be used with the end result. Entries could be appended in a fashion such as:

Code:
struct patchentry_t
{
    unsigned int   AddressOrFileOffset;
    unsigned int   PatchSize;
    unsigned char* PatchData;
};


File 'A' would collect the needed information to do this, then create a copy of file 'B'. It would then create the patch entries and append an overlay to the copy of file 'B' and then save it as file 'C'.

Doing this would allow file 'B' to be reused as a generic patcher that file 'A' uses to generate a patch file 'C'. It also allows you to easily create new patches and such without having to recode file 'B' every time since it will be using an overlay to do its work.

Another method would be to inject a resource into file 'B' that contains the patching information. However, altering a PE to inject a section is more work than just injecting an overlay which is literally just appending new data to the end of the file.


Thanks atom0s, and sorry for the late reply.
I don't quite understand your explanation. But it seems "patcher" is the key word, and I found this link based on it: https://www.codeproject.com/Articles/125721/Installer-and-Patching-Program-using-Visual-Studio

I am gonna read through it and try to figure out the problem.

Edit:
How to tell the generated .exe file (B) to do things? Like write to a file? I think this is the difficult part for me.

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 136

Joined: 25 Jan 2006
Posts: 7183
Location: 127.0.0.1

PostPosted: Mon Jul 17, 2017 1:51 am    Post subject: Reply with quote

In my example, you would have file (B) load and immediately look for overlay data in itself. If found, map out to the structure of your choice holding the information needed to do the work. Once the information is collected, you can then begin processing the file that it's supposed to target.

Load the target file that (B) is patching via something like:
Code:

var data = File.ReadAllBytes("Some\\Path\\To\\Target.exe");


Apply the needed patches to the data based on file offsets.
For example patching file offset 0x1234 with a nop would be:
Code:

data[0x1234] = 0x90;


Once done, use File.WriteAllBytes to save the data either back to the same file, or create a new patched file instead.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Dr.Disrespect
Master Cheater
Reputation: 1

Joined: 17 Feb 2016
Posts: 483

PostPosted: Wed Jul 19, 2017 8:44 am    Post subject: Reply with quote

atom0s wrote:
In my example, you would have file (B) load and immediately look for overlay data in itself. If found, map out to the structure of your choice holding the information needed to do the work. Once the information is collected, you can then begin processing the file that it's supposed to target.

Load the target file that (B) is patching via something like:
Code:

var data = File.ReadAllBytes("Some\\Path\\To\\Target.exe");


Apply the needed patches to the data based on file offsets.
For example patching file offset 0x1234 with a nop would be:
Code:

data[0x1234] = 0x90;


Once done, use File.WriteAllBytes to save the data either back to the same file, or create a new patched file instead.


Thanks for the answer and your patience, atom0s.

I think my problem is like this:
Scenario 1:
1. Using VS to generate an app "A".
In this scenario, I know how to "order" A to do things by coding, such as read, write file

Scenario 2:
1. Using VS to generate an app "A".
2. By executing "A", it generates a second .exe file, which is "B".
3. B is used for read and write data.
So, how to "order" B to do things? Such as read and write specific files.

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
Matze500
Expert Cheater
Reputation: 8

Joined: 25 Jan 2012
Posts: 241
Location: Germany

PostPosted: Wed Jul 19, 2017 2:43 pm    Post subject: Reply with quote

You could give the application the info via commandline arguments.

Greets Matze

_________________
Back to top
View user's profile Send private message
Dr.Disrespect
Master Cheater
Reputation: 1

Joined: 17 Feb 2016
Posts: 483

PostPosted: Wed Jul 19, 2017 7:49 pm    Post subject: Reply with quote

Matze500 wrote:
You could give the application the info via commandline arguments.

Greets Matze


Thanks for the reply.
Can you provide an example or a link of it?

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
Matze500
Expert Cheater
Reputation: 8

Joined: 25 Jan 2012
Posts: 241
Location: Germany

PostPosted: Wed Jul 19, 2017 8:45 pm    Post subject: Reply with quote

In C# and WPF it is string[] args = Environment.GetCommandLineArgs();
In C# and a command line app it is:
Code:
static int Main(string[] args)
{
}


It depends on the language and application type.

The program A can give the arguments to B like this:

Code:
ProcessStartInfo startInfo = new ProcessStartInfo("B.exe");
startInfo.WindowStyle = ProcessWindowStyle.Normal;
// Start with multiple arguments separated by spaces.
//  [0] = /file1
//  [1] = /file2
//  [2] = /filex
startInfo.Arguments = "/file1 /file2 /filex";
Process.Start(startInfo);


Greets Matze

_________________
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