English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories
0

How can i check if a process is running?

2007-10-16 11:18:26 · 1 answers · asked by Anonymous in Computers & Internet Programming & Design

1 answers

the only way is to use windows API functions

Private Const MAX_PATH = 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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As Any) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As Any) As Long

Private Const TH32CS_SNAPHEAPLIST = &H1
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPTHREAD = &H4
Private Const TH32CS_SNAPMODULE = &H8
Private Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST + TH32CS_SNAPPROCESS + TH32CS_SNAPTHREAD + TH32CS_SNAPMODULE)
Private Const TH32CS_INHERIT = &H80000000

Sub Button1_Click()
If ProcessRunning(TextBox1.Text) Then
MsgBox ("Running")
Else
MsgBox ("Not Running")
End If
End Sub

Public Function ProcessRunning(pName As String) As Boolean
Dim hSnapshot As Long, lRet As Long, P As PROCESSENTRY32
Dim Found As Boolean

P.dwSize = Len(P)
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, ByVal 0)
Found = False
If hSnapshot Then
lRet = Process32First(hSnapshot, P)
Do While lRet
Dim procName As String
procName = Left$(P.szExeFile, InStr(P.szExeFile, Chr$(0)) - 1)
If LCase$(procName) = LCase$(pName) Then Found = True: Exit Do
lRet = Process32Next(hSnapshot, P)
Loop
lRet = CloseHandle(hSnapshot)
End If
ProcessRunning = Found
End Function

2007-10-16 22:46:34 · answer #1 · answered by Anonymous · 0 0

fedest.com, questions and answers