Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to prevent multiple instances of a 32-bit VB application

Windows API

How to prevent multiple instances of a 32-bit VB application

by  genomon  Posted    (Edited  )
This is MSKB article# 185730. Took awhile to find it, so I
am sharing it out here to save you the trouble. I put the declarations in a seperate module in my app & used the rest of the code in my startup form, instead of Sub Main() as the article suggests.

1. Create a new Visual Basic project.
2. Go into Project Properties and set the Startup Object (or Startup Form in VB4) to be Sub Main.

3. Add a Standard Module to the Project.
4. Paste the following code into the module:
Code:
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

Private Sub Form_Load()
If App.PrevInstance Then
ActivatePrevInstance
End If
End Sub

Sub ActivatePrevInstance()

    Dim strOldTitle As String
    Dim lngPrevHndl As Long
    Dim lngResult As Long

    'Save the title of the application.
    strOldTitle = 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.
    lngPrevHndl = FindWindow("ThunderRTMain", strOldTitle)

    'Check for no success.
    If lngPrevHndl = 0 Then
    'Attempt to get window handle using VB5 class name.
    lngPrevHndl = FindWindow("ThunderRT5Main", strOldTitle)
    End If

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

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

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

    'Restore the program.
    lngResult = OpenIcon(lngPrevHndl)

    'Activate the application.
    lngResult = SetForegroundWindow(lngPrevHndl)

    'End the application.
    Unload Me

End Sub

5. Compile the project into an EXE.
6. Exit Visual Basic.
7. Run the executable you created.
8. Repeat step 7.

RESULT: The first instance of the program is given focus and the second instance is closed. If the first instance of the application was minimized, it will be restored to a normal window automatically.

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top