views:

81

answers:

2
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)

If hWnd is NULL, PeekMessage retrieves messages for any window that belongs to the current thread, and any messages on the current thread's message queue whose hwnd value is NULL (see the MSG structure). Therefore if hWnd is NULL, both window messages and thread messages are processed.

Anyone knows whether messages received via named pipe are included in window messages and thread messages?

+1  A: 

Definitely not. Named pipes do not send window messages.

The thread messages in this context are special and have nothing to do with named pipes.

Use MsgWaitForMultipleObjects instead.

CODE SAMPLE:

void MessageLoop(HANDLE hNamedPipe)
{
    do {
        DWORD res = MsgWaitForMultipleObjects(1, &hNamedPipe, INFINITE, QS_ALLEVENTS, MWMO_INPUTAVAILABLE);
        if (res == WAIT_OBJECT_0) {
           /* Handle named pipe -- at this point ReadFile will not block */
        } else if (res == WAIT_OBJECT_0 + 1) {
           MSG msg;
           if (!GetMessage(&msg, NULL, 0, 0))
              break; /* WM_QUIT */
           TranslateMessage(&msg);
           DispatchMessage(&msg);
        }
    } while (1);
}
Joshua
Can you provide a simple demo how to read a data structure from pipe by `MsgWaitForMultipleObjects`?
wamp
@Joshua,thanks !But a few wonders,What's `WAIT_OBJECT_0` in your code sample? How should I retrieve a specific **structure** in pipe?
wamp
Pipes don't convey structures. They convey bytes. And WAIT_OBJECT_0 just means the first object in the array of wait handles is in an alerted state - in this case, the array of wait handles only has one object, hNamedPipe.
bdonlan
+1  A: 

No; windows messages and named pipes are completely unrelated. You would need to use the MsgWaitForMultipleObjectsEx function to wait for either an incoming message or a message on the named pipe.

Note that MsgWaitForMultipleObjectsEx doesn't actually retrieve the message; check its return value to see if there's a windows message or data on the named pipe, then use GetMessage or ReadFile as appropriate.

bdonlan
Can you provide a simple demo how to read a data structure from pipe by `MsgWaitForMultipleObjects`, and how to write to the pipe? I used `ReadFile` and `WriteFile` but it can only deal with strings.
wamp