How to terminate process with visual basic !
Printable View
How to terminate process with visual basic !
Try using the search function in this forum.
yeah i found some codes but they dont work for me !!
or i dont now how to use them
is there any way killing a process just while writing the name on the textbox and click a button to kill it , if yes can u tell me the code please !!
Or something a little more completeCode:Call TerminateProcess("SomeProgram.exe")
Private Sub TerminateProcess(ByVal app_exe As String)
Dim Process As Object
For Each Process In GetObject("winmgmts:").ExecQuery("Select Name from Win32_Process Where Name = '" & app_exe & "'")
Process.Terminate
Next Process
End Sub
Code:Option Explicit
Private Sub Command1_Click()
Dim lRet As Long
lRet = Terminate(Text1.Text)
Select Case lRet
Case 1
MsgBox Text1.Text & " Terminated"
Case 2
MsgBox Text1.Text & " Not Running"
Case 3
MsgBox "Nothing Entered"
Case Else
MsgBox "Error# " & lRet
End Select
End Sub
' 1=Terminated, 2=Not Found, 3=Input Error, 0=General Error
Private Function Terminate(ByVal pExe As String) As Long
On Error GoTo Error_Handler
Dim Process As Object
Dim sText As String
sText = Trim(pExe)
If Len(sText) > 0 Then
For Each Process In GetObject("winmgmts:").ExecQuery _
("Select Name from Win32_Process Where Name = '" & sText & "'")
Process.Terminate
Terminate = 1
Exit Function
Next Process
Terminate = 2
Else
Terminate = 3
End If
Exit Function
Error_Handler:
Terminate = Err.Number
End Function
thank u very much that was exactly what i want
Great code.. very simple
but i have a problem.
My application initiates shell commands using wget to transfer files in the background.
for example
shell "wget url"
My app may have multiple instances of wget running.
if a user wants to cancel a download how do i kill a particular instance of WGET?
I might have answered my own question. but i am hoping there is a better way.
i copied the wget executable
wget1.exe
wget2.exe
wget3.exe
wget4.exe
wget5.exe
then depending on which one i execute i know how to kill it
If you can find out if there is anything else unique about them .. then you could use that instead of or in addition to the exe name.
Also see the Terminate API code below.
If you can get a handle from it, either by the programs title, or if you are just able to get the handle some other way, then you could use that instead.
Also if you dont need to Terminate it, you could always try closing it instead using SendMessage or PostMessage.
Basic Terminate Process API method:
Code:Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Public Function TerminateTaskAPI(ByVal pClass As String, ByVal pTitle As String) As Boolean
Dim TMHwnd As Long
Dim ProcID As Long
Dim ProcessName As Long
Dim retVal As Long
TMHwnd = FindWindow(pClass, pTitle)
retVal = GetWindowThreadProcessId(TMHwnd, ProcID)
ProcessName = OpenProcess(&H1F0FFF, 0&, ProcID)
retVal = TerminateProcess(ProcessName, 0&)
TerminateTaskAPI = retVal <> 0
End Function
Private Sub Command1_Click()
Debug.Print TerminateTaskAPI("#32770", "Display Properties")
End Sub
thanks, but what is the #32770 represent?
Quote:
Originally Posted by kurtsimons
In that case, it is the Class for the Display Properties Window .. actually it is the same class for many other common Windows XP System windows, hence in that example I also use the Window Title to search for the Handle.
Also see this project. It displays all the processes and their fields. If you can run a couple of those wgets and see if there is something else unique between them, another field, you can add that to the search as well using the basic terminate function in post #5.
http://www.vbforums.com/showpost.php?p=2655118
I want to appoligize to Rory for accidently checking off "Disaprove" when rating his post 'Terminate Function', while at the same time saying that I love it. The function is wonderful and I like most things that don't require an API. Excellent work Rory!
Thats okay .. no problem. :)
If i need to know how to terminate remote pc process ?
Because i have to close remote pc exe before i update latest exe.
Hi! i'm sorry to up this thread. I have a few question regarding terminating application which I couldn't found in the forum...
My question is I want to close all visible/active application(ei: ie, firefox, notepad, media player...etc) except my application. I used rory code I was able to look on all process but problem that I terminate other windows services or application which is needed to run win xp.
Pls. help me...
Using TerminateProcess API, terminates all Process based on the Windows in the Taskbar. Excludes Explorer.exe (folders) as well as the current app and also excludes the VB6 project. If a program is not shown in the taskbar it will not be terminated.
Add code to a Form, Add 1 Command Button, Test.
Note - use at your own risk!
Form1
Code:
Option Explicit
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, Optional lpNumberOfBytesWritten As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Const PROCESS_VM_READ = (&H10)
Const PROCESS_VM_WRITE = (&H20)
Const PROCESS_VM_OPERATION = (&H8)
Const MEM_COMMIT = &H1000
Const MEM_RESERVE = &H2000
Const MEM_RELEASE = &H8000
Const PAGE_READWRITE = &H4
Const WM_USER = &H400
Const TB_ISBUTTONHIDDEN = (WM_USER + 12)
Const TB_BUTTONCOUNT = (WM_USER + 24)
Const TB_GETBUTTONTEXTA = (WM_USER + 45)
Private Sub Command1_Click()
Call TerminateTaskbarWindows(hWnd)
End Sub
Private Sub TerminateTaskbarWindows(ByVal pHwnd As Long)
Dim sWindows As String, sArr() As String
Dim lArr As Long, iCnt As Integer, i As Integer
sWindows = GetTaskBarWindows(pHwnd)
If Len(sWindows) Then
sArr = Split(sWindows)
iCnt = UBound(sArr)
For i = 0 To iCnt
If IsNumeric(sArr(i)) Then
lArr = sArr(i)
Call TerminateAPI(lArr)
End If
Next i
End If
End Sub
Private Sub TerminateAPI(ByVal pHwnd As Long)
Dim TMHwnd As Long, ProcID As Long
Dim ProcessName As Long, retVal As Long
If pHwnd = 0 Then Exit Sub
retVal = GetWindowThreadProcessId(pHwnd, ProcID)
ProcessName = OpenProcess(&H1F0FFF, 0&, ProcID)
If IsExplorer(ProcID) = True Then Exit Sub '// dont close folders
If ProcessName = 0 Then Exit Sub
TerminateProcess ProcessName, 0&
End Sub
Private Function GetTaskBarWindows(ByVal pHwnd As Long) As String
Dim hTaskBar As Long, pID As Long, hProcess As Long
Dim N As Long, lCount As Long, lNum As Long, lLen As Long
Dim sCaption As String * 128, lpCaption As Long
Dim sTitle As String, lhWnd As Long, sTrack As String
hTaskBar = GetNotificationWindow
GetWindowThreadProcessId hTaskBar, pID
hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, 0, pID)
lpCaption = VirtualAllocEx(hProcess, ByVal 0&, Len(sCaption), MEM_COMMIT Or MEM_RESERVE, PAGE_READWRITE)
lNum = SendMessage(hTaskBar, TB_BUTTONCOUNT, 0, ByVal 0&)
Do Until lCount = lNum
lLen = SendMessage(hTaskBar, TB_GETBUTTONTEXTA, N, ByVal lpCaption)
If lLen > -1 Then
If SendMessage(hTaskBar, TB_ISBUTTONHIDDEN, N, 0&) = 0 Then
ReadProcessMemory hProcess, ByVal lpCaption, ByVal sCaption, Len(sCaption)
sTitle = Left$(sCaption, InStr(sCaption, vbNullChar) - 1)
lhWnd = FindWindow(vbNullString, sCaption)
If InStr(1, sTitle, "Microsoft Visual Basic", vbTextCompare) = 0 And lhWnd <> pHwnd And lhWnd <> 0 Then
sTrack = sTrack & lhWnd & " "
'Debug.Print sTitle & "-" & lhWnd
End If
End If
lCount = lCount + 1
End If
N = N + 1
Loop
VirtualFreeEx 0, lpCaption, 0, MEM_RELEASE
CloseHandle hProcess
GetTaskBarWindows = Trim(sTrack)
End Function
Private Function IsExplorer(ByVal pID As Long) As Boolean
Dim Process As Object
If pID = 0 Then Exit Function
For Each Process In GetObject("winmgmts:").ExecQuery("Select ProcessID, Name from Win32_Process Where ProcessID = '" & pID & "' and Name = 'explorer.exe'")
IsExplorer = True
Next Process
End Function
Private Function GetNotificationWindow() As Long
Dim lhWnd As Long
lhWnd = FindWindowEx(0&, 0&, "Shell_TrayWnd", vbNullString)
lhWnd = FindWindowEx(lhWnd, 0&, "ReBarWindow32", vbNullString)
lhWnd = FindWindowEx(lhWnd, 0&, "MSTaskSwWClass", vbNullString)
GetNotificationWindow = FindWindowEx(lhWnd, 0&, "ToolbarWindow32", vbNullString)
End Function
Let's say you have a button that closes the from can u put this in the prperties?
I get sub or function not defined on the line:Code:Call TerminateProcess("Program.exe")
Dim Process As Object
For Each Process In GetObject("winmgmts:").ExecQuery("Select Name from Win32_Process Where Name = '" & app_exe & "'")
Process.Terminate
Next Process
Code:Call TerminateProcess("Program.exe")
Scroll back up to post 5 and use the code that says "Or something a little more complete".
Ok, The Program Quits But I Get The Error:
Runtime Error -2147217385 (80041017)
Automation Error
On this line:
Code:For Each Process In GetObject("winmgmts:").ExecQuery("Select Name from Win32_Process Where Name = '" & App.path & "\CypexOS\SYSTEM34\ProgramDock.exe" & "'")