Solved

Win32 API to access NT Task Manager in VB

Posted on 1998-05-02
6
339 Views
Last Modified: 2008-10-12
Hello.  I am currently programming an application that needs to use NT's Task Manager.  Is there an Win32
API that I can use?
0
Comment
Question by:MokMok
  • 5
6 Comments
 
LVL 2

Expert Comment

by:vspeter
ID: 1437105
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
 
LVL 4

Accepted Solution

by:
zsi earned 50 total points
ID: 1437106
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
 
LVL 4

Expert Comment

by:zsi
ID: 1437107
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 4

Expert Comment

by:zsi
ID: 1437108
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
 
LVL 4

Expert Comment

by:zsi
ID: 1437109
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
 
LVL 4

Expert Comment

by:zsi
ID: 1437110
Incidentally, if you do not have PSAPI.DLL, you can download it from Microsoft at http://www.microsoft.com
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

707 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

12 Experts available now in Live!

Get 1:1 Help Now