2010-06-15 60 views
2

我需要从My C#应用程序编译C++程序。在我按下按钮时,我需要打开cl.exe。任何人都可以给我一些示例代码。从C#应用程序打开cl.exe

+1

?执行cl.exe与执行任何其他进程没有区别... – 2010-06-15 04:48:31

+0

对不起,我无法得到它。 – Kasun 2010-06-15 04:51:00

+0

http://www.csharp-station.com/howto/processstart.aspx – 2010-06-15 04:57:43

回答

3
string filePath = "c:\program files\xyz\cl.exe"; 
System.Diagnostics.Process.Start(filePath); 
+0

嗨,当我使用这种方法时,它将显示以下错误。如何传递桩名CL.EXE System.ComponentModel.Win32Exception了未处理 消息=“系统找不到指定文件” 源=“系统” 错误码= -2147467259 NativeErrorCode = 2 堆栈跟踪: – Kasun 2010-06-15 05:23:39

+0

放入CL.exe的完整路径,例如“c:\ program files \ xyz \ cl.exe” – 2010-06-15 07:24:37

+0

这不是正确的答案,cl.exe需要将mspdb100.dll(和其他一些项目)放在%PATH%中。我在下面概述了真正的解决方案。 – 2013-08-29 04:18:28

1

如果你想打开一些EXE可以使用:Process.Start("cl.exe");

+0

嗨,当我使用这种方法时,它会显示以下错误。我怎样才能通过堆名称为CL.​​exe System.ComponentModel.Win32Exception是未处理消息=“系统找不到指定的文件”Source =“System”ErrorCode = -2147467259 NativeErrorCode = 2 StackTrace – Kasun 2010-06-15 05:23:59

+1

您需要提及正确的路径的exe。 – anishMarokey 2010-06-15 06:31:44

0

cl.exe时需要DLL的不在%PATH%。您需要为架构执行正确的批处理(x86与64位)。这将更新您的环境,以便您可以正确启动cl.exe。

86就设在这里(VS2010):

  • C:\ Program Files文件(x86)的\微软的Visual Studio 10.0 \ VC \ BIN \ VCVARS32.BAT

我目前启动命令控制台,执行批处理,然后在我的临时文件上启动编译。

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(); 

我目前正在研究直接启动VCVARS32.BAT,但我不知道这是否正确更新环境对我的C#应用​​程序。我会更新你,如果我得到它的工作。

此外,请确保您已阅读并理解这一主题接受的答案:你尝试过什么ProcessStartInfo hanging on "WaitForExit"? Why?