Kill Process using TerminateProcess API

rwatts
05-11-2004, 12:54 PM
Need some help with killing a process. I am using shell to lunch cmd.exe hidden. After the script runs I still have a process running in the taskman. I am trying to use TerminateProcess API but with no luck. What am I doing wrong with this API?


option explicit
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

private sub Command1_Click()
txtnet = "TestFile.txt"
sResult = Shell("c:\winnt\system32\cmd.exe /K netsh -c interface dump > c:\" & TxtNet, vbHide)
TerminateProcess sResult, 0&
end sub



INstead of using API I went ahead and used WMI. Here is the code in case someone may need it. (works on win 2000,2003 and xp)


Dim objProcess As Object
Dim objWMIService
Dim strComputer As String
Dim colProcessList

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'cmd.exe'")

For Each objProcess In colProcessList
objProcess.Terminate
Next




Thanks

FlexCell
05-12-2004, 01:02 PM
sResult = Shell("Command.com /C netsh -c interface dump > c:\TestFile.txt", vbHide)

Mathimagics
05-12-2004, 04:26 PM
You need to pass TerminateProcess a process handle, not the process ID that is returned by the Shell function.

Dim PID As Long
Dim hProcess As Long
Const PROCESS_TERMINATE = 1

PID = Shell("c:\windows\system32\cmd.exe etc etc .......", vbNormalFocus)
hProcess = OpenProcess(PROCESS_TERMINATE, 0, PID)


TerminateProcess hProcess, 0&

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum