C#/.Net调用控制台程序并获得返回结果
2014-01-06 15:04
204 查看
使用C#/.Net调用控制台程序并获得返回结果主要是可以实现托管代码调用非托管代码过程中,非托管代码内存泄露(无源代码,无法修改)导致的主程序奔溃,而控制台程序则可以针对非托管代码再次封装,处理内存泄露异常。
注意在控制台程序中,需要使用: SetErrorMode(((SetErrorMode(ErrorModes.SYSTEM_DEFAULT) | ErrorModes.SEM_NOGPFAULTERRORBOX) | ErrorModes.SEM_FAILCRITICALERRORS) | ErrorModes.SEM_NOOPENFILEERRORBOX);来实现系统错误提示的屏蔽,如:XXXX模块内存读取失败,程序即将关闭...详见:《.NET通过 P/Invoke调用win32 dll的一个实际例子》
首先明确调用者与被调用者是在两个不同的进程中,返回结果使用委托代理来获取。主要代码如下:
string strConsoleRoot = System.Configuration.ConfigurationManager.AppSettings["ConsoleRoot"]; string strConsolePath = strConsoleRoot + @"\FingerMatchSingle.exe"; string strVerTemplate = listStudent.First()["CardNo"].ToString(); string strFingerTemplate1 = "\"" + strCardNumber + "\""; string strFingerTemplate2 = "\"" + strVerTemplate + "\"";
ProcessStartInfo start = new ProcessStartInfo(); start.Arguments = strConsolePath + " " + strFingerTemplate1 + " " + strFingerTemplate2; start.WorkingDirectory = strConsoleRoot; start.FileName = strConsolePath; start.UseShellExecute = false; start.RedirectStandardInput = true; start.RedirectStandardOutput = true; start.RedirectStandardError = true; start.CreateNoWindow = true; process = Process.Start(start); process.OutputDataReceived += (sender, e) => { if (!string.IsNullOrEmpty(e.Data)) { } else { //控制台程序异常后的代码 } process.CancelOutputRead(); process.Close(); process.Dispose(); }; process.BeginOutputReadLine();
其中控制台程序代码,请见《.NET通过 P/Invoke调用win32 dll的一个实际例子》
来自为知笔记(Wiz)
注意在控制台程序中,需要使用: SetErrorMode(((SetErrorMode(ErrorModes.SYSTEM_DEFAULT) | ErrorModes.SEM_NOGPFAULTERRORBOX) | ErrorModes.SEM_FAILCRITICALERRORS) | ErrorModes.SEM_NOOPENFILEERRORBOX);来实现系统错误提示的屏蔽,如:XXXX模块内存读取失败,程序即将关闭...详见:《.NET通过 P/Invoke调用win32 dll的一个实际例子》
首先明确调用者与被调用者是在两个不同的进程中,返回结果使用委托代理来获取。主要代码如下:
string strConsoleRoot = System.Configuration.ConfigurationManager.AppSettings["ConsoleRoot"]; string strConsolePath = strConsoleRoot + @"\FingerMatchSingle.exe"; string strVerTemplate = listStudent.First()["CardNo"].ToString(); string strFingerTemplate1 = "\"" + strCardNumber + "\""; string strFingerTemplate2 = "\"" + strVerTemplate + "\"";
ProcessStartInfo start = new ProcessStartInfo(); start.Arguments = strConsolePath + " " + strFingerTemplate1 + " " + strFingerTemplate2; start.WorkingDirectory = strConsoleRoot; start.FileName = strConsolePath; start.UseShellExecute = false; start.RedirectStandardInput = true; start.RedirectStandardOutput = true; start.RedirectStandardError = true; start.CreateNoWindow = true; process = Process.Start(start); process.OutputDataReceived += (sender, e) => { if (!string.IsNullOrEmpty(e.Data)) { } else { //控制台程序异常后的代码 } process.CancelOutputRead(); process.Close(); process.Dispose(); }; process.BeginOutputReadLine();
其中控制台程序代码,请见《.NET通过 P/Invoke调用win32 dll的一个实际例子》
来自为知笔记(Wiz)
相关文章推荐
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- .Net ( C# ) 调用控制台程序并取得输出结果
- .Net ( C# ) 调用控制台程序并取得输出结果
- .Net ( C# ) 调用控制台程序并取得输出结果(转)
- 使用ShellExecuteEx调用控制台程序或命令行,传入多个参数,并等待返回结果
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- c#.NET 调用命令控制台 并获取执行结果
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- .Net ( C# ) 调用控制台程序并取得输出结果
- 学习记录 asp.net c#连接sql server 2012 调用存储过程 用返回的记录填充CheckBoxList 都是常用的,特此记录
- c# 调用控制台程序并传参 获取控制台输出(解释器的前台处理)
- C#调用ORACLE存储过程返回结果集
- 用C#.NET调用Java开发的WebService传递int,double问题,出现java无法获得值!
- c# 调用控制台程序并传参 获取控制台输出(解释器的前台处理)
- C#中利用process类调用外部程序以及执行dos命令 - ASP.NET
- 用C#.NET调用Java开发的WebService传递int、double,结果总为0
- Delphi程序调用C#.Net编译的DLL并打开窗体(详解)
- C# 调用控制台程序,并获取输出写入文件
- C#通过R.Net调用R程序