Hi,
I used this to bring another application to the Front:
Hope this helps,
Pieter
Public Sub BringExtraToFront()
Dim prc As Process
Dim clsProc As New clsProcesses
prc = clsProc.ProcessExtra 'gets me the
process based on the name
Dim handle As IntPtr = prc.MainWindowHandle
Dim Win32Help As New Win32Helper
If Not IntPtr.Zero.Equals(handle) Then
Win32Helper.ShowWindow(handle, 1)
Win32Helper.SetForegroundWindow(handle)
End If
End Sub
Option Explicit On
Public Class clsProcesses
Public Shared Function PrevInstance() As Process
Dim c As Process = Process.GetCurrentProcess()
' Durchlaufen aller Prozesse mit gleichem Namen.
Dim p As Process
For Each p In Process.GetProcessesByName(c.ProcessName)
' Aktuellen Prozess nicht beachten.
If p.Id <> c.Id Then
' Es kann mehrere Prozesse gleichen Namens geben, die von
' unterschiedlichen Programmen stammen.
If p.MainModule.FileName = c.MainModule.FileName Then
' Prozess der ersten gefundenen anderen Instanz
' zurückgeben.
Return p
End If
End If
Next p
' Keine andere Instanz gefunden.
Return Nothing
End Function
Public Shared Function ExtraOpen() As Boolean
' Durchlaufen aller Prozesse mit gleichem Namen.
Dim p As Process
For Each p In Process.GetProcessesByName("EXTRA")
If Left(p.MainWindowTitle, 4) = SessionName Then
Return True
Exit Function
End If
Next p
Return False
End Function
Public Shared Function ProcessExtra() As Process
' Durchlaufen aller Prozesse mit gleichem Namen.
Dim p As Process
For Each p In Process.GetProcessesByName("EXTRA")
If Left(p.MainWindowTitle, 4) = SessionName Then
Return p
Exit Function
End If
Next p
Return Nothing
End Function
Public Sub New()
'error-handler die alle errors voor zn rekening neemt
AddHandler System.Windows.Forms.Application.ThreadException,
AddressOf GlobalErrorHandler
End Sub
End Class
Public NotInheritable Class Win32Helper
<System.Runtime.InteropServices.DllImport("user32.dll", _
EntryPoint:="SetForegroundWindow", _
CallingConvention:=Runtime.InteropServices.CallingConvention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Public Shared Function _
SetForegroundWindow(ByVal handle As IntPtr) As Boolean
' Leave function empty
End Function
<System.Runtime.InteropServices.DllImport("user32.dll", _
EntryPoint:="ShowWindow", _
CallingConvention:=Runtime.InteropServices.CallingConvention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Public Shared Function ShowWindow(ByVal handle As IntPtr, _
ByVal nCmd As Int32) As Boolean
' Leave function empty
End Function
End Class ' End Win32Helper