如何使用GetMessage读取另一个Thread的消息队列

时间:2018-04-28 19:23:21

标签: windows c++11 visual-c++ hook windows-api-code-pack

我正在尝试在后台运行一个从主线程读取消息队列的线程,所以我可以专注于安装和卸载钩子并响应用户操作,但我无法进行第二次线程轮询主线程的消息队列。 我搜索了一个解决方案,但没有找到任何解决我的问题的答案。 这里有谁知道该怎么办? 提前谢谢!

这是我写的代码:(每次调用时,Macro_Record钩子都会增加计数器)

int counter = 0;
MSG stam;

void Init(DWORD main_id)

{
     DWORD cur_id = GetCurrentThreadId();
     int result = AttachThreadInput(main_id, cur_id, true);
     if (!result)
     {
         cout << "Threads not attached " << result << endl;
     }
     else
     {
         cout << "Threads attached " << result << endl;
     }
     while (GetMessage(&stam, NULL, 0, 0));
}


int main()
{
    DWORD id = GetCurrentThreadId();
    HANDLE hthread;
    hthread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Init,(LPVOID)id, 0, NULL);
    if (hthread == NULL)
    {
        cout << "Thread failed" << endl;
    }
    else
    {
        cout << "Thread Success" << endl;
    }
    /*string ans;
    cout << "Enter path to file you want to open" << endl;
    getline(cin, ans);*/
    File_Run* calc= new File_Run("C:\python27\python.exe", "open");
    Macro calc_macro(calc, false, false);
    recording = &calc_macro;
    HHOOK record_handle =SetWindowsHookEx(WH_KEYBOARD_LL, Macro_Record, NULL, NULL);
    if (record_handle == NULL)
    {
        cout << "Install record failed" << endl;
        cout << GetLastError() << endl;
    }
    else
    {
        cout << "Record success, start typing" << endl;
        while (counter < 8);

0 个答案:

没有答案