日期:2014-05-18 浏览次数:21433 次
// 代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CommandTest
{
    public partial class FrmRunCommand : Form
    {
        System.IO.StreamWriter sw;  // 定义输出流 sw 作为Shell的标准输入,即命令 
        System.IO.StreamReader sr;  // 定义输出流 sr 作为Shell的标准输出,即正常结果
        System.IO.StreamReader err; // 定义输出流 err 作为Shell的错误输出,即出错结果
        System.Diagnostics.Process p = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo psI = new System.Diagnostics.ProcessStartInfo(System.Environment.GetEnvironmentVariable("ComSpec"));
    public FrmRunCommand()
        {
            InitializeComponent();
        }
    private void btnRun_Click(object sender, EventArgs e)
        {
           
            psI.UseShellExecute =false ;
            psI.RedirectStandardInput   =   true;
            psI.RedirectStandardOutput   =   true;
            psI.RedirectStandardError   =   true;
            psI.CreateNoWindow   =   true;
            p.StartInfo = psI;
        Cursor = System.Windows.Forms.Cursors.WaitCursor;
        
        p.Start(); 
            sw = p.StandardInput; 
            sr = p.StandardOutput;
            err = p.StandardError;
        sw.AutoFlush = true;
        if(coboCommand.Text != "")
            {
                sw.WriteLine(coboCommand.Text);
            }
            else
            {
                sw.WriteLine("echo 未输入命令");
            }
            sw.Close();
        tbResult.Text = "输出结果为:"+sr.ReadToEnd();
            tbResult.Text += "\n错误信息:\n"+err.ReadToEnd();
        Cursor = System.Windows.Forms.Cursors.Default;
        }
    }
}
------解决方案--------------------
C# 中调用 C++ 编写的动态链接库函数,如果需要出口参数输出,则需要使用指针,对于字符串,则需要使用双重指针,对于 C# 的导入定义,则需要使用引用(ref)定义。