A few days back, in one of my projects, I needed to enumerate all the visible windows, and then needed to determine if a given application was running or not. I first struggled a little, but soon found a simple method to do that.
Enumerating the windows
EnumWindows API, I filtered out the visible windows. Next, with
GetWindowThreadProcessId, I found the process ID of that window, and with that, I found the name of the executable file.
Below is the callback function for
BOOL CALLBACK CProcessDlg::EnumWindowsProc (HWND hWnd, LPARAM lParam)
CProcessDlg *pDlg = (CProcessDlg *)lParam;
int nItem = 0;
TCHAR szWindowText [MAX_PATH];
if (!::IsWindowVisible (hWnd))
::GetWindowText (hWnd, szWindowText, MAX_PATH);
if (_tcsstr (_T("Program Manager"), szWindowText))
GetWindowThreadProcessId (hWnd, &dwProcessID);
CString strEXEName = pDlg->GetEXEName (dwProcessID);
nItem = pDlg->m_List.InsertItem (0, szWindowText);
pDlg->m_List.SetItemText (nItem, 1, strEXEName);
Finding module name
GetEXEName procedure returns the name of the executable file from its process ID:
#pragma comment(lib, "psapi.lib")
CString GetEXEName(DWORD dwProcessID)
DWORD aProcesses , cbNeeded, cProcesses;
unsigned int i;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
cProcesses = cbNeeded / sizeof(DWORD);
for (i = 0; i < cProcesses; i++)
if (aProcesses [i] == dwProcessID)
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ, FALSE, dwProcessID);
if (NULL != hProcess)
GetModuleBaseName(hProcess, hMod, szEXEName,
return CString (szEXEName);
The code is very easy to understand and follow, please see the demo project if you have any trouble using it.
I hope you find it useful.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.