Get last line displayed by another command line window

Hi All,

I've spent my Saturday writing a program which now works perfectly well and I spent this morning making it look nice.
However, I want to make it run at the right time automatically, instead of waiting for a user to start it when needed.

The basic problem is, that in a WinPE environment an exe is running. Unfortunately it would need critical input, which must be inputted perfectly. So, I wrote a program which gets the data and sends it to the other app, by bringing it to the front and presses the keys needed using SendInput().
However, this program should wait for it's cue, then get on the inputting part. It's cue should be the point where the program waits for the first user input with this displayed on the last line:
Text:

My question is: how to listen and check whether the last line displayed is "Text:"?

I've tried with AttachConsole(), but for some reason it opens a new console window. I checked and the PID I'm using is the console window's, so I don't know why that happens.
The few lines I'm trying with:
1
2
3
4
5
6
7
8
9
10
11
12
13
    HWND hwnd = FindWindow(NULL, "Administrator: Command Prompt");
    SetForegroundWindow(hwnd);
    SetFocus(hwnd);
    DWORD process_id;
    GetWindowThreadProcessId(hwnd, &process_id);
    AttachConsole(process_id);
    char lastline[5];
    COORD pos={1,1};
    DWORD dwChars;
    ReadConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), lastline, 5, pos, &dwChars)
        lastline[dwChars]='\0';
        cout << dwChars << endl;
    FreeConsole();


Please don't criticize the first line. I know it can be ambiguous, but I modified it, so it can be tested at home by anyone using an admin prompt. When actually using it, the exe name will be in the title, so it will be unique.

Thanks in advance!
Last edited on
It can be done, but it shouldn't be. You really need to rethink how you are handling the problem.

Can you better explain exactly what your programs are trying to do? Step by step, complete with examples?
Thanks for your reply Duoas!

All right...
There's an executable, which is running a system under WinPE (Win8 based). This is "from a third party", so it works the way it does, no changing that. It is a simple command line application, no GUI.

At a certain point it will stop for user input, with the prompt:
Text:


At that point, the program I'm writing needs to step in, read the data needed from a file, get the above mentioned command line window to the front and enter the data by emulating key presses.
Most of that works perfectly, but I can't tell it when to do this.
It can't just start typing whenever it wants, it has to start when "Text:" comes up and the cursor is blinking next to it. :)

So, how can I tell what is the last line outputted by another command line program? I got it's hwnd and it's process id too.

If nothing else works, then the output of the exe could be routed into a file or something, but it needs input, so that's probably not a good idea...
Last edited on
I'm still interested in an answer, but I might have found a workaround.
I've overlooked the fact, that standard input can be directed to come from a file. So, if I take my time to check all inputs needed by the program, I should be able to write a program to compile all input required, before calling the exe with stdio redirected.

I have to get to sleep now or I won't be able to get up tomorrow morning to get to work and actually check this theory.

Thanks!
Yes, that would work fine. The 3rd party program will only read input when it is ready, so you could easily link them with a pipe.

If you need to automate stuff more advanced than this in the future, check out Expect.
http://www.nist.gov/el/msid/expect.cfm
Topic archived. No new replies allowed.