Solved

Win32 API to access NT Task Manager in VB

Posted on 1998-05-02
6
343 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS Access - Capture pressed key onclick 4 33
VB6 Compile Compatibility Issue 4 102
Copy a row 12 62
vb6 connector to mongodb 2 84
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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 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