vendredi 29 mai 2015

Read lines from console output in the current Console

My program is executing another. Both run in the same Console. Program 2 is writing text in the console. So Programm 1 should read and process it. There is no other Solution for me, because i don't own program 2, so i can't modify it. So the idea is that the output of program 2 is still visible. By now the programs both work multithreaded alongside eachother, so this part is fine. So my question is, how do i read lines from the console? A small example how this sould work:

string readMyLine(){
    //Solution Here
}    
main(){
        cout<<"Hello this is a line"<<endl;
        string line = readMyLine(); //line should be "Hello this is a line"
        cout<<"The previus Line was "<<line<<endl;
    }

Well the idea is not to use pipes, rather than reading directly from the console like shown in my example; I had a Idea but it won't work. It returns a pointer, which doesn't help me much...

string ReadConsoleLine(COORD rectStart, COORD rectEnd) {
    HANDLE     hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    PCHAR_INFO buffer = new CHAR_INFO[(rectEnd.X - rectStart.X) * (rectEnd.Y - rectStart.Y)];
    COORD      buffer_size = { (rectEnd.X - rectStart.X), (rectEnd.Y - rectStart.Y) };
    COORD      buffer_index = { 0, 0 };
    SMALL_RECT read_rect = { rectEnd.X, rectEnd.Y, rectEnd.X, rectEnd.Y};
    ReadConsoleOutput(hStdOut, buffer, buffer_size, buffer_index, &read_rect);
    stringstream Out;
    Out << buffer;
    return Out.str();
}

Aucun commentaire:

Enregistrer un commentaire