Link to home
Start Free TrialLog in
Avatar of vaultworld
vaultworld

asked on

List all applications using EnumWindows in .NET

After doing research I've found that process.GetProcessesByName("") will only display active processes.

I need to list all processes.

How do I list all processes using EnumWindows in .NET
Avatar of Glom
Glom
Flag of France image

Hi,
Have you tried Process.GetProcesses() ?
Yes, Process.GetProcesses gives you an array of System.Diagnostic.Process with all the current processes. You can get the name with the attribute ProcessName.

SoMoS
Avatar of vaultworld
vaultworld

ASKER

You can get all the process names, but it only list the active handles.  I need all the handles.

If you try your suggestion on the below example and you'll see it doesn't work.

 I would like to list out all of the id's and titles of child windows.

Here's what I do:

Close all the IEXPLORE browsers.

Start a new IEXPLORE.

Then go to File -> New Window
-- I believe this spawns a child window

-- Continue for additional windows # Then go to File -> New Window

It only displays information for the actively window.

I would like to display the information for all windows not just the active or focuesed one.
SOLUTION
Avatar of wguerram
wguerram

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I do need this is Visual Basic .NET

Thanks for you time inadvance
Getting Close -- Now I'm having problem printing back into to the list box.

-- I get the error

Reference to a non-share member requires an object reference

-- at the line in Module1.vb

        Form1.ListBox1.Items.Add(sSave)

Module1.vb:

Module Module1

    Public Delegate Function EnumWindowsProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32

    Public Declare Function EnumWindows Lib "user32.dll" _
            (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Int32) As Int32

    Public Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" _
        (ByVal hwnd As IntPtr) As Int32

    Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" _
        (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Int32) As Int32


    '  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '      EnumWindows(AddressOf EnumWindowsCallBack, 0)
    ' End Sub

    'Callback function to enum windows
    Public Function EnumWindowsCallBack(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32
        Dim sSave As String

        'Get the windowtext length
        sSave = Space(GetWindowTextLength(hwnd) + 1)

        'get the window text
        GetWindowText(hwnd, sSave, Len(sSave))

        'remove the last Chr(0)
        sSave = Microsoft.VisualBasic.Left(sSave, Len(sSave) - 1)

        'MessageBox.Show(sSave)

        Form1.ListBox1.Items.Add(sSave)

        If sSave.Trim <> "" Then
            Debug.WriteLine(sSave)
        End If

        Return 1 'continue enumeration        
    End Function

End Module


FORM1:

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.ListBox1 = New System.Windows.Forms.ListBox()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(40, 184)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(200, 56)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(32, 24)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(216, 134)
        Me.ListBox1.TabIndex = 1
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListBox1, Me.Button1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        EnumWindows(AddressOf EnumWindowsCallBack, 0)
    End Sub
End Class
Change the ListBox1 modifiers property on form1 to public in the designer

ListBox1.modifiers = Public
ListBox in .NET doesn't have a .modifiers property.

After changing it from the design it actually changed the below code:

    Public WithEvents ListBox1 As System.Windows.Forms.ListBox
    Public WithEvents Button1 As System.Windows.Forms.Button

I still get the same error after switch them both to Public.

Is there something special you need to do to access a form from a different module?
Yes it has.

When you are designing your form, clik the listbox, go to the properties, there you will find a modifiers property

change it to public:

every control in a form has a modifiers property.

You are having a scope problem.

don't pay attention to this:

ListBox1.modifiers = Public
I did change the modifier and got the below change.

      
   Public WithEvents ListBox1 As System.Windows.Forms.ListBox
    Public WithEvents Button1 As System.Windows.Forms.Button

But still Same error.

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks but no dice, same problem both ways.

It appears the C.NET has a way to reference the Form via the passed in handle,  does VB.NET have the same

I found a C.NET example at:
http://eggheadcafe.com/forums/ForumPost.asp?ID=7714&INTID=14

 private static bool EnumWindowCallBack(int hwnd, int lParam)
{
windowHandle = (IntPtr)hwnd;
listBoxHandle = (IntPtr)lParam;
ListBox lb =(ListBox)ListBox.FromHandle(listBoxHandle);
sb = new StringBuilder(1024);
sbc = new StringBuilder(256);
GetClassName(hwnd,sbc,sbc.Capacity);
GetWindowText((int)windowHandle, sb, sb.Capacity);

String xMsg  = sb+" "+sbc+" "+windowHandle;
if( sbc.Length > 0 )
{

myAl.Add(windowHandle);
i++;
lb.Items.Add(xMsg);

}
return true;
}
pass the handle in the lParam
dim _ListBox as ListBox = Control.FromHandle(listBoxHandle )
Here's the Final Solution:

ublic Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ListBox1 = New System.Windows.Forms.ListBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(32, 16)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(240, 147)
        Me.ListBox1.TabIndex = 0
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(32, 200)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(240, 64)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.ListBox1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Lst = ListBox1
        EnumWindows(AddressOf EnumWindowsCallBack, 0)
    End Sub

End Class







Module Module1

    Public Lst As ListBox

    Public Delegate Function EnumWindowsProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32

    Public Declare Function EnumWindows Lib "user32.dll" _
            (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Int32) As Int32

    Public Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" _
        (ByVal hwnd As IntPtr) As Int32

    Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" _
        (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Int32) As Int32

    'Callback function to enum windows
    Public Function EnumWindowsCallBack(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32
        Dim sSave As String

        'Get the windowtext length
        sSave = Space(GetWindowTextLength(hwnd) + 1)

        'get the window text
        GetWindowText(hwnd, sSave, Len(sSave))

        'remove the last Chr(0)
        sSave = Microsoft.VisualBasic.Left(sSave, Len(sSave) - 1)

        'Error below: Reference to a non-shared member requires an Object Reference
        Lst.Items.Add(sSave)


        If sSave.Trim <> "" Then
            Debug.WriteLine(sSave)
        End If

        Return 1 'continue enumeration        
    End Function

End Module