How to pass commands to an .exe via C #?

3

I wanted to call through a C # application any .exe and give them some commands as soon as it opened. For example, what I want to do at the moment is that the program opens, give three Tabs and fill in the values passed and give three more Tabs .

Does anyone know if it's possible to do this with C #?

Here's what I've tried:

System.Diagnostics.Process processo = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.FileName = @"C:\Users\VICTOR\Desktop\Test.exe";
startInfo.Arguments = "\t \t \t 0 \t 0 \t 0 \t 0 \t 0 \t 0 \t 0 \t 0 \t 0 \t 0 \t \t \t";
System.Diagnostics.Process.Start(startInfo);
    
asked by anonymous 19.01.2015 / 22:05

2 answers

3

You can do this:

First import the function SetForegroundWindow from user32.dll into your class:

[DllImport("user32.dll")]
static extern int SetForegroundWindow(IntPtr point);

And to send the keys you use SendKeys

    System.Diagnostics.Process processo;

    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
    startInfo.FileName = @"c:\Windows\notepad.exe";

    // inicia o processo
    processo = System.Diagnostics.Process.Start(startInfo);
    // aguarda até que o processo esteja pronto para receber entrada
    processo.WaitForInputIdle();
    // traz a janela principal pro primeiro plano
    SetForegroundWindow(processo.MainWindowHandle);

    // envia as teclas pro programa
    SendKeys.Send("{TAB 3}0{TAB}0{TAB}0{TAB}0{TAB}0{TAB}0{TAB}0{TAB}0{TAB}0{TAB}0{TAB 3}");

The complete documentation for class SendKeys can be viewed here

    
20.01.2015 / 02:04
1

Here you do not have the complete answer but a kickoff.

The user inputs (which you want to simulate) are sent to a form (in practice, a Windows window containing many other windows) through a mechanism called Windows messages .

Windows provides an API for you to send these messages: link .

C # does not have an abstraction of this API, so you'll have to use it directly as the same Windows API. In this example , the citizen sends messages from a C # application to Notepad (makes the application write text in the notepad).

Basically, you have to find the process of the other application, identify in this process the handle of the window ("form") to which you want to send the messages, and then send the messages.

    
19.01.2015 / 22:38