Ecco la soluzione:
codice:
Imports System.Runtime.InteropServices
Public Class Form1
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As CallBack, ByVal lParam As Integer) As Integer
Declare Function GetForegroundWindow Lib "user32" () As Integer
Declare Function GetParent Lib "user32" (ByVal hwnd As Integer) As Integer
Declare Function GetWindow Lib "user32" (ByVal hwnd As Integer, ByVal wCmd As Integer) As Integer
Declare Function GetWindowInteger Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Integer, ByVal nIndex As Integer) As Integer
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
Declare Function IsIconic Lib "user32" (ByVal hwnd As Integer) As Integer
Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Integer) As Integer
' Constants used with APIs
Public Const SW_SHOW = 5
Public Const SW_RESTORE = 9
Public Const GW_OWNER = 4
Public Const GWL_HWNDPARENT = (-8)
Public Const GWL_EXSTYLE = (-20)
Public Const WS_EX_TOOLWINDOW = &H80
Public Const WS_EX_APPWINDOW = &H40000
Public Delegate Function CallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetWindowThreadProcessId(ByVal hwnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer
End Function
Public Shared Function fEnumWindowsCallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
Dim lReturn As Integer
Dim lExStyle As Integer
Dim bNoOwner As Boolean
Dim sWindowText As String
Dim lpProc As Long
If hwnd <> Form1.Handle Then
If IsWindowVisible(hwnd) Then
If GetParent(hwnd) = 0 Then
bNoOwner = (GetWindow(hwnd, GW_OWNER) = 0)
lExStyle = GetWindowInteger(hwnd, GWL_EXSTYLE)
If (((lExStyle And WS_EX_TOOLWINDOW) = 0) And bNoOwner) Or ((lExStyle And WS_EX_APPWINDOW) And Not bNoOwner) Then
sWindowText = Space$(256)
Call GetWindowThreadProcessId(hwnd, lpProc)
lReturn = GetWindowText(hwnd, sWindowText, Len(sWindowText))
If lReturn Then
sWindowText = Trim(sWindowText)
Form1.ListBox1.Items.Add(hwnd & " " & lpProc & " " & sWindowText)
End If
End If
End If
End If
End If
fEnumWindowsCallBack = True
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
EnumWindows(AddressOf fEnumWindowsCallBack, 0)
End Sub
End Class