0

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
NTG
  • 73
  • 9
  • named and anonymous pipes is absolute the same in windows. anonymous(unnamed) is partial case of named (with empty or random (in old windows versions) name. if `QLocalSocket` can be used with named pipes - it can be used with anonymous of course. – RbMm May 13 '18 at 20:30
  • Well, QLocalSocket takes the name to the pipe, so I do not know how to pass it the unamed one. – NTG May 13 '18 at 22:04
  • *`QLocalSocket` shall be used for named pipes* - how i understand docs - this is false. the `QLocalSocket` used pipes as internal implementation, but you can not assign external pipe for it. i don know QT simply note that exist only one type of pipes in windows - pipes. which name of pipe (empty it or not) - not affect it functional. so named and anonymous pipes - the same - not different things – RbMm May 13 '18 at 22:20
  • I don't really know about windows pipes, but what is used with Qt to detect events on HANDLEs is [QWinEventNotifier](https://doc.qt.io/qt-5/qwineventnotifier.html) - you might be able to detect changes by using such a notifier and connecting it to the device – Felix May 17 '18 at 19:58
  • Also, since we are talking about IO - [QSocketNotifier](https://doc.qt.io/qt-5/qsocketnotifier.html#details) may be useful as well – Felix May 17 '18 at 20:00

0 Answers0