|
-
Aug 1st, 2001, 04:14 PM
#1
Thread Starter
Member
How do I kill a program from VB if I only know its name
How do I kill a program from VB if I only know its name e.g. iexplorer.exe not the name in the task bar or the whole path.
Sorry about x-posting but http://www.vbforums.com/showthread.p...threadid=93419
-
Aug 1st, 2001, 06:02 PM
#2
New Member
did I get this right? =}
I guess this is what u need.
(It worked for me on winME, but I'm not sure that it will work correctly in winNT, win2000.)
'------------------------------------------------------------------'
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
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 * 260
End Type
Const TH32CS_SNAPPROCESS = &H2&
Const hNull = 0
Dim arrProc() As PROCESSENTRY32
Const PROCESS_TERMINATE = &H1
Private Sub Form_Load()
Dim i As Integer
Dim hProc As Long
Const progName = "iexplore.exe"
GetRunningProcesses
For i = 0 To UBound(arrProc)
If InStr(1, arrProc(i).szExeFile, UCase(progName)) Then
If MsgBox("Shut down " & progName & "?", vbYesNo) = vbYes Then
hProc = OpenProcess(PROCESS_TERMINATE, 0, arrProc(i).th32ProcessID)
TerminateProcess hProc, 0
Else
Exit For
End If
End If
Next
Unload Me
End Sub
Function GetRunningProcesses() As Variant
Dim num As Integer
Dim lRet As Long
Dim strProcess As String, strProID As String
Dim hSnap As Long, udtProc As PROCESSENTRY32
hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If hSnap = hNull Then Exit Function
udtProc.dwSize = Len(udtProc)
lRet = Process32First(hSnap, udtProc)
Do While lRet
strProcess = StripNulls(udtProc.szExeFile)
strProID = udtProc.th32ProcessID
ReDim arrProc(num)
arrProc(num).szExeFile = strProcess
arrProc(num).th32ProcessID = strProID
num = num + 1
lRet = Process32Next(hSnap, udtProc)
Loop
End Function
Function StripNulls(pstrString As String) As String
StripNulls = Left(pstrString, InStr(pstrString, vbNullChar) - 1)
End Function
'---------------------------------------------------------------------------'
"that's it!" =]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|