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

Is a programm running?

Does anyone can tell me if there is an API function that allows me to know if a program is currently running. The only data i will pass to the function is the application name (program name); I don't want to use the FindWindow API function cause it doesn't work as I expect?
If you have the answer please E-mail me at jarevalo@elektra.com.mx
  • 2
1 Solution
call this with the full program name e.g. notepad.exe to return true (running) or false(not)

      Option Explicit

      Private Declare Function Process32First Lib "kernel32" _
      (ByVal hSnapshot As Long, lppe As _
      PROCESSENTRY32) As Long

      Private Declare Function Process32Next Lib "kernel32" _
      (ByVal hSnapshot As Long, lppe As _
      PROCESSENTRY32) As Long

      Private Declare Function CreateToolhelp32Snapshot Lib _
      "kernel32" (ByVal dwFlags As Long, _
      ByVal th32ProcessID As Long) As Long

      Private Type PROCESSENTRY32
         dwSize As Long
         cntUsage As Long
         th32ProcessID As Long
         th32DefaultHeapID As Long
         th32ModuleID As Long
         cntThreads As Long
         th32ParentProcessID As Long
         pcPriClassBase As Long
         dwFlags As Long
         szExeFile As String * 260
      End Type

      Private Const TH32CS_SNAPPROCESS = &H2&
      Private Const hNull = 0

      Public Function ExeRunning(ByVal ExeFind As String) As Boolean

          Dim hProc As Long, ExeName As String
          Dim lRet As Long, Proc As PROCESSENTRY32
          Dim g As Long, sDeighton As String
          Dim sClean As String
          Dim sAtom As String
          hProc = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
          ExeRunning = False
          If hProc = 0 Then
              Exit Function
          End If
          Proc.dwSize = Len(Proc)
          lRet = Process32First(hProc, Proc)
          Do While lRet
              ExeName = Left(Proc.szExeFile, Len(Proc.szExeFile) - 1)
              sClean = ""
              For g = 1 To Len(ExeName)
                sAtom = Mid(ExeName, g, 1)
                If sAtom = Chr(0) Then
                     Exit For
                    sClean = sClean & sAtom
                End If
              sDeighton = ""
              For g = Len(sClean) To 1 Step -1
                If Mid(sClean, g, 1) = "\" Then Exit For
                sDeighton = Mid(sClean, g, 1) & sDeighton
              If UCase(sDeighton) = UCase(ExeFind) Then
                  ExeRunning = True
                  Exit Function
              End If
              lRet = Process32Next(hProc, Proc)

      End Function

hasseltAuthor Commented:
I have problems with the API functions: The problem is that the program doesn’t recognize them so the system sends the following error:
“ Run time error 453 Can’t find DLL entry point CreateToolHelp32Snapshot in Kernel32. “
Can you please help me to solve it?

What Windows version do you use?  I run Windows 95.
Use App Object and PrevInstance

Sub form_load()
    Dim SaveTitle As String
    On Error Resume Next
    If App.PrevInstance Then
      SaveTitle = App.Title
      App.Title = "... duplicate instance."
      Form1.Caption = "... duplicate instance."
      AppActivate SaveTitle
    End If
End sub

This will not allow another instance to start - if one is running
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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