Results 1 to 3 of 3

Thread: List of Running Applications

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Posts
    20

    List of Running Applications

    This is, again, for my shell replacement for windows. Is there some kind of API calls or method I can use to get the titles of the currently running applications?

    Being able to get the Hwnd of an application based on it's title would be helpful too.

    And finally, how can I Maximize/Minimize/Close an application assuming I know it's Hwnd?


    Thanks.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    To get a list of running applications, just do a search on that topic in this forum. This question is asked a lot, and there are lots of examples.

    VB Code:
    1. 'Here are two ways to close a running application.  
    2. 'First, by the full path and Exe name.
    3. Option Explicit
    4. 'Originally posted by Evan
    5. '12/08/2001
    6. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    7. Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    8. Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    9. Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
    10. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    11. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    12.  
    13. Private Type PROCESSENTRY32
    14.     dwSize As Long
    15.     cntUsage As Long
    16.     th32ProcessID As Long
    17.     th32DefaultHeapID As Long
    18.     th32ModuleID As Long
    19.     cntThreads As Long
    20.     th32ParentProcessID As Long
    21.     pcPriClassBase As Long
    22.     dwFlags As Long
    23.     szexeFile As String * 6400
    24. End Type
    25.  
    26. Private Function KillAppByName(MyName As String) As Boolean
    27.     Const PROCESS_ALL_ACCESS = 0
    28.     Dim uProcess As PROCESSENTRY32
    29.     Dim rProcessFound As Long
    30.     Dim hSnapshot As Long
    31.     Dim szExename As String
    32.     Dim exitCode As Long
    33.     Dim myProcess As Long
    34.     Dim AppKill As Boolean
    35.     Dim appCount As Integer
    36.     Dim i As Integer
    37.     On Local Error GoTo Finish
    38.     appCount = 0
    39.    
    40.     Const TH32CS_SNAPPROCESS As Long = 2&
    41.    
    42.     uProcess.dwSize = Len(uProcess)
    43.     hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    44.     rProcessFound = ProcessFirst(hSnapshot, uProcess)
    45.    
    46.     Do While rProcessFound
    47.         i = InStr(1, uProcess.szexeFile, Chr(0))
    48.         szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
    49.         If Right$(szExename, Len(MyName)) = LCase$(MyName) Then
    50.             KillAppByName = True
    51.             appCount = appCount + 1
    52.             myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
    53.             AppKill = TerminateProcess(myProcess, exitCode)
    54.             Call CloseHandle(myProcess)
    55.         End If
    56.         rProcessFound = ProcessNext(hSnapshot, uProcess)
    57.     Loop
    58.  
    59.     Call CloseHandle(hSnapshot)
    60. Finish:
    61. End Function
    62. 'Usage:
    63. Private Sub Command1_Click()
    64. KillAppByName "C:\Windows\notepad.exe"
    65. End Sub
    66.  
    67. 'Next by using the Windows caption
    68. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    69. Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    70.  
    71. Private Const WM_CLOSE = &H10
    72.  
    73. Private Sub Command1_Click()
    74. Dim CloseIt As Long
    75. CloseIt = FindWindow(vbNullString, "Caption Of Window To Be Closed")
    76. PostMessage CloseIt, WM_CLOSE, CLng(0), CLng(0)
    77. End Sub
    78.  
    79. 'This will minimize all Open Windows.    To Minimize just one open window, replace "shell_traywnd"
    80. 'with the Window's caption
    81.  
    82. 'Code taken from "Ask The VB Pro", May 1999 Edition of
    83. 'Visual Basic Programs Journal.   Written by Phil Weber
    84.  
    85. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    86. Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    87.  
    88. Private Const WM_COMMAND As Long = &H111
    89. Private Const MIN_ALL As Long = 419
    90. Private Const MIN_ALL_UNDO As Long = 416
    91.  
    92. Private Sub Command1_Click()
    93. Dim DoIt As Long
    94. DoIt = FindWindow("shell_traywnd", vbNullString)
    95. 'to minimize them all
    96. Call PostMessage(DoIt, WM_COMMAND, MIN_ALL, 0&)
    97. 'to return them all to normal size
    98. Call PostMessage(DoIt, WM_COMMAND, MIN_ALL_UNDO, 0&)
    99. End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Posts
    20

    Thanks

    Thanks for the code, and thanks for not flaming me for being such a newbie to this forum.

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