且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C++ MSAPI 5:SetNotifyCallbackFunction 不起作用

更新时间:2022-06-09 01:05:38

两个问题:

1) SAPI 不允许您重新分配事件目标,因此行

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

pV->SetNotifyWin32Event();

总会失败.

2) 您需要泵送消息以传递事件.所以不要调用

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;
}