Click here to Skip to main content
15,896,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using following code to execute cpp files.

C#
public void ExecuteCommandSync(string command)
        {
            try
            {
                MessageBox.Show(command);
                                // create the ProcessStartInfo using "cmd" as the program to be run, and "/c " as the parameters.
                // Incidentally, /c tells cmd that we want it to execute the command that follows, and then exit.

                System.Diagnostics.ProcessStartInfo procStartInfo = new
                    System.Diagnostics.ProcessStartInfo("cmd", "/c" + command);
                // The following commands are needed to redirect the standard output. 
                ////This means that it will be redirected to the Process.StandardOutput StreamReader.
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.UseShellExecute = false;
                // Do not create the black window.
                procStartInfo.CreateNoWindow = true;
                // Now we create a process, assign its ProcessStartInfo and start it
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo = procStartInfo;
                proc.Start();
                // Get the output into a string
                string result = proc.StandardOutput.ReadToEnd();
                // Display the command output.
                //obj.listBox1.Items.Add(result);                
            }
            catch (Exception objException)
            {
                MessageBox.Show(objException.Message);


            }
        }

and i want to display the status of execution(success or compilation errors) to a listbox in the home page of the application. please help me in this regard.
Posted
Updated 9-Jan-12 18:20pm
v2
Comments
Sergey Alexandrovich Kryukov 10-Jan-12 0:32am    
Why? why?!
--SA
Sergey Alexandrovich Kryukov 10-Jan-12 0:33am    
What's the problem?
--SA

1 solution

You cannot "execute cpp files", you can only execute (some) executable files.

As far as I can understand, you are trying to compile.

One problem I can see is this: you should not use "cmd" "/c". You should use "command" only, with proper command line parameters including input file(s). Everything else depends on how you compile, what compiler do you use, do you build a project file or solution (which you could do with MSBuild.exe), etc.

Another problem is: redirecting of StandardOutput is not always enough. Generally, you also need to redirect StandardError; this is done in exact same way. I hope this will resolve your problem.

[EDIT #1]

You tried to re-post this question. Please don't do it — it cannot help you anyway. Instead, use the same page, in this case, this page: use "Improve question" above, comment on question/answer, reply to existing comments.

[EDIT #2]

In response to code sample of the OP's comment to this answer:

First of all, never ever use repeated string concatenation (+). Do you know that strings are immutable? In your case, use string.Format. In other cases (like loops), use System.Text.StringBuilder.

I have no idea what is ExecuteCommandSync. You can use the static method Process.Start(ProcessStartInfo) or the instance method Process.Start() with appropriate StartInfo assigned to the instance of Process. For synchronization, use the instance method Process.WaitForExit.

It's the best to do it in a separate thread and synchronized its output with your UI thread. Please see my past solutions on UI thread invocation:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

If is not clear: "unable to handle exceptional cases like errors in cpp file". There are no "exceptional cases like errors" in CPP files. All you have is build errors. Exceptions are not thrown. Errors are reported in text output which is either StandardOutput or StandardError. You need to redirect both. After all, run compilation command manually — you should get all the output in your redirected streams, check it up.

After all, see http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^] — but thoroughly.

I'm just curious: are you going to run linker as well?

Making an UI like Visual Studio is quite possible, I've done it.

[EDIT #3]

One more thing. I noticed you are hard-coding file path names. Hope this is only for experimental purposes during development.
There are no situations where a hard-coded path name can be useful. You should always calculate path names for files, get it from configuration files, etc.

—SA
 
Share this answer
 
v6
Comments
Abhinav S 10-Jan-12 0:48am    
My 5!

BTW, Congratulations for becoming an MVP.
Sergey Alexandrovich Kryukov 10-Jan-12 0:53am    
Thank you very much, Abhinav.
--SA
pasupulety 10-Jan-12 5:10am    
foreach (string file in UUT_list)
{

String filenamewithoutextn=Path.GetFileNameWithoutExtension(file);
String dest=BuildFolder_path_str+"\\"+filenamewithoutextn+".o";
String command1 = "c:\\DDC-I\\GCC\\powerpc_spe\\bin\\powerpc-eabispe-gcc.exe -c -g";
command1 +=" " + file+" ";
command1 += "-o " +"\""+dest+"\"";
//command1 += " " + IncludeFile_str;
ExecuteCommandSync(command1);
}

building the command using above code and passing it to
public void ExecuteCommandSync(string command) this funtion. then i am able to compile and generating "file.o" file but . i am unable to handle exceptional cases like errors in cpp file. Actually i am building an UI using c# to compile a bulk of cpp files
Sergey Alexandrovich Kryukov 10-Jan-12 10:57am    
(Sigh...) OK, please see the update.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900