SetWinEventHook in a GUI Application - how to exit GetMessage

I have this code from an answer on stackOF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
HWINEVENTHOOK LHook = 0;

void CALLBACK WinEventProc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime) {
  IAccessible* pAcc = NULL;
  VARIANT varChild;
  if ((AccessibleObjectFromEvent(hwnd, idObject, idChild, &pAcc, &varChild) == S_OK) && (pAcc != NULL)) {
    char className[50];
    if (GetClassName(hwnd, className, 50) && strcmp(className, "Chrome_WidgetWin_1") == 0) {
      BSTR bstrName = nullptr;
      if (pAcc->get_accName(varChild, &bstrName) == S_OK) {
        if (wcscmp(bstrName, L"Address and search bar") == 0) {
          BSTR bstrValue = nullptr;
          if (pAcc->get_accValue(varChild, &bstrValue) == S_OK) {
            printf("URL change: %ls\n", bstrValue);
            SysFreeString(bstrValue);
          }
        }
        SysFreeString(bstrName);
      }
      pAcc->Release();
    }
  }
}

void Hook() {
    if (LHook != 0) return;
    CoInitialize(NULL);
    LHook = SetWinEventHook(EVENT_OBJECT_FOCUS, EVENT_OBJECT_VALUECHANGE, 0, WinEventProc, 0, 0, WINEVENT_SKIPOWNPROCESS);
}

void Unhook() {
    if (LHook == 0) return;
    UnhookWinEvent(LHook);
    CoUninitialize();
}


int main(int argc, const char* argv[]) {
    MSG msg;
    Hook();

    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    Unhook();

    return 0;
}


I am testing this in a gui app where this code would run in a different thread.
The function in the main would be used in the function like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void startThread() {
    MSG msg;
    Hook();

    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);

        printf("in the loop");
         
        if(a_stop_variable == true)
            break;
    }

    Unhook();
}


The ideal is to pass this function to a thread and when the a_stop_variable is set to true it will just exit the function. But I noticed the code never leaves the call to code]GetMessage[/code] because the prinf function is never called.

An option is to leave the thread with this function run as long as the application is running, but I am not sure how that would impact the cpu usage.
Last edited on
I am testing this in a gui app where this code would run in a different thread.


You know, each thread needs it's own message loop.

There are very few cases where one needs to split GUI code to more than 1 thread.
A "standard" approach is to put GUI logic into one thread, additional threads usually do non GUI stuff such as computation or time consuming operations.

I think you need to change design of your program.

Other than that, native C events and handlers are tricky to implement in C++ program (using classes).
Topic archived. No new replies allowed.