Re: MsgWaitForSingleObject and named pipe. Which event am I getting ?
[email protected] (Richard) Wed, 18 Mar 2026 17:45:16 -0000 (UTC)
| Newsgroups | comp.os.ms-windows.programmer.win32,alt.comp.os.windows-xp,alt.windows7.general |
|---|---|
| Organization | multi-cellular, biological |
| Message-ID | <[email protected]> |
"R.Wieser" <[email protected]> spake the secret code <[email protected]> thusly: >I would like to write some single-threaded code to work with a single >non-blocking, non-overlapped (incoming) named pipe, in combination with >MsgWaitForSingleObject - the latter as part of the message-loop. Well, if you want to do non-blocking I/O on Win32, that is done using FILE_FLAG_OVERLAPPED when you create the pipe. "Overlapped I/O" on Win32 just means asynchronous non-blocking I/O. >The problem is that although it looks like I can wait /something/ to happen >with the pipe, I do not see how I can get *what* is happening : A new >connection (ConnectNamedPipe), or data coming in (ReadFile). The problem is that MsgWaitForSingleObject can't wait on a pipe, it can only wait on kernel synchronization objects: events, mutexes, processes. So to use MsgWaitForSingleObject you'll need to create some events. Use one of the events in an OVERLAPPED structure that you pass to ConnectNamedPipe and use the other event in an OVERLAPPED structure that you pass to ReadFile. Wait on both events and depending on which one was triggered, you know if it was a connect or a read operation that was pending. FYI, I got this answer by literally copy/pasting your question into google's AI (gemini), but I reviewed the Win32 API documentation to verify that the suggested approach makes sense. Pay attention to the return value of ReadFile, as with asynchronous I/O it may return immediately even if no data is available. Here's google's suggested code snippet (pay attention to the lifetime of the various structures passed by address to API calls) =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= HANDLE hConnectEvent = CreateEvent(NULL, TRUE, TRUE, NULL); // Manual reset HANDLE hReadEvent = CreateEvent(NULL, TRUE, FALSE, NULL); OVERLAPPED connectOverlapped = {0}; connectOverlapped.hEvent = hConnectEvent; OVERLAPPED readOverlapped = {0}; readOverlapped.hEvent = hReadEvent; HANDLE waitHandles[2] = { hConnectEvent, hReadEvent }; BOOL pendingConnect = TRUE; // Initiate the first connection wait ConnectNamedPipe(hPipe, &connectOverlapped); // --- The Message Loop --- while (TRUE) { DWORD result = MsgWaitForSingleObject(2, waitHandles, FALSE, INFINITE, QS_ALLINPUT); if (result == WAIT_OBJECT_0) { // --- hConnectEvent triggered: A client connected! --- ResetEvent(hConnectEvent); // Start waiting for data immediately ReadFile(hPipe, buffer, BUFSIZE, NULL, &readOverlapped); pendingConnect = FALSE; } else if (result == WAIT_OBJECT_0 + 1) { // --- hReadEvent triggered: Data is ready! --- ResetEvent(hReadEvent); DWORD bytesRead; GetOverlappedResult(hPipe, &readOverlapped, &bytesRead, FALSE); // Process your data here... // Queue the next read ReadFile(hPipe, buffer, BUFSIZE, NULL, &readOverlapped); } else if (result == WAIT_OBJECT_0 + 2) { // --- Window Message arrived --- MSG msg; while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) return 0; TranslateMessage(&msg); DispatchMessage(&msg); } } } =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -- "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline> The Terminals Wiki <http://terminals-wiki.org> The Computer Graphics Museum <http://computergraphicsmuseum.org> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>