How VB Can Determine If a Specific Windows Program Is Running
This article is reprinted from the Microsoft Knowledge Base. To view the article, maximize your help window. This information applies to Visual Basic for Windows, versions 2.0 and 3.0.
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, or
caption, is given by the lpCaption parameter. If the returned value is
zero, the application is not running.
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. All
Visual Basic forms have a class name of "ThunderForm." 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
The following example shows three ways to determine if the Windows
Calculator is running. To create the program, do the following:
1. Run Visual Basic, or from the File menu, select New Project
(ALT, F, N) if Visual Basic is already running. Form1 will be
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. Note that the Declare statement should be entered on
just one 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
Sub Form_Click ()
lpClassName$ = "SciCalc"
lpCaption$ = "Calculator"
Print "Handle = ";FindWindow(lpClassName$, 0&)
Print "Handle = ";FindWindow(0&, lpCaption$)
Print "Handle = ";FindWindow(lpClassName$,lpCaption$)
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
Class Name Application
Progman PROGMAN.EXE (Windows Program manager)