简体   繁体   English

使用C#应用程序中的命令行程序

[英]Using a command line program from within a C# application

I have written a C++ program (which executes from the command line), that works fine. 我编写了一个C ++程序(从命令行执行),工作正常。 Now I need to use it for my C# application. 现在我需要将它用于我的C#应用​​程序。 That is to say, I would like the output from my C++ program to be used in my C# application whenever it is called. 也就是说,我希望我的C ++程序的输出可以在我的C#应用​​程序中使用。

Is it possible? 可能吗? If so, how? 如果是这样,怎么样?

Any links or help would be appreciated. 任何链接或帮助将不胜感激。

You can use System.Diagnostics.Process to fire up your C++ program and redirect its output to a stream for use in your C# application. 您可以使用System.Diagnostics.Process启动C ++程序并将其输出重定向到流以在C#应用程序中使用。 The information in this question details the specifics: 此问题中的信息详细说明了具体内容:

string command = "arg1 arg2 arg3"; // command line args
string exec = "filename.exe";      // executable name
string retMessage = String.Empty;
ProcessStartInfo startInfo = new ProcessStartInfo();
Process p = new Process();

startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;

startInfo.UseShellExecute = false;
startInfo.Arguments = command;
startInfo.FileName = exec;

p.StartInfo = startInfo;
p.Start();

using (StreamReader output = p.StandardOutput)
{
    retMessage = output.ReadToEnd();
}

p.WaitForExit();

return retMessage;

Make your C++ code DLL, and use pinvoke to call the C++ functions from C# code. 制作C ++代码DLL,并使用pinvoke从C#代码调用C ++函数。

Read this article: Calling Win32 DLLs in C# with P/Invoke 阅读本文: 使用P / Invoke在C#中调用Win32 DLL

Another way to do this is to use Process class from .Net. 另一种方法是使用.Net的Process类。 Using Process, you don't need to make your C++ code DLL; 使用Process,您不需要制作C ++代码DLL; you can start your C++ EXE as a process from C# code. 你可以从C#代码开始你的C ++ EXE进程。

You could have your C++ program write it's output to a file, and have your C# program read from the file. 您可以让您的C ++程序将其输出写入文件,并从文件中读取您的C#程序。

If your application is very performance sensitive, then this is not the best way. 如果您的应用程序对性能非常敏感,那么这不是最好的方法。

Here is the C# code to run the C++ program: 以下是运行C ++程序的C#代码:

        try
        {
            Process p = StartProcess(ExecutableFileName);
            p.Start();
            p.WaitForExit();
        }
        catch
        {
            Log("The program failed to execute.");
        }

Now you are left to write to the file from your C++ program, and read from it in your C# program. 现在您可以从C ++程序写入文件,并在C#程序中读取它。

This will show you how to write to a file from your C++ program: http://www.cplusplus.com/doc/tutorial/files/ 这将向您展示如何从C ++程序写入文件: http//www.cplusplus.com/doc/tutorial/files/

This will show you how to read from a file in your C# program: http://msdn.microsoft.com/en-us/library/ezwyzy7b.aspx 这将向您展示如何从C#程序中的文件中读取: http//msdn.microsoft.com/en-us/library/ezwyzy7b.aspx

Since it seems the OP didn't leave any further comments, I am left wondering, would | 由于OP似乎没有留下任何进一步的评论,我想知道,将| not have sufficed? 没有足够的? I guess it comes down to the object that is the "it" in " whenever it is called ". 我想它归结为“ 无论何时被调用 ”中的“它”对象。 If "it" refers to the C++ program, then Andy Mikula's answer is best. 如果“它”指的是C ++程序,那么Andy Mikula的答案是最好的。 If "it" refers to the C# program, then I would suggest: 如果“它”指的是C#程序,那么我会建议:

C:\>myCpluplus.exe | myCsharp.exe

and simply read from Console.In within myCsharp.exe. 并简单地从myCsharp.exe中的Console.In读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM