windows 如何停止 EnumWindows 无限运行 win32
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/797967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop EnumWindows running infinitely win32
提问by s5804
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.
该代码一直有效。不知何故,我设法让 Visual C++ Express 没有在最后的 return 语句上达到断点,它似乎永远运行了。
In the example code bellow EnumWindows enumerates infinitely. How can one make it stop after all windows has been enumerated.
在下面的示例代码中,EnumWindows 无限枚举。枚举完所有窗口后,如何让它停止。
#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;
}
回答by
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?
一旦我删除了宽字符内容并添加#include <stdio.h>
以获得 printf() 声明,您的代码就对我有用。它在您的系统上产生什么输出?
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;
}
回答by Brian R. Bondy
EnumWindowsProc
should never run infinitely.
EnumWindowsProc
永远不应该无限运行。
It should run until:
它应该运行到:
- Your callback returns FALSE
- There are no more top level windows to enumerate
- 您的回调返回 FALSE
- 没有更多的顶级窗口可以枚举
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.
您的 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;
}
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.
此外,您不需要将您的增益作为 LPWSTR 施放。如果您的 buff 以某种方式是 CHAR 缓冲区,那么您需要使用 Unicode 字符集进行编译。
回答by Daniel A. White
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.
EnumWindows 一直持续到最后一个顶级窗口被枚举或回调函数返回 FALSE。
要继续枚举,回调函数必须返回 TRUE;要停止枚举,它必须返回 FALSE。
回答by Don Dickinson
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
嗯,我不明白为什么会这样。我运行它,它工作得很好。它显示了我拥有的所有窗口,然后停止了。enumWindows 将在 enumWindowsProc 返回 false(您将其编码为始终返回 true)或当它用完要枚举的顶级窗口时停止。-大学教师