How to stop EnumWindows running infinitely win32

后端 未结 4 1815
囚心锁ツ
囚心锁ツ 2021-01-06 19:37

The code worked all along. Somehow I manage to get Visual C++ Express not hit the break point on the final return statement and it appeared to run for ever.

In the e

相关标签:
4条回答
  • 2021-01-06 20:08

    hmm, i don't get why it would. i ran it and it worked just fine. it displayed all of the windows i have and then stopped. enumWindows will stop when either the enumWindowsProc returns false (you have it coded to always return true) or when it runs out of top-level windows to enumerate. -don

    0 讨论(0)
  • 2021-01-06 20:13

    From the documentation:

    EnumWindows continues until the last top-level window is enumerated or the callback function returns FALSE.

    To continue enumeration, the callback function must return TRUE; to stop enumeration, it must return FALSE.

    0 讨论(0)
  • 2021-01-06 20:19

    EnumWindowsProc should never run infinitely.

    It should run until:

    • Your callback returns FALSE
    • There are no more top level windows to enumerate

    So I suspect it appears to be running infinitely for you because of memory corruption or a memory access violation.

    Your printf should be using %s not %S.

    BOOL CALLBACK EnumWindowsProc(HWND hWnd, long lParam) {
        TCHAR buff[255];
    
        if (IsWindowVisible(hWnd)) {
            GetWindowText(hWnd, (LPWSTR) buff, 254);
            printf("%s\n", buff);//<--- %s means use TCHAR* which is WCHAR* in your case
        }
        return TRUE;
    }
    

    Also you shouldn't need to be casting your buff as a LPWSTR. If your buff is somehow a CHAR buffer then you need to compile with the Unicode character set.

    0 讨论(0)
  • 2021-01-06 20:26

    Your code works for me, once I removed the wide-character stuff and added #include <stdio.h> to get the printf() declaration. What output does it produce on your system?

    The code that works for me is:

    #include <windows.h>
    #include <stdio.h>
    
    BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) {
        char buff[255];
    
        if (IsWindowVisible(hWnd)) {
            GetWindowText(hWnd, (LPSTR) buff, 254);
            printf("%s\n", buff);
        }
        return TRUE;
    }
    
    int main() {
        EnumWindows(EnumWindowsProc, 0);
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题