Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Visual Basic (Microsoft) Versions 5/6 FAQ

Windows API

How to prevent multiple instances of a 32-bit VB application by genomon
Posted: 8 May 07

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:


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
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.  

Back to Visual Basic (Microsoft) Versions 5/6 FAQ Index
Back to Visual Basic (Microsoft) Versions 5/6 Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close