Testing for running Applications in VB

Posted on 2003-02-28
Medium Priority
Last Modified: 2012-05-04
I have application X that wants to know if application Y is currently running.  How do I do this in VB?
Question by:BoeMatic
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

MN_Dave earned 200 total points
ID: 8045188
I have had success with this. It returns a boolean so you might call it like this:

isRunning = app_Running("MyApp.exe")

I can't really take credit for this code.  It's roughly something a friend sent me to return all active processes.  I just modified it to look for a particular process.


Option Explicit

Public Declare Function CloseHandle Lib "Kernel32.dll" _
   (ByVal Handle As Long) As Long

Public Declare Function OpenProcess Lib "Kernel32.dll" _
  (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
      ByVal dwProcId As Long) As Long

Public Declare Function EnumProcesses Lib "psapi.dll" _
   (ByRef lpidProcess As Long, ByVal cb As Long, _
      ByRef cbNeeded As Long) As Long

Public Declare Function GetModuleFileNameExA Lib "psapi.dll" _
   (ByVal hProcess As Long, ByVal hModule As Long, _
      ByVal ModuleName As String, ByVal nSize As Long) As Long

Public Declare Function EnumProcessModules Lib "psapi.dll" _
   (ByVal hProcess As Long, ByRef lphModule As Long, _
      ByVal cb As Long, ByRef cbNeeded As Long) As Long

Public Const PROCESS_VM_READ = 16
Public Const MAX_PATH = 260
Public Const SYNCHRONIZE = &H100000

Public Function app_Running(appl As String) As Boolean

Dim cb                  As Long
Dim cbNeeded            As Long
Dim Elements            As Long
Dim ProcessIDs()        As Long
Dim cbNeeded2           As Long
Dim Modules(1 To 1024)  As Long
Dim lRet                As Long
Dim ModuleName          As String
Dim nSize               As Long
Dim hProcess            As Long
Dim i                   As Long
Dim sModNam            As String
'Get the array containing the process id's for each process object
cb = 8
cbNeeded = 96
'Reallocate array until cb is larger than cbNeeded.
Do While cb <= cbNeeded
   cb = cb * 2
   ReDim ProcessIDs(cb / 4) As Long
   lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded)
'number of processes found
Elements = cbNeeded / 4
For i = 1 To Elements
        'Get a handle to the Process
        hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
            Or PROCESS_VM_READ, 0, ProcessIDs(i))
        ' Loop through processes with an ID that <> 0
        If hProcess Then
            'Retrieve the number of bytes that the array of module handles requires
            lRet = EnumProcessModules(hProcess, Modules(1), 1024, cbNeeded2)
            'Get an array of the module handles for the specified process
            lRet = EnumProcessModules(hProcess, Modules(1), cbNeeded2, cbNeeded2)
            'If the Module Array is retrieved, Get the ModuleFileName
            If lRet <> 0 Then
                'Prime the buffer with blanks.
                ModuleName = Space(MAX_PATH)
                'Set the size of the buffer.
                nSize = 500
                'Get the file name
                lRet = GetModuleFileNameExA(hProcess, Modules(1), ModuleName, nSize)
                'Get the file name from the buffer, lRet is the number of characters
                sModNam = Left$(ModuleName, lRet)
                'Is this what I'm looking for?
                If InStr(1, UCase(sModNam), Trim(UCase(appl))) > 0 Then
                  app_Running = True
                  Exit Function
                End If
            End If
            'If we didn't find it, then return false
            app_Running = False
        End If
        'Close the handle to the process
        lRet = CloseHandle(hProcess)
    app_Running = False
End Function


Expert Comment

ID: 8045200
You can do that with API:

'.....Form code........
Private Sub Form_Load()
End Sub

'.....Module code......
Private Declare Function EnumWindows& Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long)
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Public Function Find_Y_App()

Call EnumWindows(AddressOf EnumWinProc, 0)

End Function
Function EnumWinProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
  Dim AppName As String, l As Long
  AppName = Space$(128)
  l = GetWindowText(hwnd, AppName, 128)
  If l > 0 Then
    AppName = Left$(AppName, l)
    'Aplication name
    If AppName = "Application Y name" Then
       Debug.Print "Application Y is running"
    End If
  End If
  EnumWinProc = 1
End Function


Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question