0

I have a long running console application that starts with Windows, or that may be started by the app I'm writing.

I have had limited success sending keys to the console application, and don't even know where to start for reading output from it. Is reading the output from an console application that isn't even started by the requesting application even possible?

Anyways, two questions...

How can I emulate a "return" key? (Here is what I have to send specific keys to the application, and the app does receive the keys, but I can't seem to find how to emulate the ENTER key.

do {
    hwndCurrentWindow = FindWindowA("ConsoleWindowClass", "My Other Console App");
    if (hwndCurrentWindow == 0) {
        break;
    }

    iStringLen = GetWindowTextW (hwndCurrentWindow, wcharWindowText, 500);
    if (iStringLen == 0) {
        continue;
    }

    SetActiveWindow(hwndCurrentWindow);
    printf("Sending '?'");
    SendMessage(hwndCurrentWindow, WM_CHAR, '?', 0);
    // '?' shows up in console app
    printf("Sending 'a'");
    SendMessage(hwndCurrentWindow, WM_CHAR, 'a', 0);
    // 'a' shows up in console app.
    //printf("Sending RETURN");
    //SendMessage(hwndCurrentWindow, WM_CHAR, VK_RETURN, 0);
    // nothing happens
    break;
} while (hwndCurrentWindow);

How can I read data from the console application? (If the other console application didn't run continuously I would just write the output to a file and read that in... heck that may still work)

I have looked at a handful of options, but many of the ones I read are for C#, and sadly I don't have the luxury of changing languages. Are there any similar options for C++?

Collect stdout output from a console app with C++ This one doesn't work because the application doesn't quit. So it just hangs and waits. Continuously adding more to the buffer.

Catch console input-output

Capturing an c# executable output from another C# program

Thanks for any help you can provide!

Community
  • 1
  • 1
Kyle Johnson
  • 639
  • 7
  • 21

1 Answers1

1

You need to redirect input/output from that application to your main application. Here is article on that: Creating a Child Process with Redirected Input and Output

Bogolt
  • 520
  • 1
  • 3
  • 9
  • Thank you! I saw another MSDN article as well (http://support.microsoft.com/kb/190351) any thoughts on that one? – Kyle Johnson Jul 06 '13 at 15:14
  • 1
    Basically they do the same thing. The point you need to watch closely is CreateProcess() function call and parameters to use. You'll need to create a set of pipes, and then specify then in CreateProcess() so that they will be used to communicate between your app and process you spawn. I'm sure you can take any of examples to get results you need. – Bogolt Jul 06 '13 at 17:41
  • That really helped! I have had a great amount of success. I used the PID to kill the application when my app finishes, and can grab the output from the console app, but have had zero luck sending any data with the WriteToPipe command.. Even supplying a valid path of a file sends nothing, I put in a small Sleep command to make sure it was running before trying to send data, or perhaps I'm not seeing the data being sent because an ENTER is not being sent. Any ideas on how to send a string to the g_hChildStd_IN_Wr pipe? Or is that the g_hChildStd_IN_Rd pipe?? That's used once. Wall-of-text. – Kyle Johnson Jul 09 '13 at 19:24
  • 1
    You don't need to send Enter, all you need is send data to pipe on one side, and receive it on anther. Make sure child app is running ( best way to test is making it run infinitely, always waiting for something to come from pipe ), there is good client example on this in the above link. And according to that link you need to use g_hChildStd_IN_Wr to write to pipe. – Bogolt Jul 10 '13 at 09:21
  • That's what I thought, so I tried sending `\r\n` or a variation of it. The console app that gets launched basically spits out some information to STDOUT and then keeps refreshing the last line waiting for user input. This blocks the ReadFromPipe() section. That aside, even sending text via WriteToPipe doesn't seem to be making it over. It's very frustrating, but every day I get a little bit closer. – Kyle Johnson Jul 10 '13 at 18:28
  • 1
    First you need to find out what part of your code do not work properly. It may be sending or receiving side. To get this - try using 3rd party samples ( like in the link above ) with your components, so you'll know what part of code is to blame. Then you can simply check against samples as to find what exactly you are doing wrong. – Bogolt Jul 11 '13 at 08:19
  • Yeah, I found that the application I'm trying to send data to is using ReadConsoleInput, so I have to use some version of ConsoleWrite.. And right now I'm trying to figure out how to build the INPUT_RECORD as it would be created by ReadConsoleInput so that I can send it to ConsoleWrite. – Kyle Johnson Jul 11 '13 at 14:37