Important alert: (current site time 7/16/2013 1:02:43 AM EDT)
 

VB icon

Absolutely no alias or multiple instances

Email
Submitted on: 9/22/1998
By: Aliasace 
Level: Not Given
User Rating: By 1 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 26507
 
     Preventing multiple instances of your app from running may not enough. This small code snip will prevent multiple instances as well as duplicates from opening.
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Absolutely no alias or multiple instances
'**************************************
-------------------------------------
 Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
 -------------------------------------
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Absolutely no alias or multiple instances
' Description:Preventing multiple instances of your app from running may not enough. This small code snip will prevent multiple instances as well as duplicates from opening.
' By: Aliasace
'
' Assumes:Coder knows the forms "Class Name", and forms caption property is initially set to nullstring.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1063&lngWId=1'for details.'**************************************

Sub Form_Load()
'--------------------------------------------------------------------------
'Form1 Caption property should be set to NullStr
'--------------------------------------------------------------------------
If App.PrevInstance Or FindWindow("ThunderRT5Form", App.Title) > 0 Then
 AppTitle$ = App.Title
 
App.Title = "#$#"
Form1.Caption = "#$#"
AppActivate AppTitle$
End
 
End If
Form1.Caption = App.Title
End Sub


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

5/11/1999 5:11:00 PMZach

How do you find a forms "class name"
(If this comment was disrespectful, please report it.)

 
7/5/1999 12:46:00 PMI3KI3

well for on,in the findwindow statement he put the class name of the forms

(If this comment was disrespectful, please report it.)

 
7/13/1999 10:19:00 PMKali

why is it called ThunderRT5Form?
(If this comment was disrespectful, please report it.)

 
8/4/1999 10:57:00 PMJob

Answer to Kali,
it called ThunderRT5Form because VB5 default handle use this name say as an internal, if you use VB6 then what? ThunderRT6Form! you right!
(If this comment was disrespectful, please report it.)

 
8/23/1999 3:22:00 PMRomeo

This Code is great. I have been looking for something like this, great job!
(If this comment was disrespectful, please report it.)

 
3/6/2002 8:23:58 AMMarcel A. Fritsch

If the previous instance is minimized this code will cause a crash!
Look at this link for a perfect solution:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=21131&l ngWId=1

(If this comment was disrespectful, please report it.)

 
7/29/2002 9:20:15 AMmerve

Is there a similar way of using "App.PrevInstance", because my visual basic editor does not recognize it?
(If this comment was disrespectful, please report it.)

 
2/16/2003 2:48:06 PM

merve, VB apps won't recognise App.PrevInstance when you run them from the IDE... so you need to compile first.
(If this comment was disrespectful, please report it.)

 
7/8/2004 12:41:20 PM

Great code! This did EXACTLY what I needed, and it was simple and small. Thanks!
(If this comment was disrespectful, please report it.)

 
10/30/2004 4:24:57 PMJeff Mayes

All right, a harder problem, how do you prevent previous instances across a network when your app is on the server?

(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.