|
-
Oct 18th, 2000, 10:46 AM
#1
Thread Starter
Member
Is there another way to kill a task than the DestroyWindow
API function.
Thanx, Tadej
-
Oct 18th, 2000, 10:58 AM
#2
Frenzied Member
If it's a window maybe you can send a ALT+F4 message to it via SendMessage API.
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Oct 18th, 2000, 11:11 AM
#3
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]
-
Oct 18th, 2000, 12:49 PM
#4
Thread Starter
Member
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
-
Oct 18th, 2000, 01:02 PM
#5
Frenzied Member
Look here for making a list of all running processes
http://www.vbsquare.com/tips/tip177.html
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Oct 18th, 2000, 01:46 PM
#6
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")
-
Oct 18th, 2000, 08:20 PM
#7
Frenzied Member
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
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Oct 18th, 2000, 10:06 PM
#8
Add this right after the "Declare Function"s:
Code:
Const MAX_PATH = 260
-
Oct 19th, 2000, 02:07 AM
#9
Frenzied Member
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?
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Oct 19th, 2000, 06:13 AM
#10
Where did you put the code? In form declarations or module?
-
Oct 19th, 2000, 08:55 AM
#11
Frenzied Member
I had tried in both. Actually the app only loads if it is in a module.
But I still get those errors.
ANy ideas?
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Oct 19th, 2000, 01:43 PM
#12
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.
-
Oct 19th, 2000, 06:12 PM
#13
In the code above, insert the word "Public" at the beginning of each line that starts with either "Type" or "Function".
-
Oct 23rd, 2000, 06:21 PM
#14
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|