Redirect stdout to an edit control (Win32)

后端 未结 3 376
终归单人心
终归单人心 2020-12-06 03:14

I have a simple Win32 GUI app which has an edit control in the main window. If I write:

printf(\"Hello world!\\n\");

I would like the text

相关标签:
3条回答
  • 2020-12-06 03:55

    Write a internal __printf function can output the text to edit control, then replace all printf functions with that.

    0 讨论(0)
  • 2020-12-06 04:13

    Check out the API call SetStdHandle. Once the stdout is redirected to your stream, read from it and send the text to the edit control.

    [Edit]

    Take a look at using dup2. The following code seems to work.

    int fds[2];
    _pipe (fds, 1024, O_TEXT);
    _dup2 (fds[1], 1);      // 1 is stdout
    printf ("Test\r\n");
    char buffer[100];
    _flushall();            // Need to flush the pipe
    int len = _read (fds[0], buffer, 100);
    buffer[len] = 0;        // Buffer now contains "Test\r\n"
    
    0 讨论(0)
  • 2020-12-06 04:18

    You can do that in Windows by redirecting stdout when you create the process. You do so by setting flags and setting some handles in the STARTUPINFO structure passed to CreateProcess. See this example on MSDN for detail on how to set this up.

    Once you have that setup can use ReadFile to read from the redirected stdout of the console process then send it to the edit control.

    0 讨论(0)
提交回复
热议问题