Step 5: Handle the Play Notifications

Notifications are received in the form of signaled events in the message loop within the WinMain function. The following fragment illustrates how the loop might be written in order to intercept signaled events as well as standard messages:

BOOL Done = FALSE;

while (!Done)

{

DWORD dwEvt = MsgWaitForMultipleObjects(

NUMEVENTS, // How many possible events

rghEvent, // Location of handles

FALSE, // Wait for all?

INFINITE, // How long to wait

QS_ALLINPUT); // Any message is an event

// WAIT_OBJECT_0 == 0 but is properly treated as an arbitrary

// index value assigned to the first event, therefore we subtract

// it from dwEvt to get the zero-based index of the event.

dwEvt -= WAIT_OBJECT_0;

// If the event was set by the buffer, there's input

// to process.

if (dwEvt < NUMEVENTS)

{

StreamToBuffer(dwEvt); // copy data to output stream

}

// If it's the last event, it's a message

else if (dwEvt == NUMEVENTS)

{

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

{

if (msg.message == WM_QUIT)

{

Done = TRUE;

}

else

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

}

} // end message processing

} // while (!Done)