Results 1 to 2 of 2

Thread: Terminating window processes

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2001
    Posts
    10

    Terminating window processes

    hello,
    I wish to terminate all running applications from my visual basic program. I got to know that there are 2 APIs GetExitCodeProcess and Terminate Process. I do not know hot to use them . Can a method be suggested employing these two APIs so that all running window processes be terminated. This is badly needed for me now.
    I work for ITE Singapore and a native from India.

    Thanks and Regards
    Sathesh

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

    Sure thing Bud

    Try this code out for size:


    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 IsWindowVisible& Lib "user32" (ByVal hwnd As Long)
    11. Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
    12. Private Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    13. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    14.  
    15. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    16. Const PROCESS_ALL_ACCESS = &H1F0FFF
    17.  
    18. Private Declare Function TerminateProcess& Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long)
    19. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    20. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    21.  
    22. Private m_hTaskBar          As Long
    23. Private m_hDeskTopIcons     As Long
    24.  
    25. Public Sub CloseExceptUs()
    26.     ';->';->';->';->';->';->';->';->';->';->';->';->';->';->
    27.     ' Author:   Nucleus                                     *
    28.     ' Location: VB World Forum                              *
    29.     ' Purpose:  Close all applications except this one      *
    30.     ';->';->';->';->';->';->';->';->';->';->';->';->';->';->
    31.    
    32.     m_hDeskTopIcons = FindWindowEx(0&, 0&, "Progman", vbNullString)
    33.     m_hTaskBar = FindWindowEx(0&, 0&, "Shell_TrayWnd", vbNullString)
    34.     EnumWindows AddressOf EnumWindowsProc, 0&
    35. End Sub
    36.  
    37.  
    38.  
    39. Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    40. Dim lThreadID               As Long
    41. Dim lPid                    As Long
    42. Dim lHp                     As Long
    43. '
    44. ' If the window is not desktop icons or taskabar
    45. '
    46. If hwnd <> m_hTaskBar And hwnd <> m_hDeskTopIcons Then
    47.     '
    48.     ' Get ThreadID and Process Id from hwnd
    49.     '
    50.     lThreadID = GetWindowThreadProcessId(hwnd, lPid)
    51.     '
    52.     ' If the ThreadId is not from this application
    53.     '
    54.     If lThreadID <> App.ThreadID Then
    55.         '
    56.         ' Check if the window is visible
    57.         '
    58.         If IsWindowVisible(hwnd) Then
    59.             '
    60.             ' Tell the window to close gently, give it a timeout in case it does not respond
    61.             '
    62.             SendMessageTimeout hwnd, WM_SYSCOMMAND, SC_CLOSE, 0, 0, 500, 0
    63.             '
    64.             ' If the window doesn't close via gently persuasion, bring out the nipple screws to force it to close
    65.             '
    66.             If IsWindow(hwnd) Then
    67.                 lHp = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPid)
    68.                 TerminateProcess lHp&, 0&
    69.                 CloseHandle lHp
    70.             End If
    71.             '
    72.         End If
    73.         '
    74.     End If
    75.     '
    76. End If
    77.  
    78. EnumWindowsProc = 1
    79.  
    80. End Function

    Usage:
    VB Code:
    1. Call CloseExceptUs

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