我有一个简单的示例程序,它从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;
}
现在我想从另一个写入stdin并从stdout读取的进程运行它。我创建了两个管道并使用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似乎坏了。
我的问题是,我做错了什么?
答案 0 :(得分:1)
您正在禁用子句将用于从stdin
读取的句柄的继承 - 孩子需要继承该句柄。而不是:
SetHandleInformation(m_ri, HANDLE_FLAG_INHERIT, 0);
尝试以下操作来禁用服务器进程用于写入子项stdin
的句柄的继承:
SetHandleInformation(m_wi, HANDLE_FLAG_INHERIT, 0);