Link to home
Start Free TrialLog in
Avatar of muskad202
muskad202

asked on

terminate a process (Vb.NET 1.1)

Hi!

Given the process Id, how can I forcefully terminate a process? I tried Process.GetProcessById(id).Kill(), but i get an "Access Denied" exception for a number of processes, even though I can terminate them via Task Manager.

Thanks,
muskad202
Avatar of king_villy
king_villy
Flag of Pakistan image

Use this code , by using this code you can terminate Process by name , by id both of style s
hope it will help you


========================================
public Sub Terminator_Sub

Dim target_hwnd, target_process_id, target_process_handle As Integer

        ' Get the target's window handle.
        target_hwnd = FindWindow(vbNullString, "Name of Terminated Process")
        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, 2) = 0) Then
            MessageBox.Show("Error terminating process")
        Else
            MessageBox.Show("Process terminated")
        End If

        ' Close the process.
        CloseHandle(target_process_handle)
End Sub
ASKER CERTIFIED SOLUTION
Avatar of kaliyugkaarjun
kaliyugkaarjun

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial