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

Asked

Viewed 2,804 times

3

I wanted to call through an application in C# a .exe any and pass 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 past values and give three more Tabs.

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

Follow 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);
  • Okay, thanks for the tip. I’ll wait for more to mark as an answer in the next questions. And the answer I set ended up solving the initial question I asked. For the other problem, in case I can’t solve, I believe it’s best to ask a new question.

  • Right! I was just going to suggest you ask a new question. I wanted to give you a touch because I noticed that you edited the question to make it more intuitive; but you’re already on. Good luck there!

2 answers

3


You can do it like this:

First import the function SetForegroundWindow of user32.dll in 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 of the class SendKeys can be seen here

  • Thank you very much! I was able to do what I wanted with this. I have another question, if you can help me I will be very grateful. How I get a secondary window opened by the program that is receiving the commands so that it also receives them?

  • @Victormelias That’s what you can use Findwindowex

1

Here you don’t have the full answer but a kick start.

User entries (which you want to simulate) are sent to a form (in practice, a Windows window that contains many other windows) through a mechanism called "windows messages".

Windows offers an API for you to send these messages: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v=vs.85). aspx.

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

Basically, you have to locate the other application’s process, identify in this other process the Handle from the window ("form") to which you want to send the messages, and then send the messages.

  • It really helped that explanation. I was able to do as an example of sending a text to the notepad, but from what I could understand, it just sends text and not commands. I couldn’t find a way to change the code to send commands instead of text, like Sendkeys.

  • 1

    @Victormelias The second parameter of Sendmessage is the type of message you want to send. By toggling this second parameter you can send any type of Windows message (like mouse click, hotkey, window repeat order), including sending a Tab. I’d set an example, but from the looks of it, Sendkeys has already solved your problem. Remember that Sendkeys only sends keys to the selected application in the foreground while Sendmessage sends to any window. Good luck there!

  • Got it, thanks! Actually, this is the problem I’m facing right now, sending commands to a second window opened by the program. In the link you have passed, they indicate the parameters that can be used here http://msdn.microsoft.com/en-us/library/windows/desktop/ms644927(v=vs.85). aspx#system_defined but do not pass the corresponding hexadecimal. You know where to find the list of hexadecimal parameters?

  • 1

    @Victormelias I found here a list of constants: https://social.msdn.microsoft.com/Forums/en-US/86987753-eb91-4c13-b0a0-7151778913e4/sendmessage. So, to send a Tab, it looks like this: SendMessage(child, 0x100, 0, 0x09); - the second parameter is the message type (key down) and the last is the Tab key.

  • Thanks again. Unfortunately it didn’t work. Neither Tab (0x09) nor Enter (0x0D) worked. I will continue searching to find a solution. Thank you very much!

  • @Victormelias I don’t have . Net here or I could do some tests. If you want tabs just to position on a particular control, try sending "set Focus".

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.