by Joachim Berz (1 Submission)
Category: Windows API Call/Explanation
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (2 Votes)
starts an exe from within your application. But if the exe is already loaded, it becomes the focus! Normaly it starts with the poor shell-Command again and again... // IN GERMAN: Startet eine EXE aus Deiner VB-Applikation. Wenn Die EXE jedoch schon einmal gestartet wurde, wird sie lediglich fokusiert(!). Normalerweise würde sie wieder und wieder gestartet werden, wenn sie zuvor vom User nicht geschlossen wurde!
API DeclarationsOption Explicit
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Sub Command1_Click()
Dim Handle As Long
' the FindWindow-API needs the Caption-Name of the exe-File (e.g. Calculator for the Calc.exe!)
' Handle = FindWindow(vbNullString, "")
Handle = FindWindow(vbNullString, "Calculator") ' Is the exe already loaded?
' *! im deutschen Windows muss bei diesem Beispiel statt "Calculator" das Wort "Rechner" stehen!!!
If Handle = 0 Then ' _if the Handle becomes 0 then START the EXE-File
Handle = Shell("CALC.EXE", 1)
Else ' _if fires a Handle, the exe is there! Let´s focus it...
ShowWindow Handle, 0 ' Hide the EXE (huh! Where is the exe???)
ShowWindow Handle, 1 ' Show the EXE (now it becomes the Focus!!!)
End If
End Sub