Click here to change the theme.

This is a console-mode program that shows a use of EnumWindows and some of the information that can be gotten using a windows handle. The EnumDesktopWindows function can be used instead of EnumWindows. This program can be compiled as-is by Visual C++ as a console program, except the GetModuleFileNameEx function uses psapi, which works for Windows NT (and 2000) only. For more samples and an excellent description of related techniques, see Microsoft KB article HOWTO: Enumerate Applications in Win32 (Q175030).

The output from this program is not especially pretty, but this is just a sample use of the functions.

#define STRICT  1 
#include <windows.h>
#include <psapi.h>		// NT only!
#include <iostream>
using namespace std;
#pragma comment(lib, "psapi")	// NT only!

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) {
	DWORD dwThreadId, dwProcessId;
	HINSTANCE hInstance;
	char String[255];
	HANDLE hProcess;
if (!hWnd)
	return TRUE;		// Not a window
if (!::IsWindowVisible(hWnd))
	return TRUE;		// Not visible
if (!SendMessage(hWnd, WM_GETTEXT, sizeof(String), (LPARAM)String))
	return TRUE;		// No window title
hInstance = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
dwThreadId = GetWindowThreadProcessId(hWnd, &dwProcessId);
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);
cout << hWnd << ' ' << dwProcessId << '\t' << String << '\t';
// GetModuleFileNameEx uses psapi, which works for NT only!
if (GetModuleFileNameEx(hProcess, hInstance, String, sizeof(String)))
	cout << String << endl;
	cout << "(None)\n";
return TRUE;

int main(int argc, char *argv[], char *envp[]) {
EnumWindows(EnumWindowsProc, NULL);
return 0;


C++ Q&A: Get the Main Window, Get EXE Name


Hosted by WinHost.com.