@indeterminatelyに配列決定からの答えが正しかった、あなたの助けをありがとうございました。問題は、パイプがコピーされたバッファが小さすぎて、それを別のパイプに割り当てることでした。
完全な解決策の助けに基づいて@決定的に出力をキュー構造にプッシュする、多分それは誰かを助けるでしょう。唯一の問題は、開かれたプログラムが決して閉じられず、TerminateProcess関数がどこかで使用されなければならないということです。
void ProcessManagement::CreateChildProcess()
// Create a child process that uses the previously created pipes for STDIN and STDOUT.
{
SECURITY_ATTRIBUTES saAttr = { sizeof(SECURITY_ATTRIBUTES) };
saAttr.bInheritHandle = TRUE; //Pipe handles are inherited by child process.
saAttr.lpSecurityDescriptor = NULL;
// Create a pipe to get results from child's stdout.
if (!CreatePipe(&hPipeRead, &hPipeWrite, &saAttr, 0))
return;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.hStdOutput = hPipeWrite;
si.hStdError = hPipeWrite;
si.wShowWindow = SW_HIDE; // Prevents cmd window from flashing. Requires STARTF_USESHOWWINDOW in dwFlags.
std::string command_temp = (" -i \"LAN 3\"");
LPSTR st = const_cast<char *>(command_temp.c_str());
BOOL fSuccess = CreateProcessA(program_path.c_str(), st, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
if (!fSuccess)
{
CloseHandle(hPipeWrite);
CloseHandle(hPipeRead);
return ;
}
/* Needs to be used somewhere
TerminateProcess(pi.hProcess,exitCode);
CloseHandle(hPipeWrite);
CloseHandle(hPipeRead);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);*/
}
void ProcessManagement::ReadFromPipe(void)
// Read output from the child process's pipe for STDOUT
{
std::deque<std::deque<std::string>> elems;
// Give some timeslice (50ms), so we won't waste 100% cpu.
bProcessEnded = WaitForSingleObject(pi.hProcess, 50) == WAIT_OBJECT_0;
// Even if process exited - we continue reading, if there is some data available over pipe.
for (;;)
{
char buf[8192];
DWORD dwRead = 0;
DWORD dwAvail = 0;
if (!::PeekNamedPipe(hPipeRead, NULL, 0, NULL, &dwAvail, NULL))
break;
if (!dwAvail) // no data available, return
break;
if (!::ReadFile(hPipeRead, buf, min(sizeof(buf)-1, dwAvail), &dwRead, NULL) || !dwRead)
// error, the child process might ended
break;
buf[dwRead] = '\0';
std::stringstream streamBuffer;
streamBuffer << unflushed_line << buf; // add to buffer also last unflashed line
unflushed_line = BufferToQueue(streamBuffer, ' ', elems);
for (auto idx = 0; idx != elems.size(); ++idx)
{
for (std::string const& b : elems[idx])
std::cout << b;
std::cout << std::endl;
}
}
}
std::string ProcessManagement::BufferToQueue(std::stringstream &streamBuffer, char delim, std::deque<std::deque<std::string>> &elems) {
std::string line;
std::string word;
std::deque<string> row;
bool is_unflushed_line = streamBuffer.str().back() != '\n';
// Splitting the stringstream into queue of queue (does not work properly)
while (std::getline(streamBuffer, line, '\n'))
{
std::istringstream iss(line);
while (std::getline(iss, word, delim)) {
row.push_back(word);
}
elems.push_back(row);
row.clear();
}
if (is_unflushed_line)
{
elems.pop_back(); // pop not fully flushed line
}
else line.clear(); // if the line was fully flushed return empty string
return line; // to add to buffer for next push to queue if the last was not flushed at the end
}
フラッシュするのを忘れたような匂い。 –
トピックオフ: 'std :: stringstream'にパイプ出力を書き出し、ストリーム上で' std :: getline'を使って読み込むことを検討しましたか? – user4581301
提案に基づいて質問が編集されました。私は、画面上のテキストを送信することがアプリケーションが適切に動作するためには厳密に必要ではないので、フラッシュでさらに試してみませんでした。プログラムの動作をチェックするのは最初の方法でした。私は、文字列がgetlineで分割された後に正しくキューに入れられないということに懸念しています。 – Jendker