Juansdk
Juansdk

Reputation: 93

Implementing a system where my program can build an exe file in c++

How would I implement a builder where my program can produce a .exe output. For example, I want my program to ask the user for their name, and once they input this data, it will generate an exe. Once they open that exe, it will print their name. Please do not not ask why this is needed. I just need help on how I would implement such a task. Would I need an internal compiler? All this will be done in c++.

Things to note: I do not want to output a .cpp then compile it.

Upvotes: 1

Views: 476

Answers (6)

dxiv
dxiv

Reputation: 17678

The .exe reference suggests Windows. In that case, one way to do it is store a "template" executable image as a resource in the master program, then at runtime write the custom data (such as user name) into string table(s) in that image and save it to disk.

  • Create an applet to use as a template e.g. an empty console app int main() { return 0; }. Add a string table resource to the template app. Reference: How to: Create Resources (C++).

  • Build the template app as an .exe, and include the .exe as a binary resource into the master program. Write code into the master program to extract the binary resource and save it to disk as an executable. Reference: Embed .exe file into C++ program?

  • Add code into the master program to update the string table in the template .exe with the new string(s). Reference: updating a string table with UpdateResource.

The above covers the steps to embed the desired string(s) into the .exe applet. To actually use the string(s) in the applet, see for example C++ win32 loading strings from resource.

Upvotes: 0

kdcode
kdcode

Reputation: 522

Assuming you only need to change 'data' of your program and build another exe, you can append data to your exe and access it from code. this way you can change the data inside the exe as you want and produce a new executable. See this question.

Upvotes: 0

user14331070
user14331070

Reputation:

I thought something very easy, you can just create a program which operate on cmd line, and which anytimes it need create or rewrite a new file where can get the name, if you just need something easy

Upvotes: 0

thakee nathees
thakee nathees

Reputation: 965

I remember I've done something like this before trying to make a small compiler. Use TCC https://bellard.org/tcc/ it's a tiny c compiler, and it's extremely small. it can generate elf, windows pe for x86. you need to provide a c source string and it'll generate an executable.

/* compile a string containing a C source. Return -1 if error. */
LIBTCCAPI int tcc_compile_string(TCCState *s, const char *buf);
/* output an executable, library or object file. DO NOT call
   tcc_relocate() before. */
LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename);

see how to use libtcc at tcc/win32/tcc-win32.txt and examples at tcc/test/libtcc_test.c

Upvotes: 0

Phil1970
Phil1970

Reputation: 2624

You could create a shortcut (.lnk) that would pass the name as an argument to the EXE. For the user, it probably does not matter much if he click on a LNK instead of an EXE.

An alternative would be to create a file with a .displayName extension and then register it for double click.

I am not sure but you could probably generated a program with a section for constant data and then modify that section. Also, using a ressource section might possibly works.

Obviously writing a compiler is far from easy especially for a language like C++ that is among the harder to parse correctly.

  1. If you want to do that for learning purpose then read books on that subject.
  2. If you really want a program that double click on it display your name, then reconsider the way to do it.

And not really explaining the purpose do make your question a bad question as one could easily create malicious software!

Finally such a quesion is beyond the scope of a forum like this. Creating a compiler is a subject for a 1000+ pages book. Nothing less.

Upvotes: 2

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275976

Learn the format of executables on the platforms you want to support. Make an executable that matched that and prints that name.

Write that binary file and mark it as executable.

Given how simple your program is, it shouldn't be long. You can futz with how the bytes would work by compiling a program in C++ manually. I advise using printf instead of std cout.

Upvotes: 2

Related Questions