|
|
Title | Terminate a process immediately in VB.NET |
Description | This example shows how to terminate a process immediately in VB .NET. |
Keywords | terminate, end, halt, stop, kill, VB.NET |
Categories | Software Engineering, API, VB.NET |
|
|
This example's download contains two projects. The Target project is a simple application that catches its QueryUnload event and refuses to unload unless it's the program's own idea. In particular, it does not close if you send it the WM_CLOSE message.
|
|
Private m_OkToClose As Boolean
Private Sub btnClose_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnClose.Click
m_OkToClose = True
Me.Close()
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e _
As System.ComponentModel.CancelEventArgs) Handles _
MyBase.Closing
e.Cancel = Not m_OkToClose
If e.Cancel Then MessageBox.Show("Unload ignored")
End Sub
|
|
The second project is the Terminator. If you click the Close button, the program uses the FindWindow API function to find the target application and sends it the WM_CLOSE message. A normal program should clean up and stop when it receives this message but the Target program continues.
|
|
' Send the target the WM_CLOSE message.
Private Sub btnSendCloseMessage_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnSendCloseMessage.Click
' Get the target's window handle.
Dim target_hwnd As Integer = FindWindow(vbNullString, _
txtTargetTitle.Text)
If (target_hwnd = 0) Then
MessageBox.Show("Error finding target window " & _
"handle")
Exit Sub
End If
' Send the application the WM_CLOSE message.
PostMessage(target_hwnd, WM_CLOSE, 0, 0)
MessageBox.Show("Sent WM_CLOSE message")
End Sub
|
|
If you click the Terminate button, the gloves come off. The program uses FindWindow to find the target's window handle. It uses GetWindowThreadProcessId to get the window'sprocess ID, and then uses OpenProcess to open a connection to the process. It then calls TerminateProcess to force the target to stop immediately (no saving throw).
|
|
' Terminate the target.
Private Sub btnTerminate_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnTerminate.Click
Dim target_hwnd, target_process_id, _
target_process_handle As Integer
' Get the target's window handle.
target_hwnd = FindWindow(vbNullString, _
txtTargetTitle.Text)
If (target_hwnd = 0) Then
MessageBox.Show("Error finding target window " & _
"handle")
Exit Sub
End If
' Get the process ID.
target_process_id = 0
GetWindowThreadProcessId(target_hwnd, target_process_id)
If (target_process_id = 0) Then
MessageBox.Show("Error finding target process ID")
Exit Sub
End If
' Open the process.
target_process_handle = OpenProcess( _
SYNCHRONIZE Or PROCESS_TERMINATE, _
0, target_process_id)
If (target_process_handle = 0) Then
MessageBox.Show("Error finding target process " & _
"handle")
Exit Sub
End If
' Terminate the process.
If (TerminateProcess(target_process_handle, 0) = 0) Then
MessageBox.Show("Error terminating process")
Else
MessageBox.Show("Process terminated")
End If
' Close the process.
CloseHandle(target_process_handle)
End Sub
|
|
|
|
|
|