Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

Testing for running Applications in VB

I have application X that wants to know if application Y is currently running.  How do I do this in VB?
1 Solution
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

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now