Results 1 to 3 of 3

Thread: kill proccess and wait....memory lapse

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2000
    Location
    USA
    Posts
    37

    kill proccess and wait....memory lapse

    I've done this before, but not for some time and i can't find the source. So anyways, how can you find,kill, and wait for a process to finish closing before executing another.

  2. #2
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258
    Search The AllApi Network For complete samples Or download the Api Guide

  3. #3
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Try this, you can close an application by class or by caption, and you can force it to close if need be.

    In a module:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long
    4. Private Const SMTO_BLOCK = &H1
    5. Private Const SMTO_ABORTIFHUNG = &H2
    6. Private Const SC_CLOSE = &HF060&
    7. Private Const WM_SYSCOMMAND = &H112
    8. Private Const WM_NULL = &H0
    9.  
    10. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    11. Const PROCESS_ALL_ACCESS = &H1F0FFF
    12.  
    13. Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
    14. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    15.  
    16. Private Declare Function TerminateProcess& Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long)
    17. Private Declare Function GetWindowThreadProcessId& Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long)
    18. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    19.  
    20. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    21. Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    22. Private Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    23.  
    24. Public Enum HowToClose
    25.     hcForce
    26.     hcOneAtATime
    27.     hcAllAtOnce
    28. End Enum
    29.  
    30. Public Enum WindowIdType
    31.     WindowClassName
    32.     WindowCaption
    33. End Enum
    34.  
    35. Private hwnds() As Long
    36. Private m_ClassOrCaption As String
    37. Private m_Identify As WindowIdType
    38.  
    39. Public Sub CloseApplication(ByVal ClassOrCaption As String, Identify As WindowIdType, Optional CloseMethod As HowToClose = hcOneAtATime)
    40.  
    41.  'By Nucleus & Tygur
    42.  'Allows you to close an application by caption or class name
    43.  'and optionally force closure
    44.  
    45.  Dim pid&, hp&, lresult&, lcount&, i&
    46.  Dim bWndNonResponsive As Boolean
    47.  
    48.  ReDim hwnds(0)
    49.  m_ClassOrCaption = ClassOrCaption
    50.  m_Identify = Identify
    51.  
    52.  '// Get all handles of windows
    53.  EnumWindows AddressOf EnumWindowsProc, 0&
    54.  
    55.  '// Now close each window
    56.  If hwnds(0) <> 0 Then
    57.  
    58.     For lcount = 0 To UBound(hwnds)
    59.        
    60.         If IsWindow(hwnds(lcount)) Then
    61.        
    62.             bWndNonResponsive = (SendMessageTimeout(hwnds(lcount), WM_NULL, 0, 0, SMTO_ABORTIFHUNG, 10, 0) = 0)
    63.            
    64.             If Not bWndNonResponsive Then
    65.                 If CloseMethod = hcOneAtATime Then
    66.                     ' wait for user to close each app one at a time
    67.                     SendMessage hwnds(lcount), WM_SYSCOMMAND, SC_CLOSE, 0
    68.                 Else
    69.                     ' hcOneAtATimE or hcAllAtOnce therefore no need to wait for user
    70.                     SendMessageTimeout hwnds(lcount), WM_SYSCOMMAND, SC_CLOSE, 0, 0, 500, 0
    71.                 End If
    72.             End If
    73.            
    74.             ' Terminate happ if hanging or if want to force app to close
    75.             If IsWindow(hwnds(lcount)) And (CloseMethod = hcForce Or bWndNonResponsive) Then
    76.                 GetWindowThreadProcessId hwnds(lcount), pid
    77.                 hp = OpenProcess(PROCESS_ALL_ACCESS, 0&, pid)
    78.                 TerminateProcess hp&, 0&
    79.                 CloseHandle hp
    80.             End If
    81.            
    82.             DoEvents
    83.            
    84.         End If
    85.        
    86.     Next lcount
    87.  End If
    88.  
    89. End Sub
    90.  
    91.  
    92.  
    93. Private Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    94.     Dim Length&, sCaption$, Temp$, lpClassName$
    95.    
    96.     If m_Identify = WindowClassName Then
    97.        
    98.         '//Class name matching required
    99.    
    100.         'Create a buffer
    101.         lpClassName = Space(256)
    102.         'retrieve the class name
    103.         GetClassName hwnd, lpClassName, 256
    104.         lpClassName = Left$(lpClassName, InStr(1, lpClassName, vbNullChar) - 1)
    105.         If LCase(lpClassName) = LCase(m_ClassOrCaption) Then
    106.             If hwnds(0) <> 0 Then ReDim Preserve hwnds(UBound(hwnds) + 1)
    107.             hwnds(UBound(hwnds)) = hwnd
    108.         End If
    109.        
    110.     Else
    111.        
    112.         '//Caption matching required
    113.        
    114.         sCaption = Space(256)
    115.         GetWindowText hwnd, sCaption, 256
    116.         If Len(sCaption) Then
    117.             sCaption = Left$(sCaption, InStr(1, sCaption, vbNullChar) - 1)
    118.             If LCase(sCaption) Like LCase(m_ClassOrCaption) Then
    119.                 If hwnds(0) <> 0 Then ReDim Preserve hwnds(UBound(hwnds) + 1)
    120.                 hwnds(UBound(hwnds)) = hwnd
    121.             End If
    122.         End If
    123.        
    124.     End If
    125.    
    126.     EnumWindowsProc = True
    127. End Function

    Usage:
    VB Code:
    1. Private Sub Command1_Click()
    2.  CloseApplication "*notepad*", WindowCaption
    3. End Sub

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