Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
I am using this code to pass two numbers as input to an .exe of a C program file through C# and after that trying to read the output from console. I am having problem to read any output from the console.

My C# code is:
C#
string output;
        string pathforStudentOutput;
        
        string text;
        //string line = "1" + "2";
        Process p = new Process();
        p.StartInfo.CreateNoWindow = true;
      
        p.StartInfo.FileName = ("C:\\Users\\...\\noname01.exe");
       
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardInput = true;
        p.Start();
       
        Thread.Sleep(500);
        SendKeys.SendWait("1");
        Thread.Sleep(500);
        SendKeys.SendWait("~");
        Thread.Sleep(500);
        SendKeys.SendWait("2");
        Thread.Sleep(500);
        SendKeys.SendWait("~");
       
            using (StreamReader streamReader = p.StandardOutput)
            {
                output = streamReader.ReadToEnd();
                Label2.Text = output;
            }
            pathforStudentOutput = @"C:\\Users\\....\\" + "StudentOutput" + ".txt";
            using (StreamWriter sw = File.CreateText(pathforStudentOutput))
            {
                sw.WriteLine(output);
                sw.Close();
               
            }

My C code to which am passing inputs is.
C++
#include<stdio.h>

    int main()
    {
    int a, b, c;

    printf("Enter two numbers to add\n");
    scanf("%d%d",&a,&b);

    c = a + b;

    printf("Sum of entered numbers = %d\n",c);

    return 0;
    }


Any kind of help/Suggestion will be appreciated
Posted
Updated 30-Aug-14 22:19pm
v2
Comments
nv3 30-Aug-14 12:32pm    
This basically the same question you posted yesterday. This is not the best way of transferring data from one application to another. For small amounts you may use command line arguments. Or use the method you tried in your previous question, piping into the stdin of the other process. Or as a third method use a winsocket.

Let's find out why your original method did not work. I would modify the main() of the C program to print the stdin input line by line, so that we first can see, what the other program receives. Dividing the input up into two numbers is the next step and should be easy, once you have figured out that your string has been correctly passed.

1 solution

The problem is with:
C#
Thread.Sleep(500);
SendKeys.SendWait("1");


SendWait is for the active process, to be sure that input is going to your Process p, use p.StandardInput. Any write to StandardInput will go to your Process p ("C:\\Users\\...\\noname01.exe")
 
Share this answer
 
Comments
Hussain ahmad 31-Aug-14 14:27pm    
Through p.StandardInput.writeline
it just opens the .exe file and never pass any kind of input

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900