1

我有一个简单的示例程序,它从 std::cin 读取并写入 std::cout。如果在 cmd.exe 或 Visual Studio 调试器中运行,它工作正常。代码(server.cpp):

#include <iostream>
#include <istream>
#include <ostream>
#include <string>

int main(int argc, char* argv[])
{
    std::string input;
    while (std::getline(std::cin, input))
    {
        if (input == "dog")
        {
            std::cout << "cat" << std::endl;
        }
        else if (input == "white")
        {
            std::cout << "black" << std::endl;
        }
        else if (input == "quit")
        {
            std::cout << "exiting" << std::endl;
            return 0;
        }
        else if (input != "")
        {
            std::cout << "unknown" << std::endl;
        }
    }

    std::cout << "error" << std::endl;
}

现在我想从另一个写入其标准输入并从标准输出读取的进程运行它。我创建两个管道并使用 CreateProcess 启动一个进程,其中一个管道的读取句柄作为 StdInput 句柄,另一个管道的写入句柄作为 Stdouput 句柄。代码(client.cpp):

#include <Windows.h>

#include <cassert>
#include <iostream>
#include <ostream>
#include <string>

namespace
{
    class Server
    {
    public:
        Server() :
            m_pi()
        {
            SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES)};
            sa.bInheritHandle = TRUE;
            assert(CreatePipe(&m_ro, &m_wo, &sa, 0));
            assert(SetHandleInformation(m_ro, HANDLE_FLAG_INHERIT, 0));
            assert(CreatePipe(&m_ri, &m_wi, &sa, 0));
            assert(SetHandleInformation(m_ri, HANDLE_FLAG_INHERIT, 0));

            STARTUPINFO si = {sizeof(STARTUPINFO)};
            si.dwFlags |= STARTF_USESTDHANDLES;
            si.hStdInput = m_ri;
            si.hStdOutput = m_wo;

            assert(CreateProcess(L"..\\Debug\\server.exe", 0, 0, 0, 1, 0, 0, 0, &si, &m_pi));
        }

        ~Server()
        {
            execute("quit\n");
            assert(WaitForSingleObject(m_pi.hProcess, INFINITE) != WAIT_FAILED);

            assert(CloseHandle(m_pi.hThread));
            assert(CloseHandle(m_pi.hProcess));
            assert(CloseHandle(m_wi));
            assert(CloseHandle(m_ri));
            assert(CloseHandle(m_wo));
            assert(CloseHandle(m_ro));
        }

        std::string execute(std::string const& cmd)
        {
            DWORD num_bytes;
            assert(WriteFile(m_wi, cmd.c_str(), (DWORD)cmd.size(), &num_bytes, 0));

            std::string output;

            DWORD n = 0;
            while (n == 0)
            {
                Sleep(0);

                assert(PeekNamedPipe(m_ro, 0, 0, 0, &n, 0));
                if (n > 0)
                {
                    output.resize(n);
                    assert(ReadFile(m_ro, &output[0], n, &num_bytes, 0));
                }
            }

            return output;
        }

    private:
        HANDLE m_wo, m_ro, m_wi, m_ri;
        PROCESS_INFORMATION m_pi;
    };

    Server g_server;
}

int main(int argc, char* argv[])
{
    std::cout << g_server.execute("white\n") << std::endl;
    std::cout << g_server.execute("foobar\n") << std::endl;
    std::cout << g_server.execute("dog\n") << std::endl;
}

问题是客户端只接收到按摩“错误”,所以服务器的std::cin似乎坏了。

我的问题是,我做错了什么?

4

1 回答 1

1

您正在为孩子将用来读取的句柄禁用继承stdin- 孩子需要继承该句柄。代替:

SetHandleInformation(m_ri, HANDLE_FLAG_INHERIT, 0);

尝试以下操作以禁用服务器进程将用于写入子进程的句柄上的继承stdin

SetHandleInformation(m_wi, HANDLE_FLAG_INHERIT, 0);
于 2013-03-07T15:52:19.247 回答