How to stop EnumWindows without win32 restrictions

The code worked all the time. Somehow I can get Visual C ++ Express to not hit the breakpoint in the last return expression, and it seems to have worked forever.

In the example below, EnumWindows code enumerates endlessly. How can I stop it after all the windows have been listed.

#include <Windows.h>

BOOL CALLBACK EnumWindowsProc(HWND hWnd, long lParam) {
    TCHAR buff[255];

    if (IsWindowVisible(hWnd)) {
        GetWindowText(hWnd, (LPWSTR) buff, 254);
        printf("%S\n", buff);
    }
    return TRUE;
}

int _tmain(int argc, _TCHAR* argv[]) {
    EnumWindows(EnumWindowsProc, 0);
    return 0;
}
+3
source share
4 answers

Your code works for me as soon as I removed the widescreen material and added #include <stdio.h>it to get printf () declaration. What result does it produce in 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;
}
+7
source

EnumWindowsProc should never work endlessly.

It should work until:

  • FALSE

, - .

printf % s % 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;
}

LPWSTR. - CHAR, Unicode.

+3

:

EnumWindows , FALSE.

, ; , FALSE.

+1

Hmm, I don’t understand why. I ran and everything went fine. he displayed all the windows that I have and then stopped. enumWindows will stop when either enumWindowsProc returns false (you encoded it to always return true), or when it runs out of top-level windows for listing. -don

+1
source

Source: https://habr.com/ru/post/1707254/


All Articles