emily_hall
asked on
Enumerate Windows
I need to go through the currently open windows and get the thread id of a certain application so I can post messages to it... Is there some way to do this in VB.NET I am missing? Every example I've found is in VB and using a bunch of stuff that won't work in .NET.
Have you used the 'GetCurrentThreadId()' function?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Delegate Function EnumWindows_Callback(ByVal hWnd As Integer, ByVal lParam As Integer) As Integer
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As EnumWindows_Callback, ByVal lParam As Integer) As Integer
Public Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Integer, ByVal lpEnumFunc As EnumWindows_Callback, ByVal lParam As Integer) As Integer
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Integer, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Integer, ByVal lpString As System.Text.StringBuilder, ByVal cch As Integer) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
EnumWindows(AddressOf EnumWindows_CBK, 0)
Label1.Text = ListBox1.Items.Count & " windows"
End Sub
' The callback routine, common to both EnumWindows and EnumChildWindows.
' the argument passed in lParam is the indent level.
Function EnumWindows_CBK(ByVal hWnd As Integer, ByVal lParam As Integer) As Integer
' display information on this window, with correct indentation
' Console.WriteLine(New String(" "c, lParam * 3) & WindowDescription(hWnd))
'Dim s As String
's = New String(" "c, lParam * 3)
'ListBox1.Items.Add(s & WindowDescription(hWnd))
'ListBox1.Refresh()
Me.WindowDescription(hWnd)
' then display all child windows, but indent them to the right
EnumChildWindows(hWnd, AddressOf EnumWindows_CBK, lParam + 1)
' Return 1 to continue enumeration.
Return 1
End Function
' return a windows description given its hWnd
Function WindowDescription(ByVal hWnd As Integer) As String
Dim text As String
text = WindowText(hWnd)
' WindowDescription = "[" & Right$("0000000" & Hex$(hWnd), 8) & "] " & WindowClassName(hWnd)
WindowDescription = "[Hnd-" & Hex$(hWnd) & "] Cls-" & WindowClassName(hWnd)
'all windows
If Len(text) > 0 Then
'if I need to find certain window
'If Len(text) > 0 AndAlso text = "VBCodeLibrary Tool" Then
WindowDescription &= " - Wnd Name""" & text & """"
ListBox1.Items.Add(WindowD escription )
ListBox1.Refresh()
End If
End Function
' Return the caption/text of a window.
Function WindowText(ByVal hWnd As Integer) As String
Dim buffer As New System.Text.StringBuilder( 256)
Dim length As Integer
length = GetWindowText(hWnd, buffer, buffer.Capacity)
WindowText = buffer.ToString.Substring( 0, length)
End Function
Function WindowClassName(ByVal hWnd As Integer) As String
Dim buffer As New System.Text.StringBuilder( 256)
Dim length As Integer
length = GetClassName(hWnd, buffer, buffer.Capacity)
WindowClassName = buffer.ToString.Substring( 0, length)
End Function
End Class
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Delegate Function EnumWindows_Callback(ByVal
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As EnumWindows_Callback, ByVal lParam As Integer) As Integer
Public Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Integer, ByVal lpEnumFunc As EnumWindows_Callback, ByVal lParam As Integer) As Integer
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Integer, ByVal lpClassName As System.Text.StringBuilder,
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Integer, ByVal lpString As System.Text.StringBuilder,
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
EnumWindows(AddressOf EnumWindows_CBK, 0)
Label1.Text = ListBox1.Items.Count & " windows"
End Sub
' The callback routine, common to both EnumWindows and EnumChildWindows.
' the argument passed in lParam is the indent level.
Function EnumWindows_CBK(ByVal hWnd As Integer, ByVal lParam As Integer) As Integer
' display information on this window, with correct indentation
' Console.WriteLine(New String(" "c, lParam * 3) & WindowDescription(hWnd))
'Dim s As String
's = New String(" "c, lParam * 3)
'ListBox1.Items.Add(s & WindowDescription(hWnd))
'ListBox1.Refresh()
Me.WindowDescription(hWnd)
' then display all child windows, but indent them to the right
EnumChildWindows(hWnd, AddressOf EnumWindows_CBK, lParam + 1)
' Return 1 to continue enumeration.
Return 1
End Function
' return a windows description given its hWnd
Function WindowDescription(ByVal hWnd As Integer) As String
Dim text As String
text = WindowText(hWnd)
' WindowDescription = "[" & Right$("0000000" & Hex$(hWnd), 8) & "] " & WindowClassName(hWnd)
WindowDescription = "[Hnd-" & Hex$(hWnd) & "] Cls-" & WindowClassName(hWnd)
'all windows
If Len(text) > 0 Then
'if I need to find certain window
'If Len(text) > 0 AndAlso text = "VBCodeLibrary Tool" Then
WindowDescription &= " - Wnd Name""" & text & """"
ListBox1.Items.Add(WindowD
ListBox1.Refresh()
End If
End Function
' Return the caption/text of a window.
Function WindowText(ByVal hWnd As Integer) As String
Dim buffer As New System.Text.StringBuilder(
Dim length As Integer
length = GetWindowText(hWnd, buffer, buffer.Capacity)
WindowText = buffer.ToString.Substring(
End Function
Function WindowClassName(ByVal hWnd As Integer) As String
Dim buffer As New System.Text.StringBuilder(
Dim length As Integer
length = GetClassName(hWnd, buffer, buffer.Capacity)
WindowClassName = buffer.ToString.Substring(
End Function
End Class
ASKER
That PAQ showed a super quick and simple way to get what I needed. Thanks!