This is what I have so far:
Dim bProcess = Process.GetProcessesByName("By").FirstOrDefault
If bProcess IsNot Nothing Then
SwitchToThisWindow(bProcess.MainWindowHandle, True)
Else
Process.Start("C:\Program Files\B\B.exe")
End If
It has two problems.
- Some people have told me that SwitchToThisWindow is deprecated.
- If application B is minimized, this function silently fails from the user’s perspective.
So what’s the right way to do this?
Get the window handle (hwnd), and then use this user32.dll function:
VB.net declaration:
C# declaration:
One consideration is that this will not work if the window is minimized, so I’ve written the following method which also handles this case. Here is the C# code, it should be fairly straight forward to migrate this to VB.
Using that code, it would be as simple as setting the appropriate process variables and calling
BringMainWindowToFront("processName");