HOWTO: VB Can Determine if Specific Windows Program Is Running

Last reviewed: September 29, 1997
Article ID: Q72918
The information in this article applies to:
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0
  • Microsoft Visual Basic programming system for Windows, version 1.0

SUMMARY

To determine if a specific program is running, call the Windows API function FindWindow.

FindWindow returns the handle of the window whose class is given by the lpClassname parameter and whose window name (caption), is given by the lpCaption parameter. If the returned value is zero, the application is not running.

NOTE: Only 16-bit implementations of VBA support the sample code in this article.

MORE INFORMATION

This information is included with the Help file provided with Microsoft Professional Toolkit for Visual Basic version 1.0, Microsoft Visual Basic version 2.0, and Microsoft Visual Basic version 3.0.

By calling FindWindow with a combination of a specific program's class name and/or the title bar caption, your program can determine whether that specific program is running.

When an application is started from the Program Manager, it registers the class name of the form. The window class provides information about the name, attributes, and resources required by your form. MDI forms in Visual Basic have ThunderMDIForm as their class name, and all other Visual Basic forms have ThunderForm as their class name.

You can determine the class name of an application by using SPY.EXE that comes with the Microsoft Windows Software Development Kit (SDK) version 3.0 or 3.1.

If the window has a caption bar title, you can also use the title to locate the instance of the running application. This caption text is valid even when the application is minimized to an icon.

Because another instance of your Visual Basic program will have the same class name and may have the same title bar caption, you must use dynamic data exchange (DDE) to determine if another instance of your Visual Basic program is running. (This DDE technique is not shown in this article).

Step-by-Step Example

The following example shows three ways to determine if the Windows Calculator is running. To create the program, do the following:

  1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.

  2. Declare the Windows API function FindWindow in the Global declarations section of Form1. The variables are declared as "Any" because you can pass either a pointer to a string, or a NULL (or 0&) value. You are responsible for passing the correct variable type.

          ' Enter the following Declare statement on one, single line:
          Declare Function FindWindow% Lib "user" (ByVal lpClassName As Any,
    
             ByVal lpCaption As Any)
    
    

  3. Add the following code to the form's Click event. This example demonstrates how you can find the instance of the application with a combination of the class name and/or the window's caption. In this example, the application will find an instance of the Windows calculator (CALC.EXE).

          Sub Form_Click ()
             lpClassName$ = "SciCalc"
             lpCaption$ = "Calculator"
       
             Print "Handle = ";FindWindow(lpClassName$, 0&)
             Print "Handle = ";FindWindow(0&, lpCaption$)
             Print "Handle = ";FindWindow(lpClassName$,lpCaption$)
          End Sub
    
    

  4. Run this program with CALC.EXE running and without CALC.EXE running. If CALC.EXE is running, your application will print an arbitrary handle. If CALC.EXE is not running, your application will print zero as the handle.

Below are some class names of applications that are shipped with Windows:

Class Name         Application
SciCalc            CALC.EXE
CalWndMain         CALENDAR.EXE
Cardfile           CARDFILE.EXE
Clipboard          CLIPBOARD.EXE
Clock              CLOCK.EXE
CtlPanelClass      CONTROL.EXE
XLMain             EXCEL.EXE
Session            MS-DOS.EXE
Notepad            NOTEPAD.EXE
pbParent           PBRUSH.EXE
Pif                PIFEDIT.EXE
PrintManager       PRINTMAN.EXE
Progman            PROGMAN.EXE   (Windows Program manager)
Recorder           RECORDER.EXE
Reversi            REVERSI.EXE
#32770             SETUP.EXE
Solitaire          SOL.EXE
Terminal           TERMINAL.EXE
WFS_Frame          WINFILE.EXE
MW_WINHELP         WINHELP.EXE
#32770             WINVER.EXE
OpusApp            WINWORD.EXE
MSWRITE_MENU       WRITE.EXE
Keywords          : kbprg kbfasttip
Technology        : kbvba
Version           : WINDOWS:2.0 3.0
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


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: September 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.