[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Win32 API to access NT Task Manager in VB

Posted on 1998-05-02
6
Medium Priority
?
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 150 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…
Suggested Courses

649 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