I have a piece of code that checks to see if a previous instance of the application is running, and if so sets focus to the already opened instance.
I am using the SetForegroundWindow API routine, and it works fine if the already openned instance has been minimized to the taskbar.
However, if the open instance is open behind another window then I get the error message "Invalid Porcedure Call or Argumnet", and it closes down. The code I am using is below.
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 VB6 class name
PrevHndl = FindWindow("ThunderRT6Main", OldTitle)
'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
ED
I am using the SetForegroundWindow API routine, and it works fine if the already openned instance has been minimized to the taskbar.
However, if the open instance is open behind another window then I get the error message "Invalid Porcedure Call or Argumnet", and it closes down. The code I am using is below.
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 VB6 class name
PrevHndl = FindWindow("ThunderRT6Main", OldTitle)
'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
ED