Terminate process in wmi with vb.net

K

Keith Grefski

I cant seem to figure out how to terminate a process in vb.net i can do it
using vbscript and wmi but terminate doesnt seem to exist in the available
classes for wmi under vb.net
 
K

Ken Tucker [MVP]

Hi,

You can do it without the wmi. Take a look at the process class.

' To list processes

Dim p As Process

For Each p In Process.GetProcesses

Debug.WriteLine(p.ProcessName)

Next

' to kill a process

'p.Kill()



Ken
 
K

Keith Grefski

that doesnt seem to work remotely

here is the code i am trying to use now, i get a cast error now

Function WMIKill(ByVal Server As String, ByVal sProcess As String)

Dim strMoniker, strQuery As String

Dim colProcesses As Management.ManagementObjectSearcher

Dim refProcess As Management.ManagementObject

colProcesses = New Management.ManagementObjectSearcher("SELECT * FROM
Win32_Process WHERE Handle = '" & sProcess & "'")

colProcesses.Scope = New Management.ManagementScope("\\" & Server &
"\root\cimv2")

If colProcesses.Get.Count = 0 Then

ListOutPut.Items.Add(sProcess & " - Not found on " & Server)

Else

'there are some matching process, so loop through and kill

For Each refProcess In colProcesses.Get

If refProcess.InvokeMethod("Terminate", refProcess("handle")) = 0 Then

ListOutPut.Items.Add("(PID " & refProcess("Handle") & ") " & _

refProcess("Name") & _

" Terminated on - " & Server)

Else

ListOutPut.Items.Add("Unable to terminate - " & _

refProcess("Name") & " on " & Server)

End If

Next

End If

End Function
 
M

MadIndian

I figured it out. Here's the code i used

Function WMIKill(ByVal Server As String, ByVal sProcess As String)

Dim colProcesses As Management.ManagementObjectSearcher

Dim colObserver As New Management.ManagementOperationObserver

Dim refProcess As Management.ManagementObject

colProcesses = New Management.ManagementObjectSearcher("SELECT * FROM
Win32_Process WHERE Handle = '" & sProcess & "'")

colProcesses.Scope = New Management.ManagementScope("\\" & Server &
"\root\cimv2")

If colProcesses.Get.Count = 0 Then

Else

For Each refProcess In colProcesses.Get

refProcess.InvokeMethod(colObserver, "Terminate", Nothing)

Next

End If

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top