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.

gbrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AnswerTheManCommented:
1.start a new project.
(Form1 is added by default)

2.start the VisualBasic Help (MSDN)

3. add this code to form1 :

Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Private Sub Form_Load()
    Dim i As Long
    Dim s As String
    i = FindWindow(s, "MSDN Library Visual Studio 6.0")
    ShowWindow i, 1
    SetForegroundWindow i
End Sub

4. run the prog

5. when starts - it shows the MSDN window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JuiletteCommented:
'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
0
gbrAuthor Commented:
Answertheman's answer did not work, but  Juilette's answer worked great, ...thanks guys
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.