Is there another way to kill a task than the DestroyWindow
API function.
Thanx, Tadej
Printable View
Is there another way to kill a task than the DestroyWindow
API function.
Thanx, Tadej
If it's a window maybe you can send a ALT+F4 message to it via SendMessage API.
An application can consist of one window, a lot of windows or no windows at all, so instaed of DestroyWindow, try the TerminateProcess API:
http://msdn.microsoft.com/library/ps...thred_1bg3.htm
http://msdn.microsoft.com/library/ps...thred_4k6r.htm
Just be careful with this function because it forces a process to destroy and doesn't notify any DLLs or such that the process is ending.
See more details in the documents above.
[Edited by Sc0rp on 10-18-2000 at 12:14 PM]
It may be a window or not so I need to terminate
the process.
Is there any way to enumerate processes currently
running like there is with windows? Because I only
know the handle to a window.
Thanks for replying.
Tadej
Look here for making a list of all running processes
http://www.vbsquare.com/tips/tip177.html
Maybe this?
Code:Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
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 * MAX_PATH
End Type
Public Function KillApp(myName As String) As Boolean
Const PROCESS_ALL_ACCESS = 0
Dim uProcess As PROCESSENTRY32
Dim rProcessFound As Long
Dim hSnapshot As Long
Dim szExename As String
Dim exitCode As Long
Dim myProcess As Long
Dim AppKill As Boolean
Dim appCount As Integer
Dim i As Integer
On Local Error GoTo Finish
appCount = 0
Const TH32CS_SNAPPROCESS As Long = 2&
uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
rProcessFound = ProcessFirst(hSnapshot, uProcess)
Do While rProcessFound
i = InStr(1, uProcess.szexeFile, Chr(0))
szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
If Right$(szExename, Len(myName)) = LCase$(myName) Then
KillApp = True
appCount = appCount + 1
myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
AppKill = TerminateProcess(myProcess, exitCode)
Call CloseHandle(myProcess)
End If
rProcessFound = ProcessNext(hSnapshot, uProcess)
Loop
Call CloseHandle(hSnapshot)
Finish:
End Function
Usage
Call KillApp("C:\app.exe")
Hey Matthew.
When I run your code, I get a stop on this line:
szexeFile As String * MAX_PATH
It stops on MAX_PATH and the error is:
Compile Error:
Constant Expression Required
Is it sth I'm doing?
Thanks
Wengang
Add this right after the "Declare Function"s:
Code:Const MAX_PATH = 260
Okay. got that. And then when I run the app, I get an error on these two lines:
myProcess = OpenProcess(PROCESS_ALL_ACCESS,_
False,uProcess.th32ProcessID)
AppKill = TerminateProcess(myProcess, exitCode)
they both say sub or function not defined. Does anybody else get these errors?
Where did you put the code? In form declarations or module?
I had tried in both. Actually the app only loads if it is in a module.
But I still get those errors.
ANy ideas?
The only problem I can think of why you are having this is that you have an Option Explicit somewhere, either in the Form declarations or in a module. Try removing Option Explicit and everything should be fine.
In the code above, insert the word "Public" at the beginning of each line that starts with either "Type" or "Function".
Well, I found the reason why they weren't working, thanks to someone who emailed me.
I forgot to include 2 api functions.
Code:Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
ByVal uExitCode As Long) As Long