Solved

Is a task running?

Posted on 1998-11-17
2
145 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now