Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am working on passing inputs to an .exe file generated by compiling a C program by Compiler through C#. My C# code is:
C#
string line = "1" + " 2 " ;

Process p = new Process();                
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.StartInfo.FileName = "C:\\..\\demo1.exe";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine(line);

p.Close();

I successfully passed single inputs for C programs that read one input. But when i try to pass multiple inputs to C programs requiring multiple inputs, it only run exe file and it do not read/pass any of the inputs i provide. Also i have hidden the process of exe file but it opens it.

My C Code to which am trying to pass multiple 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 29-Aug-14 1:59am
v2
Comments
[no name] 29-Aug-14 9:03am    
You are not passing anything to your C program and your C program is not reading any arguments passed to it.
Herman<T>.Instance 29-Aug-14 9:24am    
correct!
Hussain ahmad 29-Aug-14 9:57am    
but i am trying to pass the arguments through C# on the .exe File that is generated by the compilation of the C program written Above
George Jonsson 30-Aug-14 3:32am    
I have tested the code you have provided and for me it works well.

1 solution

Set the p.StartInfo.Arguments = line;

see here[^] (scroll a bot down) for the example.

AND:
In the Main() you do not state: Main(string numbers) and enset that to the desired int's.
 
Share this answer
 
v2
Comments
Hussain ahmad 29-Aug-14 8:37am    
Still Same Issue!
Herman<T>.Instance 29-Aug-14 8:44am    
Have you tried with 1, 2 as argument?
Or tried using Verbs command? -> http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.verbs(v=vs.110).aspx
Herman<T>.Instance 29-Aug-14 9:25am    
You are right. In the Main() you do not state: Main(string numbers) and enset that to the desired int's.
Hussain ahmad 29-Aug-14 9:05am    
yes i have tried it
and Verbs Command is to Gets or sets the verb to use when opening the application or document
Hussain ahmad 29-Aug-14 9:56am    
but i am passing the arguments on the .exe File that is generated by the compilation of the C program written Above

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