Results 1 to 8 of 8

Thread: Running Applications

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2003
    Posts
    33

    Running Applications

    Can i see what applications are running in vb?

    If i can how do you close them?

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    are you talking about creating a vb application that is like the task manager?
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2003
    Posts
    33
    Yes

  4. #4
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    Use the FindWindow() API.

  5. #5
    Addicted Member Illiad's Avatar
    Join Date
    Mar 2003
    Location
    Chicago
    Posts
    196
    This is a huge chunk of API, i tried to make a program like that. Not knowing really any API (yet alone what API stands for!). When I posted they said i'd be to huge. It is as simple as FindWindow()?

    I think that FindWindow() Can only "search" for an open form caption. I think he wants to generate a list of all the currently running applications.
    Quis Custodiet Ipsos Custodes?

    You don't have to be faster than the bear, you just have to be faster than the slowest person running from the bear.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    This will return the parent window captions. Even if the caption is
    a nullstring for all running parent windows.
    Modify this code to incorporate the FindWindowEx API to get the
    child windows. You can not use FindWindow API to find running
    processes. Note - services are not listed.

    Code:
    Option Explicit
    
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" _
    (ByVal hwnd As Long) As Long
    
    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 Const GW_HWNDNEXT = 2
    
    Private Sub Command1_Click()
        
        Dim hwndP As Long
        Dim sStr As String
        
        hwndP = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
        Do While hwndP <> 0
            sStr = String(GetWindowTextLength(hwndP) + 1, Chr$(0))
            GetWindowText hwndP, sStr, Len(sStr)
            sStr = Left$(sStr, Len(sStr) - 1)
            MsgBox "Caption - [" & sStr & "]", vbOKOnly + vbInformation, "All Running Parent Windows"
            hwndP = GetWindow(hwndP, GW_HWNDNEXT)
        Loop
        MsgBox "Done!", vbOKOnly + vbInformation
        
    End Sub
    If you want to duplicate Task Manager then you need to use different API to enumerate running processes.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    For duplicating Task Manager.
    In a module...
    Code:
    Option Explicit
    
    Public Declare Function Process32First Lib "kernel32" ( _
    ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    
    Public Declare Function Process32Next Lib "kernel32" ( _
    ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    
    Public Declare Function CloseHandle Lib "Kernel32.dll" _
    (ByVal Handle As Long) As Long
    
    Public Declare Function OpenProcess Lib "Kernel32.dll" ( _
    ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
    ByVal dwProcId As Long) As Long
    
    Public Declare Function EnumProcesses Lib "psapi.dll" _
    (ByRef lpidProcess As Long, ByVal cb As Long, _
    ByRef cbNeeded As Long) As Long
    
    Public Declare Function GetModuleFileNameExA Lib "psapi.dll" _
    (ByVal hProcess As Long, ByVal hModule As Long, _
    ByVal ModuleName As String, ByVal nSize As Long) As Long
    
    Public Declare Function EnumProcessModules Lib "psapi.dll" _
    (ByVal hProcess As Long, ByRef lphModule As Long, _
    ByVal cb As Long, ByRef cbNeeded As Long) As Long
    
    Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" ( _
    ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    
    Public Declare Function GetVersionExA Lib "kernel32" _
    (lpVersionInformation As OSVERSIONINFO) As Integer
    
    Public Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long           ' This process
        th32DefaultHeapID As Long
        th32ModuleID As Long            ' Associated exe
        cntThreads As Long
        th32ParentProcessID As Long     ' This process's parent process
        pcPriClassBase As Long          ' Base priority of process threads
        dwFlags As Long
        szExeFile As String * 260       ' MAX_PATH
    End Type
    
    Public Type OSVERSIONINFO
        dwOSVersionInfoSize As Long
        dwMajorVersion As Long
        dwMinorVersion As Long
        dwBuildNumber As Long
        dwPlatformId As Long           '1 = Windows 95/98.
                                       '2 = Windows NT
        szCSDVersion As String * 128
    End Type
    
    Public Const PROCESS_QUERY_INFORMATION = 1024
    Public Const PROCESS_VM_READ = 16
    Public Const MAX_PATH = 260
    Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
    Public Const SYNCHRONIZE1 = &H100000
    Public Const SYNCHRONIZE = &H100000 Or STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE1 Or &HFFF
    Public Const PROCESS_ALL_ACCESS = &H1F0FFF
    Public Const TH32CS_SNAPPROCESS = &H2&
    Public Const hNull = 0
    
    Public Function StrZToStr(s As String) As String
    
        StrZToStr = Left$(s, Len(s) - 1)
        
    End Function
    
    Public Function getVersion() As Long
    
        Dim osinfo As OSVERSIONINFO
        Dim retvalue As Integer
        
        osinfo.dwOSVersionInfoSize = 148
        osinfo.szCSDVersion = Space$(128)
        retvalue = GetVersionExA(osinfo)
        getVersion = osinfo.dwPlatformId
        
    End Function
    More to come for behind form.
    Last edited by RobDog888; May 13th, 2003 at 02:03 AM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Behind form (frmProcesses), one command button (Command1), one List box (List1), and one label (Lable1).
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        
        List1.Clear
        Call CheckProcess
        
    End Sub
    
    Private Function CheckProcess() As Boolean
        
        Dim sPro As String
        
        Select Case getVersion()
    
            Case 1 'WINDOWS 95/98 PLATFORM
            
                Dim f As Long, sname As String
                Dim hSnap As Long, proc As PROCESSENTRY32
                
                hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
                If hSnap = hNull Then Exit Function
                proc.dwSize = Len(proc)
                'ITERATE THROUGH THE PROCESSES
                f = Process32First(hSnap, proc)
                Do While f
                    sPro = StrZToStr(proc.szExeFile)
                    'TODO: TEST THIS PART OUT
                    f = Process32Next(hSnap, proc)
                Loop
    
            Case 2 'WINDOWS NT PLATFORM
    
                Dim cb As Long
                Dim cbNeeded As Long
                Dim NumElements As Long
                Dim ProcessIDs() As Long
                Dim cbNeeded2 As Long
                Dim NumElements2 As Long
                Dim Modules(1 To 200) As Long
                Dim lRet As Long
                Dim ModuleName As String
                Dim nSize As Long
                Dim hProcess As Long
                Dim I As Long
                
                'GET THE ARRAY CONTAINING THE PROCESS ID'S FOR EACH PROCESS OBJECT
                cb = 8
                cbNeeded = 96
                Do While cb <= cbNeeded
                    DoEvents
                    cb = cb * 2
                    ReDim ProcessIDs(cb / 4) As Long
                    lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded)
                Loop
                NumElements = cbNeeded / 4
    
                For I = 1 To NumElements
                    DoEvents
                    'GET A HANDLE TO THE PROCESS
                    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
                    Or PROCESS_VM_READ, 0, ProcessIDs(I))
                    'GOT A PROCESS HANDLE
                    If hProcess <> 0 Then
                        'GET AN ARRAY OF THE MODULE HANDLESFOR THE SPECIFIED PROCESS
                        lRet = EnumProcessModules(hProcess, Modules(1), 200, _
                                                  cbNeeded2)
                        'IF THE MODULE ARRAY IS RETRIEVED, GET THE MODULEFILENAME
                        If lRet <> 0 Then
                            ModuleName = Space(MAX_PATH)
                            nSize = 500
                            lRet = GetModuleFileNameExA(hProcess, Modules(1), _
                                            ModuleName, nSize)
                            'TRIM PROGRAM EXE NAME
                            sPro = Mid(ModuleName, InStrRev(ModuleName, "\") + 1)
                            sPro = Mid(sPro, 1, Len(Trim(sPro)) - 1)
                            If Len(sPro) <> 0 Then
                                List1.AddItem sPro
                            End If
                        End If
                    End If
                    lRet = CloseHandle(hProcess)
                Next
                Label1 = "Processes: " & frmProcesses.List1.ListCount
          End Select
    
    End Function
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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