Hi Gurus,
How can I prevent my application from being opened more than once ?
Thanks,
Krogoth!
FSM
Printable View
Hi Gurus,
How can I prevent my application from being opened more than once ?
Thanks,
Krogoth!
FSM
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
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
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
Thanks Man...
I will use the second one but will try the first one too ;-)
Krogoth!
FSM