QSocketNotifier with Win32 NamedPipes
-
Hey,
I want to use Win32 NamedPipes with QSocketNotifier, but I can't get it working.
Here is the reproducer: https://github.com/clemenswasser/named_pipes_socket_notifier
Writing into the pipe and reading from the other end works correctly, but listening with the QSocketNotifier somehow doesn't work.
I would expect it to print:Sent message Got message
But that doesn't happen. Does anyone know what I'm doing wrong?
-
Hi and welcome to devnet,
Depending on what you want to achieve, would QLocalSocket be easier to use ?
-
@SGaist
No, for the project I'm working on we already have a NamedPipe HANDLE which we create ourselves. Using QLocalSocket is not easily possible. I just want to know what I'm doing wrong or what QLocalSocket (which uses NamesPipes as far as I know) does differently... -
@Clemens-Wasser I could be off-base but when using named pipes you should be using the file IO calls, not socket IO, right?
named pipes are essentially files, not sockets.
-
@Kent-Dorfman Yes NamedPipes use file IO calls: https://learn.microsoft.com/en-us/windows/win32/ipc/named-pipe-operations
As you can see from my example, I'm using file IO calls and that works as expected.
QLocalSockets use NamedPipes on Windows (https://doc.qt.io/qt-6/qlocalsocket.html#details) and that should work for QSocketNotifier.
The docs for QSocketNotifier explicitly mention that:Although the class is called QSocketNotifier, it is normally used for other types of devices than sockets
(https://doc.qt.io/qt-6/qlocalsocket.html#details)
I've also seen many examples where they are using stdin with QSocketNotifier, so using file IO shoud be fine. -
@SGaist you suggested using QLocalSockets for this.
I've updated my example with an implementation using QLocalSockets, but that one also doesn't work? :(
QLocalSocket also uses NamedPipes on Windows, so maybe QSocketNotifier just doesn't support NamedPipes? -
Use QWinEventNotifier or something like that to watch for the Windows events in a HANDLE.
-
@kuzulis That also doesn't work for me (I modified my example, but the callback still doesn't get called). QWinEventHandler (as the name would suggest) only seems to work for event objects and not for NamedPipes.
-
kuzulis Qt Champions 2020replied to Clemens Wasser on 10 Jul 2023, 22:40 last edited by kuzulis 7 Oct 2023, 22:42
sorry?
You need to use the QWinEventNotifier in the ReadFile, WriteFile, and ConnectToPipe functions, just fill the OVERLAPPED.hEvent field with the CreateEvent.
OR, you can copy/paste the private QWinOverlappedIONotifier which was deprecated a while time ago to watch an events directly on a HANDLE. See, e.g.sources of the QSerialPort, as I remember, as an example.
9/9