How To Detect VB Running Application

Hi,

I have write a VB program and compiled to executable file.  I want to prevent user launch the program/application more than once.  If the program already launch, and user try to launch it again, i would like the program to activate the exsiting program automatically. Could it be done? How?

Thanks

chee
chee68Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VbmasterCommented:
You can use App.PrevInstance to see if the program is already running. If it is you can use close the new program. How to activate the existing app.. I don't know.. maybe by searching all the windows for the one with the right caption, that would be the most simple answer but that depends on what caption you have on ya main form?
0
SLECommented:
Put the following code in your start-up in your start-up object, e.g. Sub Main:

Sub Main()
  If App.PrevInstance Then
      AppActivate "My Application"
      End
  End If

....

End Sub


Note: "M Application" is the caption of your application.
0
CminusminusCommented:
Ok, a guy walks into a bar and order 21 shots of tequila then he goes home. He returns to the bar the next day and the bartender says "Hey wernt you that guy who order 21 shots of tequila". The man responded "why yes". The bartender asked "Well what hapend when you went home?". The man said "I blew chunks". The bartender said "Well of course after 21 shots of tequila". The man said "You dont understand chunks is my dog".
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

chee68Author Commented:
SLE, VBMaster,

I'll try out your suggestion.

Thanks

chee
0
JuiletteCommented:
sle..your code is incomplete..it does nohing with the pre existing app...you would have both open.
' this will check and if an existance then it will activate it and kill the new one'

' See if there is already and instance.
    If App.PrevInstance Then
        ' Activate the previous instance
        AppActivate App.Title
       
        ' Send a key (here SHIFT-key) to set the
        ' form from the previous instance to the
        ' top of the screen.
        SendKeys "+", True
       
        ' Terminate the new instance
        Unload Me
    End If

'if you go the other way and kill the
'old to activate the new...
''''

'from Microsoft Databse
'check for previous instance of app
'
Option Explicit

Private Sub Form_Load()

   If App.PrevInstance Then
      ActivatePrevInstance
   End If

End Sub

'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

'2) Add a Standard Module to the Project.
'3) Paste the following code into the module:

Option Explicit

Public Const GW_HWNDPREV = 3
Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Sub ActivatePrevInstance()

   Dim OldTitle As String
   Dim PrevHndl As Long
   Dim result As Long

   'Save the title of the application.
   OldTitle = App.Title

   'Rename the title of this application so FindWindow
   'will not find this application instance.
   App.Title = "unwanted instance"

   'Attempt to get window handle using VB4 class name.
   PrevHndl = FindWindow("ThunderRTMain", OldTitle)

   'Check for no success.
   If PrevHndl = 0 Then

      'Attempt to get window handle using VB5 class name.
      PrevHndl = FindWindow("ThunderRT5Main", OldTitle)
   End If

   'Check if found
   If PrevHndl = 0 Then
        'Attempt to get window handle using VB6 class name
        PrevHndl = FindWindow("ThunderRT6Main", OldTitle)
   End If

   'Check if found
   If PrevHndl = 0 Then
      'No previous instance found.
      Exit Sub
   End If

   'Get handle to previous window.
   PrevHndl = GetWindow(PrevHndl, GW_HWNDPREV)

   'Restore the program.
   result = OpenIcon(PrevHndl)

   'Activate the application.
   result = SetForegroundWindow(PrevHndl)

   'End the application.
   End

End Sub

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JuiletteCommented:
I guess I didn't make it overly clear above...this is one way the Microsoft is another.
' See if there is already and instance.
    If App.PrevInstance Then
        ' Activate the previous instance
        AppActivate App.Title
         
        ' Send a key (here SHIFT-key) to set the
        ' form from the previous instance to the
        ' top of the screen.
        SendKeys "+", True
         
        ' Terminate the new instance
        Unload Me
    End If


0
SLECommented:
Juilette,

"both open"? Did you spot the "End"? It works fine, I believe you're making things far too complicated.

You *are* right though about one thing: if the app is minimized it won't be set to the foreground (I did not make a call to SetForeGroundWindow).

;-)
0
JuiletteCommented:
Ooops...your're right...I didn't see it.

As far as too complicated...that's the MS version and I would think it covers all bases.
 
I bow out, humbly....your's will work, not as complete as the MS version, but it will work.

Wayne
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.