2

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.

Binoj Antony
  • 15,886
  • 25
  • 88
  • 96
Lucas
  • 167
  • 3
  • 15

3 Answers3

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
  • Put the full path of CL.exe, e.g. "c:\program files\xyz\cl.exe" – Binoj Antony Jun 15 '10 at 07:24
  • 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
    you need to mention the proper path of the exe. – anishMarokey Jun 15 '10 at 06:31
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