Shell.execute

I want to have a button on my form to launch the windows calculator.
Well that’s simple.

dim sh as new shell sh.Execute("calc.exe")
But, I don’t want windows to launch a new instance when I press this button for the second time. Instead I want the running instance of the Windows Calculator coming on top.
Does anyone have the trick for me ?

You can verify if an instance of Calc is already running with a shell to TaskList.

Bringing a particular window to front should be possible by using FindWindow() and SetWindowPos() or setForeGoundWindow.

Find the window by its title (Calc)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx
Bring it to front
https://msdn.microsoft.com/en-us/library/ms633539(VS.85).aspx?f=255&MSPPError=-2147217396

First of all you need the process handle as Michel said.

After that you need to pass the handle to this Win32 API function: SetForegroundWindows

Not sure if WFS has that function, but even if it doesn’t, declaring it shouldn’t be too hard.

Edit: It is there actually. It needs WFS. The function is:

[code]
Sub BringToFrontWFS(extends w as Window)
#if TargetWin32
Dim i, h, r As Integer

if not w.visible then
  w.visible = true
end if

Declare Function SetWindowPos Lib "User32" (hWnd As Integer, hWndInsertAfter As Integer, _
X As Integer, Y As Integer, cx As Integer, cy As Integer, wFlags As Integer) As Integer
Declare Function ShowWindow Lib "User32" ( hWnd As Integer, nCmdShow As Integer ) As Integer
Declare Function BringWindowToTop Lib "User32" ( hWnd As Integer ) As Integer
Declare Function SetForegroundWindow Lib "User32" ( hWnd As Integer ) As Integer

' Get the Window Handle
h = w.WinHWND
r = &h1 + &h2 + &h40

' Activate the Window
i = SetWindowPos( h,-1, 0,0,0,0, r )
i = ShowWindow( h, 1 )
i = BringWindowToTop( h )
i = SetForegroundWindow( h )
w.FlashWindowExWFS( 3 )

#else

#pragma unused w

#endif
End Sub[/code]

I do not have a win PC or VM close to me but what about using start?

sh.Execute("start calc.exe")

[quote=225198:@Tomas Jakobs]I do not have a win PC or VM close to me but what about using start?

sh.Execute("start calc.exe")

That will launch a new instance of the program, even if it is already running.

Oh okay the running instance, then you must use a Handle or you quit the running Calc Process and start a new one…

OK. It is in WFS :

vbwfs.AppActivate("Calculator")