You are currently offline, waiting for your internet to reconnect

HOWTO: VB Can Determine if Specific Windows Program Is Running

This article was previously published under Q72918
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
SUMMARY
To determine if a specific program is running, call the Windows APIfunction FindWindow.

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

NOTE: Only 16-bit implementations of VBA support the sample code in thisarticle.
MORE INFORMATION
This information is included with the Help file provided with MicrosoftProfessional Toolkit for Visual Basic version 1.0, Microsoft Visual Basicversion 2.0, and Microsoft Visual Basic version 3.0.

By calling FindWindow with a combination of a specific program's classname and/or the title bar caption, your program can determine whetherthat specific program is running.When an application is started from the Program Manager, it registers theclass name of the form. The window class provides information about thename, attributes, and resources required by your form. MDI forms in VisualBasic have ThunderMDIForm as their class name, and all other Visual Basicforms have ThunderForm as their class name.

You can determine the class name of an application by using SPY.EXE thatcomes with the Microsoft Windows Software Development Kit (SDK) version3.0 or 3.1.

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

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

Step-by-Step Example

The following example shows three ways to determine if the WindowsCalculator 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         ApplicationSciCalc            CALC.EXECalWndMain         CALENDAR.EXECardfile           CARDFILE.EXEClipboard          CLIPBOARD.EXEClock              CLOCK.EXECtlPanelClass      CONTROL.EXEXLMain             EXCEL.EXESession            MS-DOS.EXENotepad            NOTEPAD.EXEpbParent           PBRUSH.EXEPif                PIFEDIT.EXEPrintManager       PRINTMAN.EXEProgman            PROGMAN.EXE   (Windows Program manager)Recorder           RECORDER.EXEReversi            REVERSI.EXE#32770             SETUP.EXESolitaire          SOL.EXETerminal           TERMINAL.EXEWFS_Frame          WINFILE.EXEMW_WINHELP         WINHELP.EXE#32770             WINVER.EXEOpusApp            WINWORD.EXEMSWRITE_MENU       WRITE.EXE
Properties

Article ID: 72918 - Last Review: 12/12/2003 19:59:53 - Revision: 2.0

  • Microsoft Visual Basic 2.0 Standard Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 2.0 Professional Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 1.0 Standard Edition
  • kbhowto kbprogramming KB72918
Feedback