原文地址:http://blog.163.com/qiaoweipeng@126/blog/static/15737970200862331842368/
C#程序作为调用这需要生成一个Progress类,该类提供了调用EXE可执行文件所用到的属性和事件.
System.Diagnostics.Process pExecuteEXE
=
new
System.Diagnostics.Process(); Delphi可执行程序作为被调用程序,主要是接收参数信息,执行程序,由于程序执行程序完毕不能返回给调用程序信息,只能将信息写入某一位置等待调用者读取。pExecuteEXE.StartInfo.FileName = @" E:\Delphi.exe " ; pExecuteEXE.StartInfo.Arguments = " 'paramstr1 paramstr2,paramstr3' " ; pExecuteEXE.Start(); pExecuteEXE.WaitForExit(); // 无限期等待完成 // pExecuteEXE.WaitForExit(10000); // 等待最长10秒钟完成。
procedure TForm1
.
Button1Click(Sender
:
TObject); var i : integer; begin self . Caption := '' ; for i := 0 to paramcount do begin self . Caption := self . Caption + ' [ ' + inttostr(i) + ' : ' + paramstr(i) + ' ] ' ; end; end; 完成后退出。
这里是一个简单的调用例子,可以效仿: |
1. 如果exe文件的返回值是int类型,标识操作执行的结果是否成功,例如:
class Program
{
static int Main(string[] args)
{
return args.Length;
}
}
则在调用exe文件时,可以用如下方法:
Process myProcess = new Process();
string fileName = @"C:/Test.exe";
string para =@"你好 北京欢迎你!";
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(fileName, para);
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
while (!myProcess.HasExited)
{
myProcess.WaitForExit();
}
int returnValue = myProcess.ExitCode;
2. 如果exe文件是将输出内容写入标准流,例如:
class Program
{
static void Main(string[] args)
{
Console.Write(args[0] + args[1] + args [2]);
}
}
则在调用exe文件时,可以用如下方法:
string fileName = @"C:/Test.exe";
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = fileName;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.Arguments = "你好, 北京 欢迎你!";//参数以空格分隔,如果某个参数为空,可以传入””
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();