Results 1 to 4 of 4

Thread: Tasklist and Termination

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Location
    Singapore
    Posts
    98

    Tasklist and Termination

    Hi,

    How can i get a list of programs running (those listed on the taskbar) and then choose to terminate some of them? Is there any ways to verify or wait till these programs are successfully terminated before i carry on my execution?

    I searched through the general vb questions threads but found myself more confused about processes and those on the taskbar.

    I am using vb6 and intended to use the application on Win 9x and Win ME only.

    Thanks for any advice.

  2. #2
    Matthew Gates
    Guest
    To list the running applications:


    VB Code:
    1. Private Declare Function CreateToolhelpSnapshot Lib "Kernel32" Alias "CreateToolhelp32Snapshot" _
    2. (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
    3.  
    4. Private Declare Function ProcessFirst Lib "Kernel32" _
    5. Alias "Process32First" (ByVal hSnapShot As Long, uProcess As _
    6. PROCESSENTRY32) As Long
    7.  
    8. Private Declare Function ProcessNext Lib "Kernel32" _
    9. Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As _
    10. PROCESSENTRY32) As Long
    11.  
    12. Private Declare Sub CloseHandle Lib "Kernel32" _
    13. (ByVal hPass As Long)
    14.  
    15. Private Const TH32CS_SNAPPROCESS As Long = 2&
    16. Private Const MAX_PATH As Integer = 260
    17.  
    18. Private Type PROCESSENTRY32
    19.   dwSize As Long
    20.   cntUsage As Long
    21.   th32ProcessID As Long
    22.   th32DefaultHeapID As Long
    23.   th32ModuleID As Long
    24.   cntThreads As Long
    25.   th32ParentProcessID As Long
    26.   pcPriClassBase As Long
    27.   dwFlags As Long
    28.   szExeFile As String * MAX_PATH
    29. End Type
    30.  
    31.  
    32.  
    33. Private Sub Command1_Click()
    34.  
    35.     Dim hSnapShot As Long
    36.     Dim uProcess As PROCESSENTRY32
    37.     Dim r As Long
    38.  
    39.     hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    40.  
    41.     If hSnapShot = 0 Then
    42.         Exit Sub
    43.     End If
    44.  
    45.     uProcess.dwSize = Len(uProcess)
    46.  
    47.     r = ProcessFirst(hSnapShot, uProcess)
    48.  
    49.     Do While r
    50.         List1.AddItem uProcess.szExeFile
    51.         r = ProcessNext(hSnapShot, uProcess)
    52.     Loop
    53.  
    54.     Call CloseHandle(hSnapShot)
    55.  
    56. End Sub


    To close a running application (Note: This does not work on NT (nor do I think the code above does either):


    VB Code:
    1. rivate Declare Function ProcessFirst Lib "kernel32" _
    2. Alias "Process32First" (ByVal hSnapshot As Long, uProcess As _
    3. PROCESSENTRY32) As Long
    4.  
    5. Private Declare Function ProcessNext Lib "kernel32" _
    6. Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As _
    7. PROCESSENTRY32) As Long
    8.  
    9. Private Declare Function CreateToolhelpSnapshot Lib "kernel32" _
    10. Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, _
    11. lProcessID As Long) As Long
    12.  
    13. Private Declare Function CloseHandle Lib "kernel32" (ByVal _
    14. hObject As Long) As Long
    15.  
    16. Private Declare Function OpenProcess Lib "kernel32" (ByVal _
    17. dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
    18. ByVal dwProcessId As Long) As Long
    19.  
    20. Private Declare Function TerminateProcess Lib "kernel32" (ByVal _
    21. hProcess As Long, ByVal uExitCode As Long) As Long
    22.  
    23. Private Const MAX_PATH = 260
    24.  
    25. Private Type PROCESSENTRY32
    26.     dwSize As Long
    27.     cntUsage As Long
    28.     th32ProcessID As Long
    29.     th32DefaultHeapID As Long
    30.     th32ModuleID As Long
    31.     cntThreads As Long
    32.     th32ParentProcessID As Long
    33.     pcPriClassBase As Long
    34.     dwFlags As Long
    35.     szexeFile As String * MAX_PATH
    36. End Type
    37.  
    38.  
    39.  
    40. Private Function KillApp(myName As String) As Boolean
    41.     Const PROCESS_ALL_ACCESS = 0
    42.     Dim uProcess As PROCESSENTRY32
    43.     Dim rProcessFound As Long
    44.     Dim hSnapshot As Long
    45.     Dim szExename As String
    46.     Dim exitCode As Long
    47.     Dim myProcess As Long
    48.     Dim AppKill As Boolean
    49.     Dim appCount As Integer
    50.     Dim i As Integer
    51.     On Local Error GoTo Finish
    52.     appCount = 0
    53.    
    54.     Const TH32CS_SNAPPROCESS As Long = 2&
    55.    
    56.     uProcess.dwSize = Len(uProcess)
    57.     hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    58.     rProcessFound = ProcessFirst(hSnapshot, uProcess)
    59.    
    60.     Do While rProcessFound
    61.         i = InStr(1, uProcess.szexeFile, Chr(0))
    62.         szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
    63.         If Right$(szExename, Len(myName)) = LCase$(myName) Then
    64.             KillApp = True
    65.             appCount = appCount + 1
    66.             myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
    67.             AppKill = TerminateProcess(myProcess, exitCode)
    68.             Call CloseHandle(myProcess)
    69.         End If
    70.         rProcessFound = ProcessNext(hSnapshot, uProcess)
    71.     Loop
    72.  
    73.     Call CloseHandle(hSnapshot)
    74. Finish:
    75. End Function

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Location
    Singapore
    Posts
    98
    Thanks a lot Mat, will spend some time reading the codes and understanding them.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Location
    Singapore
    Posts
    98

    ICQ avoids the kill command?

    Hi again,

    The code that was adviced by Mat works real good, however, i found out that certain programs could avoid being 'killed' One good example is ICQ 2000b.

    There is one situation when ICQ could not detect a connection to the net and output an error message. If this message is not cleared by the user, the system cannot log off or shutdown. Even the app i just compiled using Mat's code does not shut the program down, is there a way to curb this ICQ program?

    thanks to any potential advisors

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