×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Disallowing multiple instances of a VB App
2

Disallowing multiple instances of a VB App

Disallowing multiple instances of a VB App

(OP)

How do I insure that my application will run only once on a desktop?

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)!

Any ideas?

Tarek

RE: Disallowing multiple instances of a VB App

(OP)

Thanks Mike , this works

The problem is that it is too simple (-:

Tarek

RE: Disallowing multiple instances of a VB App

Hey Mike,
I like that one it sure is small.

RE: Disallowing multiple instances of a VB App

(OP)
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):

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long

Private Sub Form_Load()

Dim hdl As Long
Dim rc As Long

hdl = FindWindow(vbNullString, "The Real Caption")
If hdl > 0 Then
rc = SetForegroundWindow(hdl)
End
Else
Me.Caption = "The Real Caption"
End If

End Sub

Here's how it works (there's a trick described later):

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.

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:

At design time, change the caption of the main form to some "dummy" text. When the application starts, use the "real" 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 "real" text and continue on with the app.

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)

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.

Tarek

RE: Disallowing multiple instances of a VB App

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.
-ml

Mike Lacey
Mike_Lacey@Cargill.Com
Cargill's Corporate Web Site

RE: Disallowing multiple instances of a VB App

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???

RE: Disallowing multiple instances of a VB App

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????

RE: Disallowing multiple instances of a VB App

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.
The answer will be here.
Not a question. But any post could be the solution to my problem..

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close