C++ MSAPI 5: SetNotifyCallbackFunction not working

后端 未结 1 1160
栀梦
栀梦 2020-12-21 05:10

So I\'ve tried the MSAPI 5.4 TTS with event example. Now I create an cmd prompt app that utilize the SetNotifyCallbackFunction but the function that I\'ve pass is not being

相关标签:
1条回答
  • 2020-12-21 05:11

    Two problems:

    1) SAPI doesn't let you reassign event targets, so the line

    pV->SetNotifyWin32Event();
    

    will always fail.

    2) You need to pump messages to get events delivered. So instead of calling

    pV->WaitUntilDone(INFINITE);
    

    you need to get the handle and pump messages until the event is set:

    HANDLE hWait = pV->SpeakCompleteEvent();
    WaitAndPumpMessagesWithTimeout(hWait, INFINITE);
    
    HRESULT WaitAndPumpMessagesWithTimeout(HANDLE hWaitHandle, DWORD dwMilliseconds)
    {
        HRESULT hr = S_OK;
        BOOL fContinue = TRUE;
    
        while (fContinue)
        {
            DWORD dwWaitId = ::MsgWaitForMultipleObjectsEx(1, &hWaitHandle, dwMilliseconds, QS_ALLINPUT, MWMO_INPUTAVAILABLE);
            switch (dwWaitId)
            {
            case WAIT_OBJECT_0:
                {
                    fContinue = FALSE;
                }
                break;
    
            case WAIT_OBJECT_0 + 1:
                {
                    MSG Msg;
                    while (::PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
                    {
                        ::TranslateMessage(&Msg);
                        ::DispatchMessage(&Msg);
                    }
                }
                break;
    
            case WAIT_TIMEOUT:
                {
                    hr = S_FALSE;
                    fContinue = FALSE;
                }
                break;
    
            default:// Unexpected error
                {
                    fContinue = FALSE;
                    hr = E_FAIL;
                }
                break;
            }
        }
        return hr;
    }
    
    0 讨论(0)
提交回复
热议问题