Solved

Is a task running?

Posted on 1998-11-17
2
169 Views
Last Modified: 2010-05-03
From Program A, what is the best way to determine if Executable B is running prior to shelling B out.  The class and caption of Executable B are unknown.  All that is known about B is it's path and executable name.      
0
Comment
Question by:jphalen
2 Comments
 

Accepted Solution

by:
TAD062998 earned 100 total points
ID: 1445309
This is the version for 95 ( not tested on 98 yet but it should work)
The version for NT requires an additional 'free' .dll (Psapi.dll)
If you need that code let me know.

Add the following to a .bas module :

Private Const TH32CS_SNAPPROCESS As Long = 2&
Private Const MAX_PATH           As Integer = 260

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 * MAX_PATH
End Type

Private Declare Function CreateToolhelpSnapshot Lib "Kernel32" _
                Alias "CreateToolhelp32Snapshot" _
                (ByVal lFlags As Long, ByVal lProcessID As Long) As Long

Private Declare Function ProcessFirst Lib "Kernel32" _
                Alias "Process32First" _
                (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long

Private Declare Function ProcessNext Lib "Kernel32" _
                Alias "Process32Next" _
                (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long

Private Declare Sub CloseHandle Lib "Kernel32" _
                (ByVal hPass As Long)

Public Function IsAppRunning95(ByVal sApp As String) As Boolean

    Dim hSnapshot   As Long
    Dim lContinue   As Long
    Dim bFound      As Boolean
    Dim pe32        As PROCESSENTRY32
     
    hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0)
     
    pe32.dwSize = LenB(pe32)
     
    lContinue = ProcessFirst(hSnapshot, pe32)
    bFound = False
    Do
        If UCase(sApp) = UCase(Left(pe32.szExeFile, InStr(1, pe32.szExeFile, Chr$(0)) - 1)) Then
           bFound = True
           Exit Do
        End If

        lContinue = ProcessNext(hSnapshot, pe32)
    Loop While lContinue   ' If True, return 1
     
    If bFound = True Then
        IsAppRunning95 = True
    Else
        IsAppRunning95 = False
    End If
   
    Call CloseHandle(hSnapshot)

End Function

Then, in your application use :

    If IsAppRunning95("C:\Windows\MyApplication.exe") Then
        MsgBox "Yes"
    Else
        MsgBox "No"
    End If

0
 

Author Comment

by:jphalen
ID: 1445310
If it is running, how would I get the handle so ShowWindow can be called?
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

789 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