Solved

Showing all tasks, and being able to close/kill them, or set focus to them

Posted on 2000-02-23
30
243 Views
Last Modified: 2010-05-02
For instance, Conseal firewall the desktop version shows you all the programs that are running that are using a port.  I would like my program to show all the programs running, even if they are running as services.

Thanks, your help is appreciated.
0
Comment
Question by:ThaSmartUno
  • 16
  • 11
  • 3
30 Comments
 
LVL 9

Expert Comment

by:Ruchi
ID: 2552445
download:

kpd-team.hypermart.net/download/tasks.zip

i think this is what you are looking for??
0
 
LVL 10

Author Comment

by:ThaSmartUno
ID: 2552477
its what im looking for ... but it has too much crap .  For instance there is about 10 of "DDE Server Window" and the same with OleMainThreadWndName, so is there an ez way to get rid of these, without getting rid of a real program?
0
 
LVL 1

Expert Comment

by:Maquiavelo
ID: 2553062
 See www.thescarms.com, there's a sample with source code that list all proccesses running (it's for WIN9x), if you need NT code let me know (I can't access now because I'm on a different partition... ;))
0
 
LVL 10

Author Comment

by:ThaSmartUno
ID: 2553221
I went there and I downloaded 3 files, 1 of them worked, the other 2 gave me errors.  I downloaded Running processes, how to safely kill a process, and how to check if something was not responding.  The first one worked and other 2 were errors.

Could you provide some source code, that will work?

Thanks
0
 
LVL 10

Author Comment

by:ThaSmartUno
ID: 2553324
So if i didn't make the question clear ... i wanted to know how to list em (accomplished) and how to set focus to them (not accomplished) and how to kill them (not accomplished)

Thanks
0
 
LVL 10

Author Comment

by:ThaSmartUno
ID: 2553466
well i found how to terminate the application, at planet-source-code.com ... but i still need to know how to set focus to an app ...
0
 
LVL 1

Expert Comment

by:Maquiavelo
ID: 2556446
 This VBP has a form and a BAS. In the form you have 3 buttons and a listbox.

  Cut & paste this. It should work. I did it and test it myself.

VERSION 5.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   5145
   ClientLeft      =   1650
   ClientTop       =   1650
   ClientWidth     =   7200
   LinkTopic       =   "Form1"
   ScaleHeight     =   5145
   ScaleWidth      =   7200
   Begin VB.CommandButton Command3
      Caption         =   "Set Focus"
      Height          =   495
      Left            =   2400
      TabIndex        =   3
      Top             =   4440
      Width           =   1695
   End
   Begin VB.CommandButton Command2
      Caption         =   "Close"
      Height          =   495
      Left            =   4560
      TabIndex        =   2
      Top             =   4440
      Width           =   1815
   End
   Begin VB.ListBox lstApp
      Height          =   3960
      Left            =   480
      TabIndex        =   1
      Top             =   240
      Width           =   5895
   End
   Begin VB.CommandButton Command1
      Caption         =   "Show running apps"
      Height          =   495
      Left            =   480
      TabIndex        =   0
      Top             =   4440
      Width           =   1455
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub Command1_Click()
    Dim hWnd& 'The same that 'As Long'
    Dim cWinTextLength As String
    Dim cWinText As String * 255
   
    ' Clear the listbox
    lstApp.Clear

    ' The desktop is the highest window
    hWnd& = GetDesktopWindow()

    ' It's first child is the 1st top level window
    hWnd& = GetWindow(hWnd&, GW_CHILD)

    ' Now load all top level windows
    Do
        cWinTextLength = GetWindowText(hWnd&, cWinText, 255)
        If cWinTextLength > 0 Then
            lstApp.AddItem Left(cWinText, cWinTextLength)
            lstApp.ItemData(lstApp.NewIndex) = hWnd&
        End If
        hWnd& = GetWindow(hWnd&, GW_HWNDNEXT)
    Loop While hWnd& <> 0
End Sub


Private Sub Command2_Click()
    If lstApp.ListIndex > -1 Then
        Call SendMessage(lstApp.ItemData(lstApp.ListIndex), WM_CLOSE, 0, 0)
    End If
End Sub

Private Sub Command3_Click()
    Dim hWnd As Long ' handle to window
    Dim x As Long ' work area
    Dim lngWW As Long ' Window Style bits
   
    If lstApp.ListIndex < 0 Then Beep: Exit Sub
   
    ' Get window handle from listbox array
    hWnd = lstApp.ItemData(lstApp.ListIndex)
   
    ' Get style bits for window
    lngWW = GetWindowLong(hWnd, GWL_STYLE)
   
    ' If minimized do a restore
    If lngWW And WS_MINIMIZE Then
    x = ShowWindow(hWnd, SW_RESTORE)
    End If
   
    ' Move window to top of z-order/activate; no move/resize
    x = SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)
End Sub

This is the bas module:

Attribute VB_Name = "Module1"
Option Explicit

Public Const WM_CLOSE = &H10

Public Const GW_HWNDNEXT = 2
Public Const GW_CHILD = 5

Public Const GWL_STYLE = (-16)

Public Const SW_RESTORE = 9

Public Const WS_MINIMIZE = &H20000000

Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const SWP_SHOWWINDOW = &H40

Public Const HWND_TOP = 0


Public Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal wIndx As Long) As Long
Public Declare Function GetDesktopWindow Lib "User32" () As Long
Public Declare Function GetWindow Lib "User32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function ShowWindow Lib "User32" (ByVal hWnd As Long, ByVal flgs As Long) As Long
Public Declare Function SetWindowPos Lib "User32" (ByVal hWnd As Long, ByVal insaft As Long, ByVal x%, ByVal y%, ByVal cx%, ByVal cy%, ByVal flgs As Long) As Long

'This functions sends a msg
Public Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long


0
 
LVL 10

Author Comment

by:ThaSmartUno
ID: 2556923
this has the kill statement, but its just like the link thar Ruchi gave.

There is multiple items of the same program, and there are many different items called "DDE Server Window" and "OleMainThreadWndName"

so is there a way to take all the duplicates out and all the extra crap? ... thanks
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2558607
0
 
LVL 10

Author Comment

by:ThaSmartUno
ID: 2560214
can i spilt the points? ... meaning make 2 more questions, making one names "Points for Ruchi" and "Points for Maquiavelo"  with 75 pts a piece? ... cuz u both helped and thanks
0
 
LVL 1

Expert Comment

by:Maquiavelo
ID: 2560222
Try this one now... shows no crap...

VERSION 5.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   5145
   ClientLeft      =   1650
   ClientTop       =   1650
   ClientWidth     =   7200
   LinkTopic       =   "Form1"
   ScaleHeight     =   5145
   ScaleWidth      =   7200
   Begin VB.CommandButton cmdSetFocus
      Caption         =   "Set Focus"
      Height          =   495
      Left            =   2400
      TabIndex        =   3
      Top             =   4440
      Width           =   1695
   End
   Begin VB.CommandButton cmdClose
      Caption         =   "Close"
      Height          =   495
      Left            =   4560
      TabIndex        =   2
      Top             =   4440
      Width           =   1815
   End
   Begin VB.ListBox lstApp
      Height          =   3960
      Left            =   480
      TabIndex        =   1
      Top             =   240
      Width           =   5895
   End
   Begin VB.CommandButton cmdShowRunningApps
      Caption         =   "Show running apps"
      Height          =   495
      Left            =   480
      TabIndex        =   0
      Top             =   4440
      Width           =   1455
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub cmdShowRunningApps_Click()
    Dim hwnd&
    Dim res&
    Dim Iconic&
    Dim cWinTextLength As String
    Dim cWinText As String * 255
   
    ' Clear the listbox
    lstApp.Clear

    ' The desktop is the highest window
    hwnd& = GetDesktopWindow()

    ' It's first child is the 1st top level window
    hwnd& = GetWindow(hwnd&, GW_CHILD)

    ' Now load all top level windows
    Do
        cWinTextLength = GetWindowText(hwnd&, cWinText, 255)
        If cWinTextLength > 0 Then
            res& = IsWindowVisible(hwnd&) 'Check if window is visible
            Iconic& = IsIconic(hwnd&) 'Check if window is an icon in the systray
            If Iconic& = 0 Then
                If res& <> 0 Then
                    res& = GetWindow(hwnd&, GW_OWNER) 'Check if the window has an owner (this prevents more than one window for each app)
                    If res& = 0 Then
                        lstApp.AddItem Left(cWinText, cWinTextLength)
                        lstApp.ItemData(lstApp.NewIndex) = hwnd&
                    End If
                End If
            Else
                lstApp.AddItem Left(cWinText, cWinTextLength)
                lstApp.ItemData(lstApp.NewIndex) = hwnd&
            End If
        End If
        hwnd& = GetWindow(hwnd&, GW_HWNDNEXT)
    Loop While hwnd& <> 0
End Sub


Private Sub cmdClose_Click()
    If lstApp.ListIndex > -1 Then
        Call SendMessage(lstApp.ItemData(lstApp.ListIndex), WM_CLOSE, 0, 0)
    End If
End Sub

Private Sub cmdSetFocus_Click()
    Dim hwnd As Long ' handle to window
    Dim x As Long ' work area
    Dim lngWW As Long ' Window Style bits
   
    If lstApp.ListIndex < 0 Then Beep: Exit Sub
   
    ' Get window handle from listbox array
    hwnd = lstApp.ItemData(lstApp.ListIndex)
   
    ' Get style bits for window
    lngWW = GetWindowLong(hwnd, GWL_STYLE)
   
    ' If minimized do a restore
    If lngWW And WS_MINIMIZE Then
    x = ShowWindow(hwnd, SW_RESTORE)
    End If
   
    ' Move window to top of z-order/activate; no move/resize
    x = SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)
End Sub

-----------------------------------------------------------------
This is the BAS code:

Attribute VB_Name = "Module1"
Option Explicit

Public Const WM_ACTIVATE = &H6
Public Const WM_CLOSE = &H10

Public Const GW_HWNDNEXT = 2
Public Const GW_CHILD = 5
Public Const GW_OWNER = 4

Public Const GWL_STYLE = (-16)

Public Const SW_RESTORE = 9
Public Const SW_HIDE = 0

Public Const WS_MINIMIZE = &H20000000

Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const SWP_SHOWWINDOW = &H40

Public Const HWND_TOP = 0

Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal wIndx As Long) As Long
Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal flgs As Long) As Long
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal insaft As Long, ByVal x%, ByVal y%, ByVal cx%, ByVal cy%, ByVal flgs As Long) As Long
Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long

'This functions sends a msg
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
0
 
LVL 10

Author Comment

by:ThaSmartUno
ID: 2560262
actually ... i have one question ... could one of you make a function call SetAppFocus  that will Set Focus to the App, like the one Maquiavelo did ... but without using a list, only using the text sent with the function,

for ex. something = SetAppFocus("x:\windows\notepad.exe")

and it would setfocus to notepad ... thanks
0
 
LVL 10

Author Comment

by:ThaSmartUno
ID: 2560268
Your close doesn't work.
0
 
LVL 10

Author Comment

by:ThaSmartUno
ID: 2560270
no wait it works
0
 
LVL 1

Accepted Solution

by:
Maquiavelo earned 150 total points
ID: 2561145
 Regarding the following:

actually ... i have one question ... could one of you make a function call SetAppFocus  that will Set Focus to the App, like the one Maquiavelo did ... but without using a list, only using the text sent with the function,

for ex. something = SetAppFocus("x:\windows\notepad.exe")

and it would setfocus to notepad ... thanks

  even thoug it would be another question, wich means more points :),

you can iterate through the running processes till you find the one you're looking for (i.e.: notepad.exe), (you can do this with the sample from thescarms.com), once there find the associated window handle to that process and finally use the SendMessage function with that handle.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 10

Author Comment

by:ThaSmartUno
ID: 2561237
why would this be another question? ... this is part of my total question...

Title: "Showing all tasks, and being able to close/kill them, or set focus to them"

and I can't get your code into a function because it heavily depends on the list
0
 
LVL 10

Author Comment

by:ThaSmartUno
ID: 2562103
Actually it was the answer before this ... the one "without the crap" =) thanks, I figured out a way to incorporate it into my program.  If you want to do the function you can.  But you don't have to.

And Ruchi thanks, but he provided the code to everything.


Thanks again to both of ya.
0
 
LVL 1

Expert Comment

by:Maquiavelo
ID: 2562862
 What function you mean? The notepad.exe example?
0
 
LVL 10

Author Comment

by:ThaSmartUno
ID: 2562959
something = SetAppFocus("x:\windows\notepad.exe")


that one
0
 
LVL 1

Expert Comment

by:Maquiavelo
ID: 2563206
 Ok. Let me work on it. ;)
0
 
LVL 1

Expert Comment

by:Maquiavelo
ID: 2563455
 OK here it is.

You should add this code to the previous sample I sent you, because
there are there some declarations U still need. For this to work
U have to enter the whole path i.e.: c:\windows\notepad.exe. U can solve
this where I put the *.

Public Sub SetFocusApp()
    Dim IBox As String
    Dim lSnapShot  As Long
    Dim r          As Long
    Dim uProcess   As PROCESSENTRY32
    Dim hWnd As Long
   
    IBox = InputBox("")
    If Len(Trim(IBox)) > 0 Then
        lSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
        If lSnapShot <> 0 Then
            uProcess.lSize = Len(uProcess)
            r = ProcessFirst(lSnapShot, uProcess)
            Do While r
            '*: Here you can manipulate strings so that work without the whole path
                If Trim(UCase(IBox)) = Left(uProcess.sExeFile, InStr(1, uProcess.sExeFile, vbNullChar) - 1) Then
                    'Now look 4 the window that has this PID
                    hWnd = FindWin(uProcess.lProcessId)
                    'If found show it
                    If hWnd > 0 Then Call ShowWindow(hWnd, SW_RESTORE)
                End If
                'sParentPid = Hex$(uProcess.lParentProcessId)
                r = ProcessNext(lSnapShot, uProcess)
            Loop
            CloseHandle (lSnapShot)
        End If
    End If
End Sub

This goes to the BAS

Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Public Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Public Declare Function EnumThreadWindows Lib "user32" (ByVal dwThreadId As Long, ByVal lpfn As Long, ByVal lParam As Long) As Long
Public Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long


'-------------
Public Const MAX_PATH = 260
Public Const TH32CS_SNAPPROCESS = 2&

Public Type PROCESSENTRY32
    lSize            As Long
    lUsage           As Long
    lProcessId       As Long
    lDefaultHeapId   As Long
    lModuleId        As Long
    lThreads         As Long
    lParentProcessId As Long
    lPriClassBase    As Long
    lFlags           As Long
    sExeFile         As String * MAX_PATH
End Type

Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessId As Long) As Long
Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long

0
 
LVL 1

Expert Comment

by:Maquiavelo
ID: 2563461
 Sorry forget the FindWin function.

Private Function FindWin(PID As Long) As Long
    Dim hWnd&
    Dim res&
    Dim hProc&
   
    ' The desktop is the highest window
    hWnd& = GetDesktopWindow()

    ' It's first child is the 1st top level window
    hWnd& = GetWindow(hWnd&, GW_CHILD)

    ' Now load all top level windows
    Do
        res& = GetWindowThreadProcessId(hWnd&, hProc&)
        If res& <> 0 Then
            If hProc& = PID Then
                FindWin = hWnd&
                Exit Function
            End If
        End If
        hWnd& = GetWindow(hWnd&, GW_HWNDNEXT)
    Loop While hWnd& <> 0
End Function
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2563861
No problem, thasmartuno!
Did you get it working?
0
 
LVL 10

Author Comment

by:ThaSmartUno
ID: 2566390
Well thanks for trying the function, but it didn't work.  That's ok though.

I got it working using the list.
0
 
LVL 1

Expert Comment

by:Maquiavelo
ID: 2566951
 Weird... it did work on my PC! What err did U get?
0
 
LVL 10

Author Comment

by:ThaSmartUno
ID: 2566988
nothing happened ... i called the function ... and nothing happened.
0
 
LVL 1

Expert Comment

by:Maquiavelo
ID: 2574987
 Sorry for asking this, but wich function did you call?

  You have to do sth like this:

call SetAppFocus()

  I can't believe it does not show a thing! At least should show the InputBox dialog!!

Note: the app has to be running for the function to work.
0
 
LVL 10

Author Comment

by:ThaSmartUno
ID: 2575173
the input box shows ... and i ran notepad.exe ... and i put in the input box "C:\windows\notepad.exe"

without quotes ...
0
 
LVL 1

Expert Comment

by:Maquiavelo
ID: 2578658
 And...

1) you have the notepad window minimized
2) this minimized window does not restore??
0
 
LVL 10

Author Comment

by:ThaSmartUno
ID: 2582176
not minimized just not focused
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now