Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all.

I have a basic exe file which requires the pressing of a letter key, followed by the return key to input that selection. I am trying to control this input via a game I've built in Unity, specifically, via functions I call with key-presses.

Currently, when I press 'space', the following function is called which opens the exe:
public static void runStim ()

{
    ProcessStartInfo startInfo = new ProcessStartInfo("C:\\Users\\Nathan\\Desktop\\stim_data_receiver.exe", "NO_STIMULATOR 1000000 REMOTE");
    startInfo.RedirectStandardOutput = false;
    startInfo.RedirectStandardError = false;
    startInfo.RedirectStandardInput = true;
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = false;

    Process stim = new Process();
    stim.StartInfo = startInfo;    
    stim.Start();
}


While this is open and back in the game, the return key calls a 'enterCommand' function. I need this to write a letter to the program in the same way you can when the exe is opened on it's own. I haven't been successful in this.

I have been advised that my tried methods (below) try to send data to the process by writing to the exe file itself and not the input stream of the process - can anyone help me achieve this?

I should mention that my method does work with writing a string to a txt file.

Any help is much appreciated. Thank you.

What I have tried:

public static void enterCommand()

{
    using (StreamWriter outputfile = new StreamWriter("C:\\Users\\Nathan\\Desktop\\stim_data_receiver.exe", true))
    {
        outputfile.Write("%/Hello");
    }
}

public static void enterCommand()

{
    StreamWriter w;
    w = new StreamWriter(@"C:\\Users\\Nathan\\Desktop\\stim_data_receiver.exe");
    w.WriteLine("hello");
}
Posted
Updated 11-Aug-16 3:38am
v2

1 solution

You need to write the command to the StandardInput property of the Process instance:
C#
ProcessStartInfo startInfo = new ProcessStartInfo("C:\\Users\\Nathan\\Desktop\\stim_data_receiver.exe", "NO_STIMULATOR 1000000 REMOTE");
startInfo.RedirectStandardOutput = false;
startInfo.RedirectStandardError = false;
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = false;

Process stim = Process.Start(startInfo);
stim.StandardInput.WriteLine("hello");

If you need to send input from a different method, then you'll need to store the Process instance in a field:
C#
private static Process _stim;

public static void runStim()
{
    ...
    _stim = Process.Start(startInfo);
}

public static void enterCommand()
{
    if (_stim != null && !_stim.HasExited)
    {
        _stim.StandardInput.WriteLine("hello");
    }
}
 
Share this answer
 
Comments
Member 12681347 11-Aug-16 13:21pm    
Thank you so much for your help, Richard.

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