Search Tools Links Login

Prevent more than one instance from any directory


After seeing a few examples submitted attempting to prevent more than one instance. I decided to submit my version.
This code will only prevent the 2nd instance from running no matter what directory both .exe are executed from.
In addition, it does not use App.PrevInstance and will not bring the 1st instance to the front, just merely a *WORKING* example of how to stop more then one instance from running and the code is very easy to understand and use in your own apps.

Original Author: Sabro

API Declarations

Private Const ERROR_ALREADY_EXISTS As Long = 183&
Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As Any, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
Private Declare Function ReleaseMutex Lib "kernel32" (ByVal hMutex As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Code

Private Function IsPrevInstance() As Boolean
  Dim lngMutex As Long
  lngMutex = CreateMutex(ByVal 0&, 1, App.Title)
  If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
    ReleaseMutex lngMutex
    CloseHandle lngMutex
    IsPrevInstance = True
  Else
    IsPrevInstance = False
  End If
End Function
Private Sub Form_Load()
  If IsPrevInstance = True Then
    MsgBox "This Program Is Already Active!"
    End
  End If
End Sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 101 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.