Results 1 to 16 of 16

Thread: Close an application

  1. #1

    Thread Starter
    Frenzied Member seoptimizer2001's Avatar
    Join Date
    Apr 2001
    Location
    Toledo, Ohio USA GMT -5
    Posts
    1,075

    Question Close an application

    Is it possible to close another application from a VB program if it is not shelled?
    seoptimizer2001
    VB 6.0, VC++, VI, ASP, JavaScript, HTML,
    Perl, XML, SQL Server 2000

    If God had intended us to drink beer, He would have given us stomachs.


    Please use the [code] and [vbcode] tags in your posts!
    If you don't know how to use them please go HERE!


  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Certainly is, provided you know the window caption
    VB Code:
    1. 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
    2. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    3.  
    4. Const WM_CLOSE = &H10
    5.  
    6. Private Sub Command1_Click()
    7.  
    8. Dim intHwnd As Long
    9.  
    10. intHwnd = FindWindow(vbNullString, "Calculator") ' Must match the caption of the window exactly
    11. SendMessage intHwnd, WM_CLOSE, 0&, 0&
    12.  
    13. End Sub

  3. #3

    Thread Starter
    Frenzied Member seoptimizer2001's Avatar
    Join Date
    Apr 2001
    Location
    Toledo, Ohio USA GMT -5
    Posts
    1,075
    Thanks alot!
    seoptimizer2001
    VB 6.0, VC++, VI, ASP, JavaScript, HTML,
    Perl, XML, SQL Server 2000

    If God had intended us to drink beer, He would have given us stomachs.


    Please use the [code] and [vbcode] tags in your posts!
    If you don't know how to use them please go HERE!


  4. #4
    Member
    Join Date
    Aug 2001
    Posts
    46
    Can you get the exact name of running tasks from VB?

  5. #5
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    This code will fill a listbox with the caption of each open "window". This includes all programs in the task bar and any windows within those programs

    In a Module
    VB Code:
    1. Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    2. 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
    3. Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
    4.  
    5. Private Const WM_GETTEXT = &HD
    6.  
    7. Private aCaptions() As String
    8. Private lCount As Long
    9.  
    10. Public Function GetAllCaptions() As Variant
    11.  
    12. lCount = 0
    13.  
    14. Call EnumWindows(AddressOf EnumWindowsProc, 0&)
    15.  
    16. If lCount Then ReDim Preserve aCaptions(lCount - 1)
    17.     GetAllCaptions = IIf(lCount, aCaptions, Array())
    18. End Function
    19.  
    20. Private Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    21.  
    22. Dim sBuffer As String * 260
    23.  
    24. If IsWindowVisible(hwnd) Then
    25.     ReDim Preserve aCaptions(lCount)
    26.     aCaptions(lCount) = Left(sBuffer, SendMessage(hwnd, WM_GETTEXT, 260, ByVal sBuffer))
    27.     If Len(Trim(aCaptions(lCount))) Then lCount = lCount + 1
    28. End If
    29.  
    30. EnumWindowsProc = hwnd
    31.  
    32. End Function

    In a form (with a List1 and a Command1)
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. Dim vCaps As Variant
    4. Dim lIndex As Long
    5.  
    6. vCaps = GetAllCaptions()
    7. List1.Clear
    8.  
    9. For lIndex = 0 To UBound(vCaps)
    10.     List1.AddItem vCaps(lIndex)
    11. Next
    12.  
    13. End Sub

  6. #6

    Thread Starter
    Frenzied Member seoptimizer2001's Avatar
    Join Date
    Apr 2001
    Location
    Toledo, Ohio USA GMT -5
    Posts
    1,075
    That's an even bigger help
    seoptimizer2001
    VB 6.0, VC++, VI, ASP, JavaScript, HTML,
    Perl, XML, SQL Server 2000

    If God had intended us to drink beer, He would have given us stomachs.


    Please use the [code] and [vbcode] tags in your posts!
    If you don't know how to use them please go HERE!


  7. #7
    Member
    Join Date
    Aug 2001
    Posts
    46
    Originally posted by chrisjk
    This code will fill a listbox with the caption of each open "window". This includes all programs in the task bar and any windows within those
    Thanks!

  8. #8
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255
    Code:
    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
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    Const WM_CLOSE = &H10
    
    Private Sub Command1_Click()
    
    Dim intHwnd As Long
    
    intHwnd = FindWindow(vbNullString, "Calculator") ' Must match the caption of the window exactly
    SendMessage intHwnd, WM_CLOSE, 0&, 0&
    
    End Sub
    I have a strong suspicion this won't work on Windows NT. Anyone know for sure ?
    VB6 Enterprise SP4

  9. #9
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    I built it using 2k

  10. #10
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255
    I'll try it at work tomorrow and let you know.
    VB6 Enterprise SP4

  11. #11
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    yeah, thanks. NT and 2K are similar, but you never know with MS

  12. #12
    Tygur
    Guest
    Originally posted by tcurrier
    I have a strong suspicion this won't work on Windows NT. Anyone know for sure ?
    I looked at it and I can see no reason why it wouldn't work on WinNT. It should work with no problem.

  13. #13
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255
    Sorry guys! False alarm! It works fine on Windows NT 4.0 !!!
    VB6 Enterprise SP4

  14. #14
    Member
    Join Date
    Aug 2001
    Posts
    46
    Originally posted by tcurrier

    I have a strong suspicion this won't work on Windows NT. Anyone know for sure ?
    How about 98/me?

  15. #15
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    98 has no problems with that code

  16. #16
    Blacknight
    Guest
    Hey chrisjk,
    what do u mean by you built it, how do build stuff like that and how do u know which functions to call?

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