Solved

Is a task running?

Posted on 1998-11-17
2
178 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
[X]
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
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How does CurrentUser work? 10 44
Set email body to html using vbscript 6 96
RUNRMTCMD from AS/400 13 130
Powerpoint 2013: Change cell reference in excel link 3 140
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

751 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