Results 1 to 3 of 3

Thread: terminating application

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2001
    Posts
    10

    terminating application

    Sir,
    Please tell me as to how I can terminate an application that was started from vb using the Shell function. How do I acquire the handle to the started application so that it can be terminated.

    Please help me as I need this technique to be implemented.

    Bye and Regards
    Sathesh

  2. #2
    Matthew Gates
    Guest
    Try this (does not work for Win NT/2k):


    VB Code:
    1. Private 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


    Since you shelled the program by it's exe file path, you can close it the same way.

  3. #3
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530

    Here you go...

    See attached
    Attached Files Attached Files

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