ACC: How to Determine If a Specific Windows Program Is Running

Last reviewed: November 21, 1997
Article ID: Q88167
The information in this article applies to:
  • Microsoft Access versions 2.0, 7.0, 97

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

There may be times when you want only one instance of an application to run under Microsoft Windows. For example, if you add a command button to a form that starts the Windows Calculator (Calc.exe) program, the user can start many instances of Calculator. This is an inefficient use of memory and system resources. Through the use of a Windows application programming interface (API) function, Microsoft Access can determine if Calculator is already running.

The API function used to determine if a specific program is running is called 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.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access version 2.0. For more information about Access Basic, please refer to the "Building Applications" manual.

MORE INFORMATION

When a program is started from an icon or the command line, it registers the class name of its main window. The window class provides information about the name, attributes, and resources required by the program. The Microsoft Access window has a class name of "OMain." Additional command class names are provided at the end of this article.

By calling FindWindow() with a combination of a specific program's class name or the title bar caption, Microsoft Access can determine whether that specific program is running.

You can determine the class name of an application by using Spy.exe, which is supplied with the Microsoft Windows Software Development Kit (SDK) version 3.1. or the Microsoft Win32 SDK.

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.

The following example shows three ways to determine if the Windows Calculator is running.

NOTE: You may have some Microsoft Windows API functions defined in an existing Microsoft Access library; therefore, your declarations may be duplicates. If you receive a duplicate procedure name error message, remove or comment out the declarations statement in your code.

  1. Create a module and type the following line in the Declarations section.

    NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.

        In version 7.0 and 97:
    

          Option Explicit
          Declare Function FindWindow Lib "user32" Alias "FindWindowA" (_
    
                           ByVal lpClassName as Any,_
                           ByVal lpWindowName as Any) As Long
    
        In version 2.0:
    
          Option Explicit
          Declare Function FindWindow% Lib "user" (_
                           ByVal lpClassName As Any,_
                           ByVal lpCaption As Any)
    
    

  2. Type the following procedure:

          Function CalculatorUp ()
    

             Const lpClassName = "SciCalc"
             Const lpCaption = "Calculator"
    
             'This demonstrates three different ways to call FindWindow:
                '1. The ClassName only.
                '2. The Caption only.
                '3. Both the ClassName and the Caption
    
             MsgBox "Calculator Handle = " & FindWindow(lpClassName, 0&)
             MsgBox "Calculator Handle = " & FindWindow(0&, lpCaption)
             MsgBox "Calculator Handle = " & FindWindow(lpClassName, _
                   lpCaption)
    
             'This function could return the handle of a window.
             CalculatorUp = FindWindow(lpClassName, 0&)
          End Function
    
    

  3. To test this function, start Calculator, type the following line in the Debug window (or Immediate window in version 2.0), and then press ENTER.

           ?CalculatorUp()
    

    Note that three message boxes open, each displaying the handle to the Calculator window. If Calculator is not running, each message box will display a zero.

Below are some class names of some common Windows applications:

   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

   Windows 95 Only:

   MSPaintApp         PBRUSH.EXE
   ExploreWClass      EXPLORER.EXE
   WordPadClass       WORDPAD.EXE

REFERENCES

For more information about the Class Names for the Office 97 Applications, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q169240
   TITLE     : Window Class Names for the Office 97 Applications

"Programming Windows: the Microsoft Guide to Writing Applications for Windows 3," by Charles Petzold, Microsoft Press, 1990

"Microsoft Windows 3.1 Programmer's Reference," Volume 2

"Microsoft Win32 Sofware Development Kit"


Additional query words:
Keywords : kbprg
Version : 2.0 7.0 97
Platform : WINDOWS
Hardware : x86
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: November 21, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.