// Get a "snapshot" of the currently running processes in the system
hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if ( hSnap == NULL )
return;
// Very important!! Set the size of the structure so that the enumeration
// functions know which version of the structure they are working with.
process.dwSize = sizeof(PROCESSENTRY32);
f = Process32First(hSnap, &process);
// Loop through all of the processes running in this snapshot
while ( f )
{
// Is this ours?
if ( process.th32ProcessID == idProc )
{
// Yes. Get the name of it and get out of here.
CloseHandle( hSnap );
strcpy( szBuffer, process.szExeFile );
return;
}
f = Process32Next(hSnap, &process);
}
// Make sure the handle is closed.
CloseHandle( hSnap );
}
// CallBack function for the EnumWindows Windows API function.
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam )
{
char szBuffer[ 256 ];
char szFileName[ 256 ];
// Make sure this is a visible, or iconized, parent window.
if ( (GetWindowLong(hwnd,GWL_HWNDPARENT)==0) &&
(IsWindowVisible(hwnd) || IsIconic(hwnd)) )
{
GetWindowText( hwnd, szBuffer, 256 );
if ( strlen(szFileName) && strlen(szBuffer) )
{
AnsiString s = szBuffer;
s += " : ";
s += szFileName;
TMForm->ListBox1->Items->Add( s.c_str() );
}
}
// You return TRUE from a callback function to continue enumerating.
return TRUE;
}