Click here to Skip to main content
15,073,911 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
i am using following code to compile cpp and c files from command prompt. but i am unable to differentiate the errors and warnings . i need abort the process when i get an error and want to continue when i get a warning.

[Actual requirement:- when we click on a button code should be run which can compile hundreds of c anc cpp files ]
public string ExecuteCommandSync(string command,string file)
       {
               MessageBox.Show(command);
               string strError=string.Empty;
               // 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.RedirectStandardError = 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();
               //MessageBox.Show("warning"+result);
               //if (result == "")
               //{
               //    result += command + "success";
               //    ce_obj.statusMsg(result);
               //}
               //strError += "standardoutput----->"+proc.StandardOutput.ReadToEnd()+"<\r\n";
               strError = proc.StandardError.ReadToEnd();
               //MessageBox.Show("error"+strError);
               return result;

       }





please suggest me whether is this code correct or not.
in this code i am unable to differentiate in between an error and warning while ccompiling.


please suggest me an alternate way of compiling.
Posted
Updated 21-Mar-12 5:10am
v2
Comments
Abhinav S 21-Mar-12 11:11am
   
Code tags added.
Sergey Alexandrovich Kryukov 21-Mar-12 15:46pm
   
I voted 4 for correct question and the code being developed in right direction (I mentioned some minor issues in my answer).
--SA

1 solution

Unfortunately, the errors end warnings are usually not differentiated in any way except the text of the issue. You would need to read line by line or parse the stream into lines and recognize the type of the issue report by a keyword like "Error" or "Warning" at a certain position in a line. For this purpose, you should analyze the output from a particular compiler/linker to understand its exact string format.

Some other method of such differentiation could be an error code. You can see documentation of a particular compiler/linker.

One problem you have is this: using "cmd" "/c". You never really need it. You should use immediately the command line of your application: full path name of executable file and required command line.

In practice, you usually need to run your code in a separate thread. As C or C++ compilers can work pretty slowly, you could even read the data in parallel and notify your, say, UI thread on the fly, before you read to the end. Please see my past answers on related topics:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^],
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
   
Comments
Shahin Khorshidnia 21-Mar-12 16:28pm
   
My +5
Sergey Alexandrovich Kryukov 21-Mar-12 16:31pm
   
Thank you, Shahin.
--SA
pasupulety 22-Mar-12 2:49am
   
instead of using "cmd" "/c" i have to change my current command prompt directory to the location where i specify.
how can i change the currect directory instead of my application path or c:/> . even if we give "cmd" "/c" it is taking our application path as default . i am unable to understand "cmd" "/c" means.
actually i need to change my currect directory to the path which i specify instead of application path or command path . please suggest me with sample code to change my currect path to path which i specify.


ex:- c:\workingdir\build>command
i need to get c:\workingdir\build> instead of c:\> orC:\c# applcation path........ etc.
   
No, you don't have to change it, but you can if your want. System.IO.Directory.SetCurrentDirectory.
Why? Just use the full path name of the program to run.
--SA
pasupulety 22-Mar-12 6:13am
   
thank U so much.
Sergey Alexandrovich Kryukov 22-Mar-12 15:58pm
   
You are welcome. If you agree that it makes sense, please accept the answer formally (green button) -- thanks.
--SA
pasupulety 28-Mar-12 9:51am
   
Thank U.
Sergey Alexandrovich Kryukov 28-Mar-12 21:24pm
   
My pleasure.
Good luck, call again.
--SA

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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