Results 1 to 7 of 7

Thread: how do i show all running tasks?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299
    I want to make a small program, and its only purpose is to show a list of running tasks (all tasks, not just the ones shown in the task list) ie including all services, in VB. Does anyone have a link or some code, to help me along this path?
    Any help would be appreciated.
    BW

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409

    Code:
    Private Declare Function GetWindow Lib "user32" _
    (ByVal hwnd As Long, ByVal wCmd 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
    Const GW_HWNDFIRST = 0
    Const GW_HWNDNEXT = 2
    
    Private Sub Command1_Click()
    Dim CurrWnd As Long
    Dim Length As Long
    Dim TaskName As String
    Dim Parent As Long
    List1.Clear 'add a listbox
    CurrWnd = GetWindow(Form1.hwnd, GW_HWNDFIRST)
    Do While CurrWnd <> 0
    TaskName = Space(255)
    Length = GetWindowText(CurrWnd, TaskName, 255)
    If Length > 0 Then
    List1.AddItem TaskName
    End If
    CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
    Loop
    End Sub
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    That works.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299

    it sure does. but..

    can i go a step further and show the associated exe files for each item shown?
    Is that possible? Thats exactly what i wanted in the code above, but i want to also relate each item to the exe or dll it comes from. Sorta like what MSInfo32 does.
    BW
    PS Thanks guys for the quick replies.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Why not enumarating the processes instead?
    Code:
    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.
                                            '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 SYNCHRONIZE = &H100000
          'STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF
          Public Const PROCESS_ALL_ACCESS = &H1F0FFF
          Public Const TH32CS_SNAPPROCESS = &H2&
          Public Const hNull = 0
    
          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
    
    Function Processes() As String()
        Dim Arr$(): ReDim Arr(0)
        Select Case getVersion()
            Case 1 'Windows 95/98
                Dim f&, sname$, hSnap&, 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
                    Arr(UBound(Arr)) = StrZToStr(proc.szExeFile)
                    ReDim Preserve Arr(UBound(Arr) + 1)
                    f = Process32Next(hSnap, proc)
                Loop
            Case 2 'Windows NT
                Dim cb&, cbNeeded&, NumElements&, ProcessIDs&(), cbNeeded2&, NumElements2&, Modules&(1 To 200), lRet&, ModuleName$, nSize&, hProcess&, i&
                'Get the array containing the process id's for each process object
                cb = 8
                cbNeeded = 96
                Do While cb <= cbNeeded
                    cb = cb * 2
                    ReDim ProcessIDs(cb / 4) As Long
                    lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded)
                Loop
                NumElements = cbNeeded / 4
                For i = 1 To NumElements
                    '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 handles for 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)
                            Arr(UBound(Arr)) = Left(ModuleName, lRet)
                            ReDim Preserve Arr(UBound(Arr) + 1)
                        End If
                    End If          'Close the handle to the process
                    lRet = CloseHandle(hProcess)
                Next
        End Select
        ReDim Preserve Arr(UBound(Arr) - 1)
        Processes = Arr
    End Function
    
    'usage:
    'place a commandbutton and a listbox on the form:
    Private Sub Command1_Click()
    Dim n
        For Each n In Processes
            List1.AddItem n
        Next n
    End Sub
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299

    Excellent

    That was just what i was after... Thanks heaps for that kedaman. And you other guys..
    Its all good now.
    BW
    PS just a thought, is it possible to get the company name and comments etc of a dll or exe file (like when you go to the properties of exe files, and it has all the details).
    If you could just point me in the right direction to add that to it? I'm just trying to make my little program look more professional so its not urgent, if you could just suggest some apis or something to do that... cheers.

    [Edited by But_Why on 11-16-2000 at 02:22 PM]

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299

    Just an Update

    I have got the file version working sort of and product version working, again sort of. And File Type.
    I am unsure of the File OS, Companyname, productname, comments, Internal name, Language and original filename. I know not all of these are entered in all files, but does anyone have any idea how to get these? And maybe a better way to get file version and product version? I tried using GetFileVersionInfo, but i don't think i am using it right.
    Any Ideas?
    BW

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