Article ID: 242416 - Last Review: July 1, 2004 - Revision: 2.1

How To Enumerate 16-bit Tasks on Window NT Using Visual Basic

System TipThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
This article was previously published under Q242416

On This Page

Expand all | Collapse all

SUMMARY

There may be times when it is necessary to enumerate all of the 16-bit tasks that are running on a Windows NT system. To do this you use the VDMEnumProcessWOW and VDMEnumTaskWOWEx API functions. The MORE INFORMATION section to follow demonstrates how to use these functions within Visual Basic. Note, that it is also necessary to use call back functions to enumerate the 16-bit processes. If you are unfamiliar with the use of callback functions from within Visual Basic, see the article mentioned in the REFERENCES section of this article.

MORE INFORMATION

Steps to Create the Example

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. Add one CommandButton to the form.
  3. Set the forms AutoReDraw property to True.
  4. Select Project from the Menu and add a new module to the project. Module1 is created by default.
  5. Copy the following code into the Code window of Module1:
    Option Explicit
    
    Declare Function VDMEnumProcessWOW Lib "VDMDBG.dll" _ 
    (ByVal fp As Long, lparam As Long) As Integer 
    
    Declare Function VDMEnumTaskWOWEx Lib "VDMDBG.dll " _ 
    (dwProcessId As Long, ByVal fp As Long, lparam As Long) As Integer 
    
    Declare Function lstrcpy Lib "kernel32" _ 
    (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long 
    
    Public Function PROCESSENUMPROC _
    (ByVal dwProcessId As Long, ByVal dwAttributes As Long, _
    lpUserDefined As Long) As Boolean
      
        Form1.Cls
        Form1.Print "dwProcessId: " & dwProcessId
        Form1.Print "dwAttributes: " & dwAttributes & vbCrLf 
    ' Pass the Process ID to this next function to enumerate that Process.
        Call VDMEnumTaskWOWEx(ByVal dwProcessId, AddressOf TASKENUMPROCEX, 0)
    
    End Function
    
    Public Function TASKENUMPROCEX _ 
    (ByVal dwThreadId As Long, ByVal hMod16 As Long, _ 
    ByVal hTask16 As Long, ByVal pszModName As Long, _ 
    ByVal pszFileName As Long, lpUserDefined As Long) As Boolean 
    
    ' Print Enumerated task of the 16bit process
        Form1.Print "dwThreadId: " & dwThreadId & vbCrLf & _
        "hMod16: " & hMod16 & vbCrLf & "hTask16: " & hTask16 & vbCrLf & _
        "pszModName: " & PointerToString(pszModName) & vbCrLf & _
        "pszFileName: " & PointerToString(pszFileName) & vbCrLf & _
        "lpUserDefined: " & lpUserDefined & vbCrLf
    ' Return value is false until there are no more tasks to enumerate.  
    End Function
    
    Private Function PointerToString(lPtr As Long) As String
    Dim sTemp As String * 255, Retval As Long
    ' Parse String Values returned from function call. 
    
        Retval = lstrcpy(sTemp, lPtr)
        If (InStr(1, sTemp, Chr(0)) = 0) Then
             PointerToString = ""
        Else
             PointerToString = Left(sTemp, InStr(1, sTemp, Chr(0)) - 1)
        End If
        
    End Function
    					
  6. Copy the following code into the Code window of Form1:
    Private Sub Command1_Click() 
    
    Dim Retval As Long 
    ' Call VDMEnumProcessWOW to beging the enumeration
       Retval = VDMEnumProcessWOW(AddressOf PROCESSENUMPROC, 0)
    
    End Sub 
    					
  7. Save the Project and then press F5 to run the project. The enumerated processes will list on the form.

REFERENCES

170570  (http://support.microsoft.com/kb/170570/EN-US/ ) How To Build a Windows Message handler with Addressof in VB
181578  (http://support.microsoft.com/kb/181578/EN-US/ ) How To CallBack Visual Basic Functions From a C Dll

APPLIES TO
  • Microsoft Visual Basic 5.0 Learning Edition, when used with:
    • Microsoft Windows NT 4.0
  • Microsoft Visual Basic 6.0 Learning Edition, when used with:
    • Microsoft Windows NT 4.0
  • Microsoft Visual Basic 5.0 Professional Edition, when used with:
    • Microsoft Windows NT 4.0
  • Microsoft Visual Basic 6.0 Professional Edition, when used with:
    • Microsoft Windows NT 4.0
  • Microsoft Visual Basic 5.0 Enterprise Edition, when used with:
    • Microsoft Windows NT 4.0
  • Microsoft Visual Basic 6.0 Enterprise Edition, when used with:
    • Microsoft Windows NT 4.0
Keywords: 
kbhowto kbapi KB242416