-4

Hello im creating program that check for opened window name but at the moment it check it like 100 times per second and i want to check it every time the window change.I know its propably becasue of infinity loop.But im new in C++ and i have no idea how to do it, can someone help me ?

    char wnd_title[256];
    while(1) {
        HWND hwnd=GetForegroundWindow();
        GetWindowText(hwnd,wnd_title,sizeof(wnd_title));
        cout << wnd_title;
    }

Best regards.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Mariusz
  • 149
  • 2
  • 8
  • Dupe is tagged C# but the answers only use basic Win API, so they still apply. – zett42 Nov 06 '18 at 18:09
  • ehh my function work correctly i just need to write it 1 time not 1000 times, i know that the simplest answer is paste another topic , but every problem is different not the same , thats why i dont like to post something on stack :) – Mariusz Nov 06 '18 at 18:14

1 Answers1

-1

There are several things you can do to achieve that:

Best (thanks to Remy in the comments): "Another option is to use SetWinEventHook() to listen for EVENT_OBJECT_NAMECHANGE events. That is "more promising" than intercepting WM_SETTEXT messages, and less intrusive than "infiltrating"."

  • simple and boring (and it seems not really what you wanted): set a timer for the request so it is called in longer periods
  • propably the most promising one: Set up a Window hook with WH_GETMESSAGE to listen for the WM_SETTEXT which is messaged from the SetWindowTextA function
  • or go full crazy and CreateRemoteThread and infiltrate your enemy with a trusty spy.
Yastanub
  • 1,227
  • 8
  • 19
  • 2
    Another option is to use [`SetWinEventHook()`](https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setwineventhook) to listen for `EVENT_OBJECT_NAMECHANGE` events. That is "more promising" than intercepting `WM_SETTEXT` messages, and less intrusive than "infiltrating". – Remy Lebeau Nov 06 '18 at 18:41
  • @RemyLebeau good hint thanks! Must have skipped this on my quick research. – Yastanub Nov 06 '18 at 18:46