How to Determine the Number of VB Applications Running at Once

ID: Q84836


The information in this article applies to:


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 query words: 2.00 3.00


Keywords          : 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: June 11, 1999