Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ SetWinEventHook不捕获任何事件_C++_Winapi_Event Handling_Event Hooking - Fatal编程技术网

C++ SetWinEventHook不捕获任何事件

C++ SetWinEventHook不捕获任何事件,c++,winapi,event-handling,event-hooking,C++,Winapi,Event Handling,Event Hooking,这是这个问题的后续问题: 我尝试使用Winuser API中的SetWinEventHook函数捕捉alt tab开关菜单打开(并退出)的时刻但是,钩子不会捕获任何事件(例如最小化窗口),因此不会调用HandleWinEvent 下面的代码深受上提供的代码的启发 我正在使用Windows XP和MinGW/GCC编译器,版本4.5。来自MSDN:调用SetWinEventHook的客户端线程必须具有消息循环才能接收事件。主线程等待“q”按钮,但不运行消息循环。如果没有消息循环,则无法获取事件。

这是这个问题的后续问题:

我尝试使用Winuser API中的SetWinEventHook函数捕捉alt tab开关菜单打开(并退出)的时刻但是,钩子不会捕获任何事件(例如最小化窗口),因此不会调用HandleWinEvent

下面的代码深受上提供的代码的启发


我正在使用Windows XP和MinGW/GCC编译器,版本4.5。

来自MSDN:调用SetWinEventHook的客户端线程必须具有消息循环才能接收事件。主线程等待“q”按钮,但不运行消息循环。

如果没有消息循环,则无法获取事件。
#ifndef _WIN32_WINNT
    #define _WIN32_WINNT 0x0500
#endif

#ifndef WINVER
    #define WINVER 0x0501
#endif

#include "conio.h"
#include <windows.h>
#include <iostream>


// Global variable.
HWINEVENTHOOK g_hook;

// Prototype
void HandleWinEvent(HWINEVENTHOOK , DWORD , HWND ,
                         LONG , LONG ,
                         DWORD , DWORD );

// Initializes COM and sets up the event hook.
//
void InitializeMSAA()
{
    CoInitialize(NULL);
    g_hook = SetWinEventHook(
        EVENT_MIN ,EVENT_MAX,  // Range of events .
        NULL,                                          // Handle to DLL.
        HandleWinEvent,                                // The callback.
        0, 0,              // Process and thread IDs of interest (0 = all)
        WINEVENT_OUTOFCONTEXT ); // Flags.
}

// Unhooks the event and shuts down COM.
//
void ShutdownMSAA()
{
    UnhookWinEvent(g_hook);
    CoUninitialize();
}

// Callback function that handles events.
//
void HandleWinEvent(HWINEVENTHOOK hook, DWORD event, HWND hwnd,
                         LONG idObject, LONG idChild,
                         DWORD dwEventThread, DWORD dwmsEventTime)
{
    std::cout << std::hex << event ; // desperate attempt to see if any event is caught 

    if (event == EVENT_SYSTEM_SWITCHSTART)
    {
        std::cout << "Begin" ;
    }
    else if (event == EVENT_SYSTEM_SWITCHEND)
    {
        std::cout << "End ";
    }
}



int main()
{
    InitializeMSAA();
    while( getch()!= 'q' ){;}
    ShutdownMSAA();
    return 0;
}
g++ -o alttab main.cpp -luser32 -lole32