Results 1 to 14 of 14

Thread: Killing a task

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Slovenia, Europe
    Posts
    58
    Is there another way to kill a task than the DestroyWindow
    API function.

    Thanx, Tadej

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  3. #3
    Guest
    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]

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Slovenia, Europe
    Posts
    58
    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

  5. #5
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  6. #6
    Guest
    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")

  7. #7
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    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

  8. #8
    Guest
    Add this right after the "Declare Function"s:
    Code:
    Const MAX_PATH = 260

  9. #9
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    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

  10. #10
    Guest
    Where did you put the code? In form declarations or module?

  11. #11
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    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

  12. #12
    Guest
    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.

  13. #13
    Guest
    In the code above, insert the word "Public" at the beginning of each line that starts with either "Type" or "Function".

  14. #14
    Guest
    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
  •  



Click Here to Expand Forum to Full Width