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!

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you! I saw another MSDN article as well (support.microsoft.com/kb/190351) any thoughts on that one?
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.
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.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.