Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to detect if an application is running 1

Status
Not open for further replies.

gieboy

Programmer
Feb 15, 2001
20
0
0
PH
I want to know how can i detect if an appliaction is running?
 
OK, here is the code that you need along with a brief description of what is going on . . . ENJOY!


In a BAS Module's General Declarations - Declare your APIs


Public Const MAX_PATH As Integer = 260

'** OpenHandle Constants.
Public Const PROCESS_QUERY_INFORMATION = 1024
Public Const PROCESS_VM_READ = 16
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const SYNCHRONIZE = &H100000

'** WaitForSingleObject Return Values.
Public Const WAIT_FAILED = &HFFFFFFFF
Public Const WAIT_OBJECT_0 = &H0
Public Const WAIT_TIMEOUT = &H102

'** API declarations.
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Public Declare Sub CloseHandle Lib "kernel32.dll" (ByVal hPass As Long)

Public Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long

Public Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long

Public Declare Function GetModuleBaseNameA Lib "psapi.dll" (ByVal lngProcessHandle As Long, ByVal lngModuleHandle As Long, ByVal strModuleName As String, ByVal lngSize As Long) As Long

Public Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long



Next, define the actual function that finds the runnings application's process ID

Private Function GetProcessHandle(strName As String) As Long


'This Function accepts the name of the EXE (i.e. SomeProgram.exe) and scans
'the process list for it. When it is found, the Process ID is returned. If
'it is not found, then the function returns 0.

Dim lngProcessID() As Long
Dim lngCB As Long
Dim lngCBNeeded As Long
Dim lngRetCode As Long
Dim lngLoop As Long
Dim lngProcessHandle As Long
Dim strModuleName As String
Dim strTemp() As String
Dim lngModules(1 To 200) As Long
Dim lngCbNeeded2 As Long
Dim intTemp As Integer

'** Get the current list of running Processes IDs
'******************************************************************************
lngCB = 8
lngCBNeeded = 96
Do While lngCB <= lngCBNeeded
lngCB = lngCB * 2
ReDim lngProcessIDs(lngCB / 4) As Long
lngRetCode = EnumProcesses(lngProcessIDs(1), lngCB, lngCBNeeded)

If lngRetCode = 0 Then
MsgBox Err.LastDllError
Exit Do
End If
Loop
'******************************************************************************


'** Get the name of the Base Module in each Process Space.
'** NOTE : If you wanted to, you could also use the GetModuleBaseNameA API
'** to get every DLL and OCX loaded in the process space.
'******************************************************************************

For lngLoop = 1 To lngCBNeeded / 4
'Get a handle to the Process
lngProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION _
Or PROCESS_VM_READ, 0, lngProcessIDs(lngLoop))
'Got a Process handle
If lngProcessHandle <> 0 Then
'Get an array of the module handles for the specified
'process
lngRetCode = EnumProcessModules(lngProcessHandle, lngModules(1), 200, lngCbNeeded2)
'If the Module Array is retrieved, Get the ModuleFileName
If lngRetCode <> 0 Then
strModuleName = Space(MAX_PATH)
lngRetCode = GetModuleBaseNameA(lngProcessHandle, lngModules(1), strModuleName, 500)
ReDim Preserve strTemp(intTemp)
strTemp(intTemp) = Trim$(strModuleName)


'** Check to see if the current process is the target process.
If UCase$(Trim$(strModuleName)) = UCase$(Trim$(strName & Chr$(0))) Then
GetProcessHandle = lngProcessIDs(lngLoop)
Exit For
End If


intTemp = intTemp + 1
End If
End If
Next lngLoop
'******************************************************************************



End Function
- Jeff Marler B-)
 
Please note that the previous piece of code was cut/pasted from a larger piece . . . because of that, there are some extra API declarations that are not needed in the listed code . . . this shouldn't hurt anything, and if you'd like, please feel free to remove them if you use this. :) - Jeff Marler B-)
 
There is also a FAQ that shows how to list all running processes which can be tweaked to see if a target process is in the list.
 
How can I check whether a program is running on a separate networked computer
 
Dsi,
That is basically what the cod e I listed does. Given the name of the process/exe, it scans the process list to see if it is running.

Ken68,
I know that you can scan the process list on another computer (pview.exe does it all the time), but I am not sure exactly how those APIs (the ones that enumerate the process list on another PC) work . . . sorry.
- Jeff Marler B-)
 
I thought it only check the local computer on which the VB application is running


Ken68
 
I think that the question of Gieboy this means or asked !

If App.PrevInstance = True Then
Call MsgBox(&quot;This program is already running!&quot;, vbExclamation)
End
End If Eric De Decker
vbg.be@vbgroup.nl

License And Copy Protection AxtiveX.

Download Demo version on my Site:
 
jmarler,

Thanks for this code. It certainly gives me hope. I have a plan builder and a plan viewer and I want only one and not the other to run at any given time, so I need to detect when the other is running.

I'm having a minor glitch. Though psapi.dll is on my machine in several locations, when I run this code I get &quot;Run-time error '48': File not found: psapi.dll&quot; occuring where the code calls EnumProcesses to get the list of current running process.

I try to register psapi.dll and get &quot;LoadLibrary(&quot;C:\Windows\System\psapi.dll&quot;) failed. GetLastError returns 0x0000001f.&quot; I'm unfamiliar with how to pursue this type of error.

Dependency walker tells me that psapi.dll relies on NTDll.dll, Kernel32.dll, and ImageHlp.dll, all of which are installed and registered properly.

I'm at a dead end. Any suggestions?
TIA

scarfhead
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top