I want to use a QIODevice in order to read from a unnamed pipe if data is available. I tried this with QFile:
m_pFile_Pipe = new QFile();
HANDLE hRead, hWrite;
connect(m_pFile_Pipe, SIGNAL(readyRead()), this, SLOT(OutputAvailable_QFile()));
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
CreatePipe(&hRead, &hWrite, &sa, 0);
auto fhRead = _open_osfhandle((intptr_t)hRead, _O_RDONLY);
auto OpenResult = m_pFile_Pipe->open(fhRead, QIODevice::ReadOnly);
The "raw" pipe itself works, I can read data from it. However, readyRead() is never signaled and during testing:
void TestPipe() {
char acBuffer[128];
DWORD NumBytesRead;
auto NumBytes = m_pFile_Pipe->bytesAvailable();
qDebug() << "NumBytes" << NumBytes;
ReadFile(hRead, acBuffer, sizeof(acBuffer), &NumBytesRead, NULL);
qDebug() << QString::fromUtf8(acBuffer);
while (m_pFile_Pipe->canReadLine()) {
auto out = m_pFile_Pipe->readLine(512);
qDebug() << "Line: " << out;
}
auto out_all = m_pFile_Pipe->readAll();
qDebug() << "Raw: " << out_all;
}
NumBytes was always 0, canReadLine() always returned false, and readAll() did not return. ReadFile() could read the expected data.
Is there a QOIDevice to use for anonymous pipes? The Qt docs say that QLocalSocket shall be used for named pipes, but I did not find anything about anonymous pipes.