2

I'm trying to create a C# form app that will allow me to use all of my previous C++ programs from one central program.

I'm able to open the exes with Process.Start(), however it does not compile the code correctly.

Example code:

Process.Start("C:\\\Documents and Settings\\\dan\\\Desktop\\\test.exe");

This will bring up the console and act like it's running, but it does not run like when I normally compile out of the C++ editor. Is there a startinfo variable I need to set to signify that it's a C++ program or something along that line?

Also, is there any way to execute a C++ program using process.start that will allow me to pass it variables through the command line via argc and argv?

Thanks

ctype.h
  • 1,470
  • 4
  • 20
  • 34
Dan
  • 35
  • 2
  • 6
  • What do you mean mean you say it doesn't run the same? What is different? – Kevin Crowell Mar 26 '10 at 20:23
  • 1
    @Dan: Why should your C++ test.exe know or care that it's written in C++ vs. any other language? What's the precise difference between what you see and what you expect? – Eric J. Mar 26 '10 at 20:24
  • It doesn't fully run, it just brings up the console, prints out the first couple prompt lines and halts, does not run all the way through to create the output. I tried this with several different C++ exes all giving the same result. – Dan Mar 26 '10 at 20:26
  • Is this behavior because you are not passing in any arguments? – Kevin Crowell Mar 26 '10 at 20:28
  • I got it to work, my issue was the working directory and shell execute. – Dan Mar 26 '10 at 20:36

5 Answers5

5

There are only a couple of differences when you use Process.Start the way you did vs. when you just execute the program directly. Both can be addressed by using ProcessStartInfo.

  1. The WorkingDirectory will not be the same. Set this to the path containing the executable to get the same behavior.
  2. Set UseShellExecute to true, so the windows shell is used to execute the process.

As for adding command line arguments: You can do that via ProcessStartInfo.Arguments. There shouldn't be one required due to it being a C++ application, however.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thanks, after changing the working directory and setting shell execute to true it worked correctly. Thanks again for the speedy response. – Dan Mar 26 '10 at 20:35
1

So far there's only enough info to answer your final question. Yes, you can include command line arguments as shown here. Look at the section titled "=== Program that runs EXE (C#) ==="

Eric J.
  • 147,927
  • 63
  • 340
  • 553
1

To add command line arguments:

Process process = new Process();
process.StartInfo.FileName = "C:\\Documents and Settings\\dan\\Desktop\\test.exe";
process.StartInfo.Arguments = ""; // Put your arguments here
process.Start();
Kevin Crowell
  • 10,082
  • 4
  • 35
  • 51
0

After the program has been compiled into an EXE, it shouldn't matter what language it was written in.

As for the program arguments, you need to take a look at the ProcessStartInfo class, and the override of Process.Start() that uses it: Process.Start(ProcessStartInfo)

jwismar
  • 12,164
  • 3
  • 32
  • 44
0

I faced a similar problem in python, are you expecting verbose output?

In my case the output buffer got full and hence execution stalled.

anijhaw
  • 8,954
  • 7
  • 35
  • 36