1

I'm trying to run a thread in the background which reads the message queue from the main thread, so I can focus on installing and uninstalling hooks and responding to user actions, but I can't make the second thread poll from the main thread's message queue. I searched for a solution but didn't find any answer which solves my problem. Does anyone here know what to do? thanks in advance!

this is the code I wrote: (the Macro_Record hook increase counter everytime it is called)

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);
Monty6400
  • 39
  • 4
  • So, what happens when you run that code? Does one of the functions fail? Do you not see events? Explain yourself. – Ben Voigt Apr 28 '18 at 19:44
  • (also note that your path to python contains invalid escape sequences) – Ben Voigt Apr 28 '18 at 19:44
  • I see a message confirming the threads were attached succesfully. then when I start typing I see nothing (the hook have some couts in it so if it was working I would have seen it). about the path- you are right, I'll fix it. – Monty6400 Apr 28 '18 at 19:59
  • You cannot read messages from another thread's queue. – Raymond Chen Apr 28 '18 at 20:05
  • @RaymondChen: MSDN says "Keyboard and mouse events received by both threads are processed by the thread specified by the idAttachTo parameter until the threads are detached" which makes it sound like this should work for some messages. Of course right now the messages are just being discarded... – Ben Voigt Apr 28 '18 at 20:22
  • so there is no way to read the messages from another thread or make the hook run without reading messages? – Monty6400 Apr 28 '18 at 21:02
  • Attaching threads synchronizes input but does not change which thread processes each message. I'll file a documentation bug against MSDN. – Raymond Chen Apr 28 '18 at 21:07
  • @RaymondChen Is there any other way to make the GetMessage/PeekMessage fucntions run simultnesly with the main code on the same thread? – Monty6400 Apr 28 '18 at 21:24
  • A tought that just popped in my head- does winforms app in c# use a message loop? if so, I should be able to install hooks without calling GetMessage as the winform does that for me? – Monty6400 Apr 28 '18 at 22:32
  • The functions already run simultaneously on different threads. It's not clear what you're trying to do. – Raymond Chen Apr 28 '18 at 22:50
  • @RaymondChen I'm trying to read the message queue of my thread constantly without blocking my code so I can react to user Input and stuff. – Monty6400 Apr 29 '18 at 06:10
  • The way your program is structured, I don't see why the main thread has to install the hook. Let the second thread install the hook and process messages. – Raymond Chen Apr 29 '18 at 14:36

0 Answers0