Link to home
Start Free TrialLog in
Avatar of gbr
gbr

asked on

Activate an application already running

When starting a vb app, I am using the following code to detect if an instance of the application is already running.

If App.PrevInstance = True Then
    AppActivate App.Title    
Else
    ...start application
End If

This works, but not really in the way i want it to work.  If a previous instance of the application is running, i want it to be made the active window, and if it is minimized, i want to be able to restore the application window, so that it is the active window.  The above code detects if the application is already running, but doesn't actually activate it, infact i dont think the appactivate command does anything at all.  Also, i have coded the application such that when minimized, it appears in the system tray, and not on the taskbar, so i would want it to be restored from the system tray if minimized.
I have spent hours on this trying various things, and i'm sure its something simple, can anyone help?  Thanks.

ASKER CERTIFIED SOLUTION
Avatar of AnswerTheMan
AnswerTheMan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Juilette
Juilette

'from Microsoft Databse
'check for previous instance of app
'
Option Explicit

Private Sub Form_Load()

   If App.PrevInstance Then
      ActivatePrevInstance
   End If

End Sub

'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

'2) Add a Standard Module to the Project.
'3) Paste the following code into the module:

Option Explicit

Public Const GW_HWNDPREV = 3
Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Sub ActivatePrevInstance()

   Dim OldTitle As String
   Dim PrevHndl As Long
   Dim result As Long

   'Save the title of the application.
   OldTitle = App.Title

   'Rename the title of this application so FindWindow
   'will not find this application instance.
   App.Title = "unwanted instance"

   'Attempt to get window handle using VB4 class name.
   PrevHndl = FindWindow("ThunderRTMain", OldTitle)

   'Check for no success.
   If PrevHndl = 0 Then

      'Attempt to get window handle using VB5 class name.
      PrevHndl = FindWindow("ThunderRT5Main", OldTitle)
   End If

   'Check if found
   If PrevHndl = 0 Then
        'Attempt to get window handle using VB6 class name
        PrevHndl = FindWindow("ThunderRT6Main", OldTitle)
   End If

   'Check if found
   If PrevHndl = 0 Then
      'No previous instance found.
      Exit Sub
   End If

   'Get handle to previous window.
   PrevHndl = GetWindow(PrevHndl, GW_HWNDPREV)

   'Restore the program.
   result = OpenIcon(PrevHndl)

   'Activate the application.
   result = SetForegroundWindow(PrevHndl)

   'End the application.
   End

End Sub
Avatar of gbr

ASKER

Answertheman's answer did not work, but  Juilette's answer worked great, ...thanks guys