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

ID: Q210605


The information in this article applies to:
  • Microsoft Access 2000

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

This article applies to a Microsoft Access database (.mdb) and a Microsoft Access project (.adp).


SUMMARY

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

The API function used to determine whether 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.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp


MORE INFORMATION

When you start a program 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, 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 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. In a new database, create a module and type the following lines in the Declarations section:


  2. 
    Option Explicit
    
    Option Compare Database
       Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
       (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
     
  3. Type the following procedure:


  4. 
    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, _
             VBNullString)
       MsgBox "Calculator Handle = " & FindWindow(VBNullString, _
             lpCaption)
       MsgBox "Calculator Handle = " & FindWindow(lpClassName, _
             lpCaption)
    
       'This function could return the handle of a window.
       CalculatorUp = FindWindow(lpClassName, 0&)
    End Function
     
  5. To test this function, start Calculator, type the following line in the Immediate window, 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 "0".


The following are class names of some common Windows applications:


   Class Name         Application
   -------------------------------
   SciCalc            CALC.EXE
   Notepad            NOTEPAD.EXE
   Solitaire          SOL.EXE
   MW_WINHELP         WINHELP.EXE
   MSPaintApp         PBRUSH.EXE
   ExploreWClass      EXPLORER.EXE
   WordPadClass       WORDPAD.EXE 


REFERENCES

For more information, see the Microsoft Win32 Software Development Kit.

Additional query words:

Keywords : kbprg kbdta AccCon
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: September 10, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.