Determining the Number of Instances an Application Is Running

ID: Q99955


The information in this article applies to:
  • Microsoft Word for Windows, versions 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0


SUMMARY

Sometimes your WordBasic macro may need to determine how many instances of an application are currently running in memory.


MORE INFORMATION

Word 6.0

In Word 6.0, API calls may be avoided by displaying the active applications alphabetically in a listbox similar to the Task List. This sample macro may be enhanced to return the actual number of a specific window if that is desired.

Sub MAIN
 size = AppCount() - 1
 Dim winnames$(size)
 AppGetNames winnames$()
 SortArray winnames$()
 Begin Dialog UserDialog 420, 162, "Active Application List"
      Text 156, 14, 138, 13, "Active Applications.", .Text1
      ListBox 41, 35, 340, 84, winnames$(), .ListBox1
      OKButton 170, 133, 88, 21
 End Dialog
 Dim dlg As UserDialog
 Dialog dlg
End Sub 

Word 2.x

In Word for Windows 2.x, you can call GetModuleUsage in the Windows Kernel dynamic-link library (DLL) to determine how many instances of an application are running in memory.

The following macro example demonstrates the use of the Windows GetModuleUsage function to determine how many instances of Word for Windows are running. "MSWORD" is the module name for Word for Windows, but any other application module name can be substituted below. (For example, the module name for Microsoft Excel is "EXCEL.")

Declare Function GetModuleHandle Lib "Kernel"(ModuleName$)
As Integer
Declare Function GetModuleUsage Lib "Kernel"(hTask As
Integer) As Integer
Sub MAIN
Number = GetModuleUsage(GetModuleHandle("MSWORD"))
If number = 1 Then
  MsgBox "There is" + Str$(Number) +\ 
  " instance of Word running."
Else
  MsgBox "There are" + Str$(Number) +\ 
  " instances of Word running."
EndIf
End Sub 
The module name for an application is usually the name of the executable file.

   Module Name       Program .EXE Filename
   -----------       ---------------------
   EXCEL             EXCEL.EXE
   MSMAIL            MSMAIL.EXE
   MSACCESS          MSACCESS.EXE 
WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.


REFERENCES

"Using WordBasic," by WexTech Systems and Microsoft, pages 30-44

Additional query words: GetModuleHandle GetModuleUsage winword winword2 2.0 running declare dll

Keywords :
Version : WINDOWS:2.0,2.0a,2.0a-CD,2.0b,2.0c,6.0
Platform : WINDOWS
Issue type :


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