It seems like you want to get a list of all the running applications on your machine so you can look for one by main window title, right?
Here's a class I found somewhere online, and modified a bit:
Imports System.Runtime.InteropServices
Public Class clsEnumWindows
<unmanagedfunctionpointerattribute(callingconvention.stdcall)> _
Public Delegate Function EnumWindowsProc(ByVal hwnd As System.IntPtr, ByVal lparam As System.IntPtr) As Boolean
<dllimportattribute("user32.dll",> _
Public Shared Function EnumWindows(ByVal lpEnumFunc As EnumWindowsProc, <marshalasattribute(unmanagedtype.sysint)> ByVal lParam As IntPtr) As <marshalasattribute(unmanagedtype.bool)> Boolean
End Function
<dllimportattribute("user32.dll",> _
Public Shared Function IsWindowVisible(<inattribute()> ByVal hWnd As System.IntPtr) As <marshalasattribute(unmanagedtype.bool)> Boolean
End Function
<dllimportattribute("user32.dll",> _
Public Shared Function GetWindowText(<inattribute()> ByVal hWnd As System.IntPtr, <outattribute(),> ByVal lpString As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer
End Function
<dllimportattribute("user32.dll",> _
Public Shared Function GetWindowTextLength(<inattribute()> ByVal hWnd As System.IntPtr) As Integer
End Function
<dllimportattribute("user32.dll",> _
Public Shared Function GetWindowThreadProcessId(<inattribute()> ByVal hWnd As System.IntPtr, <outattribute()> ByRef lpdwProcessId As Integer) As UInteger
End Function
<dllimportattribute("user32.dll",> _
Public Shared Function GetClassName(<inattribute()> ByVal hWnd As System.IntPtr, <outattribute(),> ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer
End Function
Public Shared Function GetWindows() As List(Of ManagedWindow)
Dim WindowList As New List(Of ManagedWindow)
Dim ListHandle As GCHandle = GCHandle.Alloc(WindowList)
Try
clsEnumWindows.EnumWindows(New clsEnumWindows.EnumWindowsProc(AddressOf EnumWindowsCallBack), GCHandle.ToIntPtr(ListHandle))
Finally
ListHandle.Free()
End Try
Return WindowList
End Function
Private Shared Function EnumWindowsCallBack(ByVal handle As IntPtr, ByVal lParam As IntPtr) As Boolean
If clsEnumWindows.IsWindowVisible(handle) Then
Dim TitleBuilder As New System.Text.StringBuilder(clsEnumWindows.GetWindowTextLength(handle) + 1)
Dim WndList As List(Of ManagedWindow) = DirectCast(GCHandle.FromIntPtr(lParam).Target, List(Of ManagedWindow))
clsEnumWindows.GetWindowText(handle, TitleBuilder, TitleBuilder.Capacity)
Dim ProcessID As Integer = -1
clsEnumWindows.GetWindowThreadProcessId(handle, ProcessID)
Dim ClassNameBuilder As New System.Text.StringBuilder(255)
clsEnumWindows.GetClassName(handle, ClassNameBuilder, ClassNameBuilder.Capacity)
WndList.Add(New ManagedWindow(handle, TitleBuilder.ToString, Process.GetProcessById(ProcessID), ClassNameBuilder.ToString))
End If
Return True
End Function
Public Class ManagedWindow
Private _Title As String = String.Empty
Public Property Title() As String
Get
Return _Title
End Get
Set(ByVal value As String)
_Title = value
End Set
End Property
Private _Handle As IntPtr
Public Property Handle() As IntPtr
Get
Return _Handle
End Get
Set(ByVal value As IntPtr)
_Handle = value
End Set
End Property
Private _OwningProcess As Process
Public Property OwningProcess() As Process
Get
Return _OwningProcess
End Get
Set(ByVal value As Process)
_OwningProcess = value
End Set
End Property
Private _ClassName As String = String.Empty
Public Property ClassName() As String
Get
Return _ClassName
End Get
Set(ByVal value As String)
_ClassName = value
End Set
End Property
Public Sub New()
End Sub
Public Sub New(ByVal Hwnd As IntPtr, ByVal TitleText As String, ByVal Owner As Process, ByVal NativeClassName As String)
_Handle = Hwnd
_Title = TitleText
_OwningProcess = Owner
_ClassName = NativeClassName
End Sub
Public Overrides Function ToString() As String
If OwningProcess Is Nothing Then
Return Handle.ToString & " —- " & ClassName & " —- " & Title
Else
Return Handle.ToString & " —- " & ClassName & " —- " & Title & " —- " & OwningProcess.ProcessName
End If
End Function
End Class
End Class
To see how it works, create a new vb.net windows forms application. Add this class, drop a listbox on form1, and put this code in Form1_Load:
Dim windows As List(Of clsEnumWindows.ManagedWindow) = clsEnumWindows.GetWindows
For Each window As clsEnumWindows.ManagedWindow In windows
Me.ListBox1.Items.Add(window.Title)
Next
Then run it. I hope looking at that simple code example (the stuff in form_load), this class's usage is clear.
- Pete