If you wish to be alerted of pending DirectMusic notification messages by a Windows event object, you must first obtain an event handle by calling the Win32 CreateEvent function. Typically you would create an auto-reset event with a call such as this:
HANDLE g_hNotify = CreateEvent(NULL, FALSE, FALSE, NULL);
Note It is not necessary to create an event in order to retrieve notification messages in your application's message loop. As long as you have requested notifications by calling the IDirectMusicPerformance::AddNotificationType method, the performance will send DMUS_NOTIFICATION_PMSG messages which can be retrieved by calling IDirectMusicPerformance::GetNotificationPMsg.
After creating the event, you assign the handle to the performance by passing it to the IDirectMusicPerformance::SetNotificationHandle method. You can use the second parameter of this method to change the default time that DirectMusic will hold onto the event if it is not retrieved; a value of 0 in this parameter indicates that the default time of 2 seconds is to be used.
In the following example, g_pPerf is a valid pointer to the IDirectMusicPerformance interface:
g_pPerf->SetNotificationHandle(g_hNotify, 0);
The following sample function executes repeatedly in its own thread, checking for signaled events and retrieving notification messages.
void WaitForEvent( LPVOID lpv)
{
DWORD dwResult;
DMUS_NOTIFICATION_PMSG* pPmsg;
char szCount[4];
while (TRUE)
{
dwResult = WaitForSingleObject(g_hNotify, 100);
while (S_OK == g_pPerf->GetNotificationPMsg(&pPmsg))
{
// Check notification type and do something in response.
.
.
.
g_pPerf->FreePMsg((DMUS_PMSG*)pPmsg);
}
}
}
This thread is executed as follows:
_beginthread(WaitForEvent, 0, NULL);
When notifications are no longer needed, the following code shuts down the thread, removes the notification handle from the performance, and destroys the event object.
_endthread();
g_pPerf->SetNotificationHandle(0, 0);
CloseHandle(g_hNotify);