WriteFile function with assembly debugging (syncin

2019-07-21 08:56发布

问题:

First of all, this question is based on my last question here: Reading Console Buffer / Output C++

I have a compiled executable binary file. It has some outputs, what I would like to redirect it to an other program, that handles the lines. I successfully found where the output is sent, and I modified it to STDOUT. The problem is that, when I use it like:

./jampDed.exe | stdout.exe

then the output is not synced. I got the content after every 1000-2000 bytes.

stdout.cpp

#include <iostream>

int main() {
    std::string s;
    while (std::getline(std::cin, s, '\n')) {
        std::cout << s << std::endl;
    }

    return 0;
}

I also created a picture about assembly modification, where Kernel32.WriteFile function was used by default.

So the question is that, how can I make it synced? How to get every line as soon as it happens on the dedicated server?

回答1:

Somewhere in the executable where it establishes stdout is an option bit for unbuffered output. Just set (or clear) that bit. Then every call to write is transferred without delay. This adds significant execution time and i/o system effort to that program but is probably okay for this.

The program which processes that output (as input) should buffer full lines because the program is unlikely to do full line output itself.



回答2:

Why don't you try:

std::cout << s << std::endl << std::flush;
                               ^^^^^^^^^^