How to Determine the Number of VB Applications Running at Once

Last reviewed: June 21, 1995
Article ID: Q84836
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0

SUMMARY

To determine the total number of Microsoft Visual Basic for Windows applications running at any given time, you can use the Microsoft Windows API functions GetModuleHandle and GetModuleUsage.

MORE INFORMATION

The following code fragment demonstrates a technique to find the total number of Visual Basic for Windows applications currently executing by determining the number of instances of the Visual Basic run-time module (VBRUN100.DLL) with the Windows API functions GetModuleHandle and GetModuleUsage. Remember that Visual Basic for Windows itself is not counted; only applications created with Visual Basic for Windows are included.

Steps to Create Example Program

  1. Start several Visual Basic for Windows applications and leave them running.

  2. Run Visual Basic for Windows, or from the File menu, choose New Project (press ALT, F, N) if Visual Basic for Windows is already running. Form1 is created by default.

  3. Enter the following Windows API function declarations into the General Declarations section of Form1:

       Declare Function GetModuleUsage% Lib "kernel" (ByVal hModule%)
       Declare Function GetModuleHandle% Lib "kernel" (ByVal FileName$)
    
    

  4. Place a command button (Command1) on Form1. Double-click that button to open the Code window. In the Command1_Click procedure, add the following code:

       Sub Command1_Click ()
         msg$ = "Number of executing VB Apps: "
    
         hModule% = GetModuleHandle("VBRUN300.DLL")
           ' For Visual Basic versions 1.0 and 2.0 for Windows, use
           ' VBRun100.DLL and VBRun2.00.DLL respectively.
         nInstances% = GetModuleUsage(hModule%)
    
         msg$ = msg$ + Str$(nInstances%)
         MsgBox msg$
       End Sub
    
    

  5. From the File menu, choose Make EXE File.

  6. Press the F5 key to run the file.

  7. Click the command button.

A message box displays the total number of executing Visual Basic for Windows applications.

NOTE: This program itself will count as one application.


Additional reference words: 1.00 2.00 3.00
KBCategory: kbui kbprg kbcode
KBSubcategory: APrgWindow


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: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.