• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

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
0
chee68
Asked:
chee68
1 Solution
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now