SetWinEventHook和GetMessage c ++

时间:2016-08-10 08:25:16

标签: c++

我想用SetWinEventHook设置一些事件并等待。 在五个事件之后,我想退出消息循环。问题是代码卡在getMessage中而没有进入循环。 每当发生事件时都会正确调用Callback函数。

HWINEVENTHOOK hEvent = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, NULL,
    WinEventProcCallback, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);

HWINEVENTHOOK hWinEventHook0 = SetWinEventHook(
    EVENT_OBJECT_CREATE, EVENT_OBJECT_CREATE,
    NULL, WinEventProcCallback, 0, 0,
    WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);

HWINEVENTHOOK hWinEventHook1 = SetWinEventHook(
    EVENT_OBJECT_DESTROY, EVENT_OBJECT_DESTROY,
    NULL, WinEventProcCallback, 0, 0,
    WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);

    while (ret = GetMessage(&msg, NULL, 0, 0) > 0) {
        if (ret == -1)
        {
            return;
        }
        if (cont >= 5) break;
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        cont++;
        std::cout << "Reached : " << cont << std::endl;
    }

我怎么解决这个问题? 提前谢谢

2 个答案:

答案 0 :(得分:1)

if (ret = GetMessage(&msg, NULL, 0, 0) > 0)

等于

if (ret = (GetMessage(&msg, NULL, 0, 0) > 0))

答案 1 :(得分:0)

while (ret = GetMessage(&msg, NULL, 0, 0) > 0) {
    if (ret == -1)
    {
        return;
    }

在这种情况下,if语句不会为true,因此您将无法返回。 ret不能同时大于0和-1