Retrieving the Names of Simultaneous Tasks Under Windows

Last reviewed: July 23, 1997
Article ID: Q80124
3.00 3.10 WINDOWS kbprg

The information in this article applies to:

  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.0 and 3.1

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.

In Windows 3.0, 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.

In Windows 3.1, however, an application should instead use the ToolHelp Library functions. More specifically, use TaskFirst(), TaskNext(), ModuleFirst(), and ModuleNext().

NOTE: Since the ToolHelp functions work in Windows 3.0 and since ToolHelp is itself a redistributable library, these functions can be safely used in Windows 3.0 also.

MORE INFORMATION

The most efficient way to retrieve the name of each task running under Windows 3.0 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);
    /* Where: hWnd is the original/current window. GW_HWNDFIRST will
       return the first sibling window for a child window, otherwise
       it returns the first top-level window in the system's list of
       top-level windows */
    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().


Additional reference words: 3.00 3.10
KBCategory: kbprg
KBSubcategory: KrTsksIns
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 23, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.