Click to See Complete Forum and Search --> : Is Program Running?


Hound_Cat
March 20th, 2001, 10:44 AM
Given a program name or file name, how can I detect if it is currently running on the system?

Thanks...

John G Duffy
March 20th, 2001, 12:54 PM
Start a new project. Add a Module to it. Paste this code into the General declarations section of the Form.

option Explicit

private Sub Form_Load()
Dim iRet as Long
' Check if THIS APP is currently running.
' Zero result says no, Non-Zero says Yes
iRet = IsItRunning(App.Title) '
MsgBox iRet
' is another app running?
' Passing another Application Title, Check if it is currently running.
iRet = IsItRunning("Program Manager")
MsgBox iRet
End
End Sub



'
' Paste this code into the module

option Explicit

private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(byval lpClassName as string, byval lpWindowName as string) _
as Long

'*******************************************************************************
' IsItRunning (FUNCTION)
'
' PARAMETERS:
' (In/Out) - who - string -
'
' RETURN VALUE:
' Long - 0 if application is not running
' - hwnd of the application window
' DESCRIPTION:
'
'*******************************************************************************
Function IsItRunning(who as string) as Long
Dim OldTitle as string
' Save existing Title in case testing for myself
OldTitle = App.Title
App.Title = "Some Junk Title"
'Attempt to get window handle
IsItRunning = FindWindow(vbNullString, who)
' Restore Applications title
App.Title = OldTitle

End Function




Run the program. It will look for itself then the "Program Manager"


John G

Hound_Cat
March 20th, 2001, 01:23 PM
Will this also work for programs running in the system tray?

John G Duffy
March 20th, 2001, 04:50 PM
Do not know why not. It's looking for the Programs title which should not be effected by where its running. ONe of the checks is for "Program Manager" which is an internal Windows app and has no windows at all.
Here is another sample that will Enumerate all running processes. Check for yours.
Start a new project, add a big text box to it, Add a Module.
Paste the following into the Form

option Explicit

'Add this code to a form
private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
'set the form's graphics mode to persistent
me.AutoRedraw = true
'call the Enumwindows-function
Dim x
EnumWindows AddressOf EnumWindowsProc, byval 0&
End Sub
'
' Add this code to the Module
'
option Explicit
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net

'Add this code to a module
Declare Function EnumWindows Lib "user32" (byval lpEnumFunc as Long, byval lParam as Long) as Boolean
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (byval hwnd as Long, byval lpString as string, byval cch as Long) as Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (byval hwnd as Long) as Long
public Function EnumWindowsProc(byval hwnd as Long, byval lParam as Long) as string
Dim sSave as string, Ret as Long
Ret = GetWindowTextLength(hwnd)
sSave = Space(Ret)
GetWindowText hwnd, sSave, Ret + 1
If Trim(sSave) <> "" then _
frmIsMeRunning.Text1 = frmIsMeRunning.Text1 & sSave & vbCrLf
'continue enumeration
EnumWindowsProc = true
End Function




'
'
Run the project

John G

Hound_Cat
March 21st, 2001, 08:10 AM
Thanks :)