Using command line from c # application

I wrote a C ++ program (which runs from the command line) that works fine. Now I need to use it for my C # application. That is, I would like the output of my C ++ program to be used in my C # application whenever it is called.

Is it possible? If so, how?

Any links or help would be appreciated.

+3
source share
4 answers

You can use System.Diagnostics.Processto run your C ++ program and redirect output to a stream for use in your C # application. 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;
+8
source

++- DLL pinvoke ++ #.

: DLL Win32 # P/Invoke

- Process .Net. Process, DLL ++-; ++ EXE #.

+1

++ # .

, .

# ++:

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

++ #.

, ++: http://www.cplusplus.com/doc/tutorial/files/

, #: http://msdn.microsoft.com/en-us/library/ezwyzy7b.aspx

+1

, OP , , | ? , , "" ", ". "" ++, . "" #, :

C:\>myCpluplus.exe | myCsharp.exe

Console.In myCsharp.exe.

0

Source: https://habr.com/ru/post/1787879/


All Articles