如果数据可用,我想使用QIODevice来读取未命名的管道。我用 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);
“原始”管道本身可以工作,我可以从中读取数据。但是,在测试期间,readyRead() 永远不会发出信号:
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 始终为 0,canReadLine() 始终返回 false,而 readAll() 未返回。ReadFile() 可以读取预期的数据。
是否有用于匿名管道的QOIDevice ?Qt 文档说QLocalSocket应用于命名管道,但我没有找到任何关于匿名管道的信息。