Unexpected output from EnumWindows

I am trying to get a list of all windows with the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <windows.h>

using namespace std;

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
	TCHAR buffer[512];
	SendMessage(hwnd, WM_GETTEXT, 512, (LPARAM)(void*)buffer);
	cout << buffer << endl;
	return TRUE;
}

int main()
{
	EnumWindows(EnumWindowsProc, NULL);
	return 0;
}



However the output I am getting is not what I expected, I am getting the following text repeated around 40 times.

...
0031F6F0
0031F6F0
0031F6F0
0031F6F0
0031F6F0
0031F6F0
0031F6F0
...



When I ran this same code 1 year ago, I got a list of windows, does anybody know why it would now be printing out this weird hexcode?

Last edited on
It may depend on what type TCHAR represents.

Maybe try changing cout to wcout at line 10.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <windows.h>
#include <tchar.h>

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
  TCHAR buffer[512];
  SendMessage(hwnd, WM_GETTEXT, 512, (LPARAM) (void*) buffer);
  _tprintf(_T("%s\n"), buffer);
  return TRUE;
}

int main()
{
  EnumWindows(EnumWindowsProc, NULL);
  return 0;
}
Thanks guys both solutions worked. How should I go about filtering these windows?

For example here follows some of the output produced by the above code.


...
Task Switching
Microsoft Visual Studio
CiceroUIWndFrame


Battery Meter

Network Flyout
...


Out of those 5 entries, and blank spaces, only Microsoft Virtual Studio is an actual window, the rest are services I assume run in the background or perhaps have transparent 0x0 pixel windows?
Have a look here. Maybe that's more what you want.

http://simplesamples.info/windows/EnumWindows.aspx
Topic archived. No new replies allowed.