I need to compile a C++ program from My C# application. In there I need to open the cl.exe when I press button. Can anyone give me some sample code.
            Asked
            
        
        
            Active
            
        
            Viewed 1,048 times
        
    2
            
            
        - 
                    1What have you tried? Executing cl.exe is no different to executing any other process... – Dean Harding Jun 15 '10 at 04:48
 - 
                    Sorry i couldn't get it. – Lucas Jun 15 '10 at 04:51
 - 
                    http://www.csharp-station.com/howto/processstart.aspx – Robert Harvey Jun 15 '10 at 04:57
 - 
                    cl.exe requires a different environment to run correctly. This is setup by using a batch file provided by Visual Studio. See http://stackoverflow.com/questions/84404/using-visual-studios-cl-from-a-normal-command-line for details. – Nathan Goings Aug 29 '13 at 04:19
 
3 Answers
3
            string filePath = "c:\program files\xyz\cl.exe";
System.Diagnostics.Process.Start(filePath);
        Binoj Antony
        
- 15,886
 - 25
 - 88
 - 96
 
- 
                    Hi, When i use this methode it will display the following error. how can i pass the pile name for CL.exe System.ComponentModel.Win32Exception was unhandled Message="The system cannot find the file specified" Source="System" ErrorCode=-2147467259 NativeErrorCode=2 StackTrace: – Lucas Jun 15 '10 at 05:23
 - 
                    
 - 
                    This is not the correct answer, cl.exe requires mspdb100.dll (and a few other items) to be in your %PATH%. I've outlined the real solution below. – Nathan Goings Aug 29 '13 at 04:18
 
1
            
            
        If you want to open some exe you can use:Process.Start("cl.exe");
        anishMarokey
        
- 11,279
 - 2
 - 34
 - 47
 
- 
                    Hi, When i use this methode it will display the following error. how can i pass the pile name for CL.exe System.ComponentModel.Win32Exception was unhandled Message="The system cannot find the file specified" Source="System" ErrorCode=-2147467259 NativeErrorCode=2 StackTrace – Lucas Jun 15 '10 at 05:23
 - 
                    1
 
0
            
            
        cl.exe requires DLL's that aren't in the %PATH%. You need to execute the correct batch for the architecture (x86 versus 64 bit). This will update your environment so you can launch cl.exe correctly.
x86 is located here (VS2010):
- C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat
 
I currently launch the command console, execute the batch, then launch the compile on my temp file.
string tempPath = Path.GetTempPath();
string tempName = "scrap_" + random.Next();
Process compiler = new Process();
compiler.StartInfo.FileName = "cmd.exe";
compiler.StartInfo.WorkingDirectory = tempPath;
compiler.StartInfo.RedirectStandardInput = true;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.StartInfo.UseShellExecute = false;
compiler.Start();
compiler.StandardInput.WriteLine("\"" + @"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat" + "\"");
compiler.StandardInput.WriteLine(@"cl.exe /nologo /EHsc " + tempName + ".cpp");
compiler.StandardInput.WriteLine(@"exit");
string output = compiler.StandardOutput.ReadToEnd();
compiler.WaitForExit();
Debug.Write(output);
compiler.Close();
I'm currently looking at launching vcvars32.bat directly, but I'm not sure if it updates the environment for my C# app correctly. I'll update you if I get it working.
Also, make sure you read and understand the accepted answer on this thread: ProcessStartInfo hanging on "WaitForExit"? Why?
        Community
        
- 1
 - 1
 
        Nathan Goings
        
- 1,145
 - 1
 - 15
 - 33