Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Killing Running Process

Status
Not open for further replies.

ghost807

IS-IT--Management
Jun 27, 2003
99
0
0
US
in my app i have a process that is started but i also need the ability to kill the process if conditions are no longer met. i have found (in the sdk) a reference to terminate process but i can't seem to figure out how to make it work. i can list all the processes running but that's about it.
Heres some of the code i have(for getting all the processes)


Sub LoadTaskList()
Dim CurrWnd As Long
Dim Length As Long
Dim TaskName As String
Dim Parent As Long
List1.Clear
CurrWnd = GetWindow(Form1.hwnd, GW_HWNDFIRST)
While CurrWnd <> 0
Parent = GetParent(CurrWnd)
Length = GetWindowTextLength(CurrWnd)
TaskName = Space$(Length + 1)
Length = GetWindowText(CurrWnd, TaskName, Length + 1)
TaskName = Left$(TaskName, Len(TaskName) - 1)

If Length > 0 Then
If TaskName <> Me.Caption Then
List1.AddItem TaskName
End If
End If
CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
DoEvents
Wend
End Sub
 
The following will terminate the process in which Excel.exe is running


strComputer = &quot;.&quot;
Set objWMIService = GetObject(&quot;winmgmts:{impersonationLevel=impersonate}!\\&quot; & strComputer & &quot;\root\cimv2&quot;)
Set colProcessList = objWMIService.ExecQuery(&quot;SELECT * FROM Win32_Process WHERE Name = 'Excel.exe'&quot;)
For Each objProcess In colProcessList
objProcess.Terminate
Next


I'm not sure if this is what you are after, but if not it might give you some clues to get what you want

Stephen
 
You need a handle to the process's main window to use the TerminateProcess API. If you know the window title or class name then use FindWindow to get the handle.

Here's the TerminateProcess stuff:
Code:
Public Declare Function GetWindowThreadProcessId Lib &quot;user32&quot; _
(ByVal hwnd As Long, lpdwProcessId As Long) As Long

Public Declare Function OpenProcess Lib &quot;kernel32&quot; _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long

Public Declare Function TerminateProcess Lib &quot;kernel32&quot; _
(ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Public Const PROCESS_ALL_ACCESS = &H1F0FFF

Public Sub sKillProcess(Byval hWnd As Long)

  '--- Kills a process

  '--- Parameter
  '    hWnd: the process's window handle

  Dim lngRtn As Long
  Dim lngProc As Long
  DimlngProcID As Long

  lngRtn = GetWindowThreadProcessId(hWnd, lngProcID)
  lngProc = OpenProcess(PROCESS_ALL_ACCESS, Clng(0), lngProcID)
  lngRtn = TerminateProcess(lngProc, Clng(0))

End Sub
Paul Bent
Northwind IT Systems
 
Thanx this worked really well i know the name of the app i want to kill. and i have it as part of an if statement. like i said thanx. i have tried in several diff places and this is the only place that has a wonderfull solution in such a short time.
thanx once again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top