Win32 API to access NT Task Manager in VB

Hello.  I am currently programming an application that needs to use NT's Task Manager.  Is there an Win32
API that I can use?
MokMokAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vspeterCommented:
Have you check out the API Text Viewer program that comes with VB5 ?.

It guess, you need to check out the SDK to find out which API function is required to do the job then refer to the program above for VB equivalent reference.

Cheers,
0
zsiCommented:
The API functions that you are looking for are TaskFirst and TaskNext.

Here is some sample code derived from Dan Appleman's "Visual Basic 5.0 programmers Guide to the Win32 API".  It is posted here for education purposes only and all copyrights are retained by the owner.

Hope this helps!

zsi
================================
Option Explicit

Type TASKENTRY ' 40 bytes
    dwSize As Long
    hTask As Integer
    hTaskParent As Integer
    hInst As Integer
    hModule As Integer
    wSS As Integer
    wSP As Integer
    wStackTop As Integer
    wStackMinimum As Integer
    wStackBottom As Integer
    wcEvents As Integer
    hQueue As Integer
    szModule As String * 10
    wPSPOffset As Integer
    hNext As Integer
End Type

Declare Function TaskFirst% Lib "toolhelp.dll" (lpte As TASKENTRY)
Declare Function TaskNext% Lib "toolhelp.dll" (lpte As TASKENTRY)

Sub GetTaskList
Dim success%
    Dim te As TASKENTRY
   
    lst_Tasks.Clear       ' Clear the list box

    ' It is very important to initialize the value of the size field in the structure.
    te.dwSize = Len(te)
    success% = TaskFirst(te)
    Do While success%
        lst_Tasks.AddItem GetModuleNameTE$(te) & " " & Hex$(te.hTask)
        success% = TaskNext(te)
    Loop
End Sub

Function GetModuleNameTE$ (lpte As TASKENTRY)
    Dim pos%
    pos% = InStr(lpte.szModule, Chr$(0))
    If pos% > 0 Then
        GetModuleNameTE$ = Left$(lpte.szModule, pos% - 1)
    Else
        GetModuleNameTE$ = ""
    End If
End Function




0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zsiCommented:
I forgot to add the following items:

- The Type and Function Declarations need to be in a BAS module.
- You need to place a listbox control on your form and name it lst_Tasks.
- The function GetModuleNameTE also goes in the public module

Also, the code that I posted is 16bit.  I'll post the 32bit equivilent in a little bit (have to convert it first).

Sorry for any confusion

zsi

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

zsiCommented:
You didn't say what version of Windows this is for.  The above code works for Win 3.x and should work for Win95.

WinNT does not use the toolhelp.dll and therefore requires different code.  Please let me know if you need this.

zsi
0
zsiCommented:
Ok, as promised, here is a version for NT.  Again, you will need a ListBox named lst_Tasks.

This code is based on that provided in Bruce McKinney's "Hardcore Visual Basic"
========================================
In the (declarations) section:

 Declare Function EnumProcesses Lib "PSAPI" ( _
    lpidProcess As Long, ByVal cb As Long, cbNeeded As Long) As Long

 Declare Function EnumProcessModules Lib "PSAPI" ( _
    ByVal hProcess As Long, lphModule As Long, _
    ByVal cb As Long, lpcbNeeded As Long) As Long

 Declare Function GetModuleBaseName Lib "PSAPI" Alias "GetModuleBaseNameA" ( _
    ByVal hProcess As Long, ByVal hModule As Long, _
    ByVal lpBaseName As String, ByVal nSize As Long) As Long

 Declare Function GetModuleBaseNameW Lib "PSAPI" ( _
    ByVal hProcess As Long, ByVal hModule As Long, _
    lpBaseName As Byte, ByVal nSize As Long) As Long

 Declare Function GetModuleFileNameEx Lib "PSAPI" Alias "GetModuleFileNameExA" ( _
    ByVal hProcess As Long, ByVal hModule As Long, _
    ByVal lpFileName As String, ByVal nSize As Long) As Long

 Declare Function GetModuleFileNameExW Lib "PSAPI" ( _
    ByVal hProcess As Long, ByVal hModule As Long, _
    lpFileName As Byte, ByVal nSize As Long) As Long

 Type MODULEINFO
    lpBaseOfDll As Long
    SizeOfImage As Long
    EntryPoint As Long
End Type

Declare Function GetModuleInformation Lib "PSAPI" ( _
   ByVal hProcess As Long, ByVal hModule As Long, _
   lpmodinfo As MODULEINFO, ByVal cb As Long) As Long

Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
 

Const PROCESS_QUERY_INFORMATION = &H400
Const PROCESS_VM_READ = &H10




Sub ShowProcessList()
   
   Dim c As Long, f As Long, sName As String
   Dim c2 As Long, sPathName As String
   
   ' Windows NT uses PSAPI functions
   Dim i As Long, iCur As Long, cRequest As Long, cGot As Long
   Dim aProcesses() As Long, hProcess As Long, hModule As Long
   
   cRequest = 96       ' Request in bytes for 24 processes
   Do
       ReDim aProcesses(0 To (cRequest / 4) - 1) As Long
       f = EnumProcesses(aProcesses(0), cRequest, cGot)
       If f = 0 Then Exit Sub
       If cGot < cRequest Then Exit Do
       cRequest = cRequest * 2
   Loop
   cGot = cGot / 4     ' From bytes to processes
   ReDim Preserve aProcesses(0 To cGot - 1) As Long
   
   For i = 0 To cGot - 1
       hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or _
                              PROCESS_VM_READ, 0, _
                              aProcesses(i))
       ' Ignore processes that fail (probably no
       ' security rights)
       If hProcess = 0 Then GoTo NextFor
       ' Get first module only
       f = EnumProcessModules(hProcess, hModule, 4, c)
       If f = 0 Then GoTo NextFor
       sName = String$(255, 0)
       sPathName = String$(255, 0)
       
       c = GetModuleBaseName(hProcess, hModule, sName, 255)
       c2 = GetModuleFileNameEx(hProcess, hModule, sPathName, 255)
       
      Form1.lstTasks.AddItem Left$(sName, c) & Chr(9) & Left$(sPathName, c2)
 
NextFor:
   Next

End Sub

0
zsiCommented:
Incidentally, if you do not have PSAPI.DLL, you can download it from Microsoft at http://www.microsoft.com
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.