visual fzz
February 2nd, 2000, 04:36 AM
How can I kill a process
exemple : I launch cmd.exe with the shell function, I want to kill this process in my code.
Fzz
Chris Eastwood
February 2nd, 2000, 04:45 AM
A quick 'search' of this forum found this :
http://codeguru.developer.com/bbs/wt/showpost.pl?Board=vb&Number=14211&page=1&view=expanded&sb=5&category=All
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
February 2nd, 2000, 11:06 AM
Hello,
This code is very useful, however, is there any code which can terminate a program which is running in memory and does not have a "window title"? (i.e. similar to the "End Task" method in Windows)
-
Aaron Young
February 2nd, 2000, 01:42 PM
If you know the ProcessID, (returned by the Shell() Function) then you can bypass the Windows Operations in my other Sub and use something like this:
private Declare Function GetExitCodeProcess Lib "kernel32" (byval hProcess as Long, lpExitCode as Long) as Long
private Declare Function TerminateProcess Lib "kernel32" (byval hProcess as Long, byval uExitCode as Long) as Long
private Declare Function OpenProcess Lib "kernel32" (byval dwDesiredAccess as Long, byval bInheritHandle as Long, byval dwProcessId as Long) as Long
private Const PROCESS_TERMINATE = &H1
public Sub TerminateShell(byval ProcessID as Long)
Dim lExitCode as Long
Dim lProcHnd as Long
lProcHnd = OpenProcess(PROCESS_TERMINATE, 0&, ProcessID)
If lProcHnd then
Call GetExitCodeProcess(lProcHnd, lExitCode)
Call TerminateProcess(lProcHnd, lExitCode)
End If
End Sub
Usage: Call TerminateShell(lProcessID)
If you don't know the Process Handle, or just want to list all Processes and use one of my 2 methods for terminating them, try my rendition of MicroSofts Example List Processes Code:
'In a Standard Module..
public Type PROCESSENTRY32
dwSize as Long
cntUsage as Long
th32ProcessID as Long
th32DefaultHeapID as Long
th32ModuleID as Long
cntThreads as Long
th32ParentProcessID as Long
pcPriClassBase as Long
dwFlags as Long
szExeFile as string * 260
End Type
private Type OSVERSIONINFO
dwOSVersionInfoSize as Long
dwMajorVersion as Long
dwMinorVersion as Long
dwBuildNumber as Long
dwPlatformId as Long
szCSDVersion as string * 128
End Type
public Declare Function Process32First Lib "kernel32" (byval hSnapshot as Long, lppe as PROCESSENTRY32) as Long
public Declare Function Process32Next Lib "kernel32" (byval hSnapshot as Long, lppe as PROCESSENTRY32) as Long
public Declare Function CloseHandle Lib "Kernel32.dll" (byval Handle as Long) as Long
public Declare Function OpenProcess Lib "Kernel32.dll" (byval dwDesiredAccessas as Long, byval bInheritHandle as Long, byval dwProcId as Long) as Long
public Declare Function EnumProcesses Lib "psapi.dll" (byref lpidProcess as Long, byval cb as Long, byref cbNeeded as Long) as Long
public Declare Function GetModuleFileNameExA Lib "psapi.dll" (byval hProcess as Long, byval hModule as Long, byval ModuleName as string, byval nSize as Long) as Long
public Declare Function EnumProcessModules Lib "psapi.dll" (byval hProcess as Long, byref lphModule as Long, byval cb as Long, byref cbNeeded as Long) as Long
public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (byval dwFlags as Long, byval th32ProcessID as Long) as Long
private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation as OSVERSIONINFO) as Long
public Const VER_PLATFORM_WIN32_WINDOWS = 1
public Const PROCESS_QUERY_INFORMATION = 1024
public Const PROCESS_VM_READ = 16
public Const TH32CS_SNAPPROCESS = &H2
public Function CheckVersion() as Long
Dim tOS as OSVERSIONINFO
tOS.dwOSVersionInfoSize = len(tOS)
Call GetVersionEx(tOS)
CheckVersion = tOS.dwPlatformId
End Function
'In a Form with a Listbox and a Command Button..
private Sub Command1_Click()
Dim aPID() as Long
Dim lProcesses as Long
Dim lProcess as Long
Dim lModule as Long
Dim sName as string
Dim iIndex as Integer
Dim bCopied as Long
Dim lSnapShot as Long
Dim tPE as PROCESSENTRY32
List1.Clear
If CheckVersion() = VER_PLATFORM_WIN32_WINDOWS then
'Windows 9x
'Create a SnapShot of the Currently Running Processes
lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If lSnapShot < 0 then Exit Sub
tPE.dwSize = len(tPE)
'Buffer the First Processes Info..
bCopied = Process32First(lSnapShot, tPE)
While bCopied
'While there are Processes List them..
List1.AddItem _
Right$("000" & tPE.th32ProcessID, 3) & " - " & _
Left$(tPE.szExeFile, InStr(proc.szExeFile, Chr(0)) - 1)
bCopied = Process32Next(lSnapShot, tPE)
Wend
else
'Windows NT
'The EnumProcesses Function doesn't indicate how many Process there are,
'so you need to pass a large array and trim off the empty elements
'as cbNeeded will return the no. of Processes copied.
ReDim aPID(255)
Call EnumProcesses(aPID(0), 1024, lProcesses)
lProcesses = lProcesses / 4
ReDim Preserve aPID(lProcesses)
for iIndex = 0 to lProcesses - 1
'get the Process Handle, by Opening the Process
lProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, aPID(iIndex))
If lProcess then
'Just get the First Module, all we need is the Handle to get
'the Filename..
If EnumProcessModules(lProcess, lModule, 4, 0&) then
sName = Space(260)
Call GetModuleFileNameExA(lProcess, lModule, sName, len(sName))
While InStr(sName, "\")
sName = mid$(sName, InStr(sName, "\") + 1)
Wend
List1.AddItem Right$("000" & aPID(iIndex), 3) & " - " & sName
End If
'Close the Process Handle
lRet = CloseHandle(lProcess)
End If
next
End If
End Sub
Aaron Young
Analyst Programmer
ajyoung@pressenter.com
aarony@redwingsoftware.com