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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check if a process is already running? 2

Status
Not open for further replies.

dashen

Programmer
Jul 14, 2005
233
US
Hi guys,

I am at a loss, but can anyone point me in the right direction or provide a snippet for VB6, which would allow me to check if the executable is already running, and if so kill the process being called? Thanks in advanced.
 
If App.PrevInstance = True then
.........
end if

 
I can't use any .NET Solutions either because not all users have .NET features.
 
Try VBHelp - look for App.Previnstance

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Wow - what a lot of quick typists!

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Here's an API solution from Hypetia that I use regularly:
Code:
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32.dll" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Const PROCESS_QUERY_INFORMATION = &H400

Private Function IsProcessRunning(pid As Long) As Boolean
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid)
CloseHandle hProcess
IsProcessRunning = hProcess
End Function

You can use the above function to kill a process as well:
Code:
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Const PROCESS_TERMINATE = 1
Private Function KillProcess(pid As Long) As Boolean
Dim hProcess As Long
Do
    hProcess = OpenProcess(PROCESS_TERMINATE, 0, pid)
    KillProcess = TerminateProcess(hProcess, 0)
    CloseHandle hProcess
Loop While IsProcessRunning(pid)
End Function
IsProcessRunning will tell you if a process is running. KillProcess will attempt to terminate a process, check to see if it's still running, and then attempt to terminate it again until successful. (Of course, if it's never successful.......)

HTH

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top