Determining if a process is running

D

Doug

I have a situation where I need to be able to find out if
a specific process is running when I run my VBA code. If
the process is running, then I do one thing, otherwise I
do another.

There has to be a Windows 2000 API call somewhere that can
do that. When executing, sometimes the process displays
one window, sometimes another, and sometimes no window at
all, so I can't do anything that looks for the existence
of a window. I need something equivalent to the Processes
tab of the Windows task manager, where I can give the API
call the name of the process, and find out if it's
executing.

Can anybody give me a hand?
 
L

Lars-Eric Gisslén

Doug,

There is no single API call to find out if a process is running. There are
two way to get the processes, depending on if the code has to work under Win
98/ME or just Win 2000 +. I have written code for both ways and if you let
me know what your requirements are I try to port it to VB.
 
L

Lars-Eric Gisslén

Dough,

Ok, it was not so much job to convert the code that works even under Win 98.

Try this code:

Sub test()

If IsAppRunning("winword.exe") Then
MsgBox "Found MS Word!"
Else
MsgBox "Hmm... Could not find myself??"
End If

End Sub

' Add the following code to another module

Const TH32CS_INHERIT = &H80000000
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST + TH32CS_SNAPPROCESS + _
TH32CS_SNAPTHREAD + TH32CS_SNAPMODULE)
Const INVALID_HANDLE_VALUE = &HFFFFFFFF
Const MAX_PATH = 260

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

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

Declare Function Process32First Lib "kernel32" _
(ByVal hSnapshot As Long, _
pPROCESSENTRY32 As PROCESSENTRY32) _
As Boolean

Declare Function Process32Next Lib "kernel32" _
(ByVal hSnapshot As Long, _
pPROCESSENTRY32 As PROCESSENTRY32) _
As Boolean

Declare Function CloseHandle Lib "kernel32" _
(hObject As Long) As Boolean

Function IsAppRunning(ByVal sEXE As String) As Boolean
Dim hSnapshot As Long
Dim nCount As Long
Dim nPos As Long
Dim cApp As String
Dim cProc As String
Dim pModuleName As Long
Dim pProcEntry As PROCESSENTRY32

hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0)

If hSnapshot = INVALID_HANDLE_VALUE Then
Exit Function
End If

pProcEntry.dwSize = Len(pProcEntry)

If Process32First(hSnapshot, pProcEntry) Then
If InStr(1, UCase(pProcEntry.szExeFile), UCase(sEXE)) Then
IsAppRunning = True
Else
Do While Process32Next(hSnapshot, pProcEntry)
If InStr(1, UCase(pProcEntry.szExeFile), UCase(sEXE)) Then
IsAppRunning = True
Exit Do
End If
Loop
End If
End If

CloseHandle (hSnapshot)

End Function
 
J

Jonathan West

Hi Doug,

In Word VBA, the Tasks collection gives you a list of running applications.
 
L

Lars-Eric Gisslén

Jonathan,

It gives you a list of applications with a Window and the Windows Caption.
If the application is running without a Window, as in Dough's case, I thing
the .Taks collection will not help much. To me the Tasks methods seems to be
just wrappers around EnumWindows()/GetWindowText() and related Win API
functions. If you are for instance searching for an application that is just
displaying a tray icon on the toolbar (beside the clock), or a background
process, I bet you must enumerate the processes and search for the EXE name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top