CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2001
    Location
    Singapore.
    Posts
    7

    Application Instance Already Open ?

    Hi Gurus,

    How can I prevent my application from being opened more than once ?

    Thanks,
    Krogoth!

    FSM

  2. #2
    Join Date
    Dec 2001
    Posts
    6

    Re: Application Instance Already Open ?

    There are two techniques to achive this:

    1) Using windows API

    Const ERROR_ALREADY_EXISTS = 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
    private Sub Form_Load()
    Dim hMutex as Long
    hMutex = CreateMutex(byval 0&, 1, App.Title) 'Try to create a Mutex

    'Check the mutex already exist or not
    If (Err.LastDllError = ERROR_ALREADY_EXISTS) then
    'Clean up all
    ReleaseMutex hMutex
    CloseHandle hMutex

    'More than one instance detected
    MsgBox "Give your Message"
    End
    else
    'form load code
    End If
    End Sub





    2) Using VB App global Object

    private Sub Form_Load()
    If App.PrevInstance = true then MsgBox "Give your Message": End
    End Sub





    programmer at MIND

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Application Instance Already Open ?

    You should not use "End" keyword...

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: Application Instance Already Open ?

    2) Using VB App global Object ( Corrected )


    private Sub Form_Load()
    If App.PrevInstance = true then
    MsgBox "Your Message"
    Unload me
    End Sub




    Nicolas Bohemier
    ______________

    Un sourire ne coûte rien, mais il rapporte beaucoup; il enrichit celui qui le reçoit sans appauvrir celui qui le donne.

    Frank Irving Fletcher

    Nicolas Bohemier

  5. #5
    Join Date
    Nov 2001
    Location
    Singapore.
    Posts
    7

    Re: Application Instance Already Open ?

    Thanks Man...

    I will use the second one but will try the first one too ;-)

    Krogoth!

    FSM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured