Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 149
  • Last Modified:

How to prevent multiple instances of a vb app?

I want to know how to stop a user from reopening an application if it is already opened on their pc.
0
hutchison
Asked:
hutchison
  • 3
  • 3
1 Solution
 
jbirkCommented:
I think you're asking this in the wrong section...
Are you talking about your own application or some other application like notepad?

If it's your own app, then it would have to be in whatever language you're using to develop it...  if not, I'm not sure how to do it, but you might have more luck on that in the EE category for the OS you're using.

If you're referring to something related web pages, then please elaborate.
-Josh
0
 
MasseyMCommented:
Here is the answer in Visual Basic:

'Taken from Microsoft Knowledgebase Article ID: Q185730
'Paste the following code into the code Module for Form1:
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
 
jbirkCommented:
MasseyM, you should know better than to answer a question like this when you're not even sure if it's the answer.  The protocol is to leave a comment if you think it might be what they want, and post it as an answer only if you are positive you know the correct answer.  In this case there is no way to know if that is exactly what hutchison wants, though it could be...

Anyway, you should know better!  You've been doing this for a while now!

-Josh

P.S.  I have this solution using almost the same method for C++, and a different way using memory mapped files in C++, if you're interested, hutchison.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
MasseyMCommented:
My apologies...
0
 
jbirkCommented:
Sorry I was so hard on you MasseyM.  Had a bad day....  but am glad you apologized too.
0
 
MasseyMCommented:
I can understand those bad days... No problems jbirk... Just trying to help the questioneer... I just assumed VB... Not sure why.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

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