长时间运行的控制台应用程序(C ++)中的输入和输出文本

时间:2013-07-06 08:32:45

标签: c++ winapi console stdout stdio

我有一个长期运行的控制台应用程序,它以Windows开头,或者可能是由我正在编写的应用程序启动的。

我向控制台应用程序发送密钥的成功有限,甚至不知道从哪里开始读取它的输出。是否正在读取甚至可能由请求应用程序启动的控制台应用程序的输出?

无论如何,有两个问题......

如何模拟“返回”键? (这是我必须向应用程序发送特定密钥,应用程序确实收到密钥,但我似乎无法找到如何模拟ENTER键。

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);

如何从控制台应用程序中读取数据? (如果其他控制台应用程序没有连续运行,我只会将输出写入文件并在...中读取它仍然可以工作)

我已经看了一些选项,但我读过的许多选项都是针对C#的,遗憾的是我没有改变语言的奢侈品。 C ++有没有类似的选项?

Collect stdout output from a console app with C++ 这个不起作用,因为应用程序不会退出。所以它只是挂起并等待。不断向缓冲区添加更多内容。

Catch console input-output

Capturing an c# executable output from another C# program

感谢您提供的任何帮助!

1 个答案:

答案 0 :(得分:1)

您需要将该应用程序的输入/输出重定向到主应用程序。以下是关于此的文章:Creating a Child Process with Redirected Input and Output

相关问题