Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Only find applications visible on the Taskbar? 2

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
GB
I'm using Solution 2 from this faq:

Can I modify it to only fill the list with applications which are visible on the Taskbar rather than all EXEs?

- Andy
___________________________________________________________________
If a man speaks in a forest and there are no women around to hear him - will he still be wrong?
 
Sure sounds like you're trying to create spyware. I can't think of a legitimate reason for this. Maybe you can offer a hint?
 
A friend of mine wants a utility which will close an application if it's open for more than 20 minutes (his application links to a wireless camera via a satellite link and people in the office keep forgetting to close the link which racks up a big phone bill). I've already written the app and it works fine, it's just that it lists all the processes rather than just the ones which appear on the "Applications" tab in Task Manager. I simply wanted to narrow the list down to the 'major' applications.

- Andy
___________________________________________________________________
If a man speaks in a forest and there are no women around to hear him - will he still be wrong?
 
These two are not the same:

>just the ones which appear on the "Applications" tab in Task Manager
>applications which are visible on the Taskbar

Which do you want? The latter is trickier, but if the former, then something like the following should work:

Code:
[blue]Option Explicit

Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long

Private Const GW_OWNER = 4
Private Const GWL_STYLE = (-16)
Private Const WS_VISIBLE = &H10000000

Public Sub Main()
   EnumWindows AddressOf EnumWindowsProc, ByVal 0&
End Sub

Private Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Boolean
     Dim sSave As String, Ret As Long
     sSave = Space(255)
     Ret = GetWindowText(hWnd, sSave, 255)
     If Ret And (GetWindow(hWnd, GW_OWNER) = 0) And ((GetWindowLong(hWnd, GWL_STYLE) And WS_VISIBLE) <> 0) Then Debug.Print Left(sSave, Ret)
     [green]' continue enumeration[/green]
     EnumWindowsProc = True
End Function[/blue]
 
Do all the apps use a particular DLL? Have a look at EnumProcesses and EnumProcessModules. You can then pick out the ones that use specific DLLs or specific process names and kill them off.
 
Thanks strongm - is it possible to reveal the name of EXE rather than the caption of the window? So for example it would return Firefox.exe rather than "Visual Basic(Microsoft): Version 5 & 6 - Only find applications visible on the Taskbar?". Sorry I didn't make that clear in my original question but the FAQ to which I referred returns the names of the EXEs and I want to narrow that list down to the EXEs listed by your code above.

- Andy
___________________________________________________________________
If a man speaks in a forest and there are no women around to hear him - will he still be wrong?
 
Sure. You just need this function; just pass it the hWnd and it'll return the associated executablepath:

Code:
[blue]Option Explicit
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long

Public Function GetModuleName(hWnd As Long) As String
    Dim objWMIService As Object
    Dim colItems As Object
    Dim oProcess As Object
    Dim result As Long
    Dim lPID As Long
    
    result = GetWindowThreadProcessId(hWnd, lPID) [green]' we want process associated with window[/green]
       
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colItems = objWMIService.execquery("select * from Win32_Process where ProcessID = " & lPID)

    [green]' there'll be 0 or 1 result[/green]
    For Each oProcess In colItems
        GetModuleName = oProcess.executablepath
    Next

End Function[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top