Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disallowing multiple instances of a VB App 2

Status
Not open for further replies.

VB400

Programmer
Sep 8, 1999
359
0
0
US
<br>
How do I insure that my application will run only once on a desktop?<br>
<br>
I tried the API FindWindow in Sub Main upon the startup of the application thinking that if I get a non-zero then the application is already running. The problem is that as soon as the application starts, the FindWindow API returns the handle to the application itself (the current new instance)!<br>
<br>
Any ideas?<br>
<br>
Tarek
 
Try this in your app initialisation code<br>
<br>
If App.Previnstance then<br>
&nbsp;&nbsp;&nbsp;&nbsp;Unload Me ' or whatever will make your app go away<br>
&nbsp;&nbsp;&nbsp;&nbsp;Exit Sub<br>
End If<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
<br>
Thanks Mike , this works<br>
<br>
The problem is that it is too simple (-:<br>
<br>
Tarek
 
Hey Mike,<br>
I like that one it sure is small.<br>
<br>

 
I just found this other solution that is a tad more involved but will allow you to actually switch over to the running instance (Win95 and NT4 only -- will not work for Win98):<br>
<br>
Public Declare Function FindWindow Lib &quot;user32&quot; Alias &quot;FindWindowA&quot; (ByVal lpClassName As String, ByVal lpWindowName As String) As Long<br>
<br>
Public Declare Function SetForegroundWindow Lib &quot;user32&quot; Alias &quot;SetForegroundWindow&quot; (ByVal hwnd As Long) As Long<br>
<br>
Private Sub Form_Load()<br>
<br>
Dim hdl As Long<br>
Dim rc As Long<br>
<br>
hdl = FindWindow(vbNullString, &quot;The Real Caption&quot;)<br>
If hdl &gt; 0 Then<br>
rc = SetForegroundWindow(hdl)<br>
End<br>
Else<br>
Me.Caption = &quot;The Real Caption&quot;<br>
End If<br>
<br>
End Sub<br>
<br>
Here's how it works (there's a trick described later):<br>
<br>
The FindWindow API requires exact description of the Window you're trying to find. The API returns a handle to the Instance that's running. If the handle is 0 then there is no other instance; otherwise, we feed the handle to the SetForegroundWindow API to make that instance the front app.<br>
<br>
The trick is this: as mentioned in the original post, as soon as the application starts, the FindWindow will a return a reference to itself (the new instance). This means that we would really never know whether or not there is another instance. So here's what we do:<br>
<br>
At design time, change the caption of the main form to some &quot;dummy&quot; text. When the application starts, use the &quot;real&quot; caption as the parameter in the FindWindow API. If the API returns non-zero then switch to the app; otherwise, change the caption of the form to the &quot;real&quot; text and continue on with the app.<br>
<br>
It seems complicated but it is real simple and works well (on Win/95 and Win/NT only). Win98 and Win2000 do not allow you to change the focus to another application -- or something like that (do not quote me on this one)<br>
<br>
Remember, if you don't need to switch over to the running instance then use Mike's code above -- it's simple and very effective.<br>
<br>
Tarek
 
Thanks Tarek - interesting. I'm looking forward to seeing Dan Appleman's new book (I'm presuming that there will be one) about the Win2000 API - should be fun.<br>
-ml<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
I have tried the app.previnstance in order to end the project if there is one already open, but the problem is that the app.previnstanve always = false no matter how many instances of the project i have open. I use the app.previnstance command in the mainmodule sub main. does anybody have any ideas???
 
Really? What operating system and version of VB are you using?<br>
<br>
Mike<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Mike I am using vb5 and win 98. No matter how many times the application is open the App.PrevInstance always = false. This happens when I am running in debug mode and also when I build an exe with the code in it it still allows me to open multiple instances. I cannot figure out why. Do you have any other ideas how I can stop multiple instances???? All ideas welcome????
 
Sorry, guys, I find this thread almost laughable since my goal is to do the opposite: keep two instances of an app running, no matter what happens.<br>
The answer will be here.<br>
Not a question. But any post could be the solution to my problem..<br>

 
Gettoblaster - have you tried vb400's findwindow code? <p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top