Nate
Nate

Reputation: 65

One Solution, two projects: how to call a console project from a windows application? C++

Using Visual Studio 2010, coding in C++:

So I've got a solution and two projects: Project 1 is a Windows application meant to act as the GUI for the program, while Project 2 is a console application meant to interact with some external applications.

I want to create a button in Project 1 that when pressed executes Project 2. What's the simplest way to do this? I'm thinking of just running Project 2 through the Command prompt with a "system("Project2.exe");" kind of approach, but I don't know how to reference a project executable instead of an external, already existing executable.

I'm pretty new to C++ and Visual Studio in general so I could be missing something obvious here, sorry. Thanks for your help!

Upvotes: 1

Views: 3971

Answers (4)

BigBoss
BigBoss

Reputation: 6914

If you want to run another program(even your other project or some unrelated executive that you get from your last traveling to moon) you should now the path of other project in either absolute or relative form or it should be in the path.

So in your system that you know the path, you can hard-code it in your source file, for example system( "C:\\path\\to\\my\\application.exe" ) or system( "..\\project2\\output.exe").

And in another system you have 3 options:

  • Put your project2.exe in the PATH by either adding its path to the system PATH or copy it into a folder that is already in the path like system folder

  • By using a setup copy it into a predefined folder( usually relative to project1.exe ), for example in the same folder or ..\\server\\project2.exe

  • Create a config file that user can put the path of executable of project2.exe in it

Upvotes: 2

Aniket Inge
Aniket Inge

Reputation: 25705

Other than as mentioned by Mike Corcoran, you can also use any external program if you put it into the system's PATH variable and then have it executed by system(const char *) function

Driving it this way is passable, but its not the correct way. For example, if your program was interactive it would fail instantly or have an UB(undefined behavior). To avoid this, separate the logic of the program from the input/output and work your way around it in your code.

Some successful Linux programs have managed to get the output of the console application and display it on the GUI(having you to interact with it internally). Even Visual Studio does this - the output you see when you compile your applications such as "successful build" etc etc is executed in the command line and then the output of it is redirected.

Good luck.

Upvotes: 0

user238801
user238801

Reputation:

I think what Mike said is the better way but I guess you'll encounter the same problem here. You have to define "Project Dependencies". Right click on project 1 -> Project Dependencies -> Select project 2. Now Project 2 gets compiled before project 1. (You have to do the same for libs if you decide to go that way).

If you want to move a file after compiling it you can define a post-build event in your properties. (Configuration Properties -> Build Events -> Post-Build Event) There you can copy a file by defining for example a command like xcopy /y "$(ProjectDir)Release/myexe.exe" "Some path"

Upvotes: 0

Mike Corcoran
Mike Corcoran

Reputation: 14565

can you change project 2 to build as a class library, so you can just use that dll in your project 1?

Upvotes: 1

Related Questions