GTF
GTF

Reputation: 27

Embed one exe into another in order to build a single exe?

I have a Winforms project and a console project.
I was starting the console app from the Winforms app as an external process, but would like to compile both into a single exe, if possible.

I have brought them both into a single VS 2015 solution. Should I be able to accomplish this by converting the console app to a library?

Aside from all the reference, dependency and linker settings/paths, how would I pass the command line arguments to, and start the console app from the Winforms code. i.e. jump to the entry point of the console app? The solution builds OK so far.

Upvotes: 2

Views: 518

Answers (1)

Todd Christensen
Todd Christensen

Reputation: 1327

Yes, a library is the right approach. If you want one exe, you'll want to link it as a static library.

After converting the project to a library, change the "main" function to use a different name, I will use "foobar" for example.

If your Winforms project is C++ as well, next create an h file that has the definition for the "foobar" function, e.g.:

int foobar(int argc, char *argv[]);

This will be your library's external interface. Simply include it, and then you can call it:

const char *argv[] = {"progname", "arg1", "arg2"};
int result = foobar(3, argv);

Of course, you can also change this function to take more convenient arguments. Remember that, by default, argv[0] is the program name and is probably ignored.

If you're using C# or otherwise can't include the .h directly, I'm not sure if this is possible. See here:

How to compile C# application with C++ static library?

Upvotes: 1

Related Questions