INF: Retrieving the Names of Simultaneous Tasks Under Windows

ID Number: Q80124

3.00

WINDOWS

Summary:

There are situations when it is necessary for an application to obtain

a list of all applications that are running in the Windows environment

at a particular time. Instead of using the EnumWindows() function and

an application-supplied callback function to enumerate all parent

windows, the application can retrieve a handle to the first window in

the task list and walk through the list to obtain the names of all

windows in the task list.

More Information:

The most efficient way to retrieve the name of each task running under

Windows is to use the GetWindow() function. GetWindow(hwnd,

GW_HWNDFIRST) provides the handle to the first window in the task

list. The application can walk through the task list by calling

GetWindow(hwndCurrent, GW_HWNDNEXT). The following example

demonstrates how to obtain a handle to each top-level window. The

GetWindowText() function provides the name of each window from its

handle.

hwndNext = GetWindow(hWnd, GW_HWNDFIRST);

while (hwndNext)

{

if ((hwndNext != hWnd) && // Do not get this application's

// name.

(IsWindowVisible(hwndNext)) &&

(!GetWindow(hwndNext, GW_OWNER)))

{

if (GetWindowText(hwndNext, (LPSTR)szTemp, sizeof(szTemp)))

{

// This is a valid top-level window handle.

// Its name is in szTemp...

}

}

hwndNext = GetWindow(hwndNext, GW_HWNDNEXT);

}

The code above will retrieve the name of each visible window. To also

retrieve the names of invisible windows, remove the call to

IsWindowVisible().