Solved

How to prevent multiple instances of a vb app?

Posted on 2000-02-29
6
142 Views
Last Modified: 2010-04-09
I want to know how to stop a user from reopening an application if it is already opened on their pc.
0
Comment
Question by:hutchison
  • 3
  • 3
6 Comments
 
LVL 8

Expert Comment

by:jbirk
ID: 2570737
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
 
LVL 10

Accepted Solution

by:
MasseyM earned 100 total points
ID: 2571005
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
 
LVL 8

Expert Comment

by:jbirk
ID: 2571303
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 10

Expert Comment

by:MasseyM
ID: 2571367
My apologies...
0
 
LVL 8

Expert Comment

by:jbirk
ID: 2571516
Sorry I was so hard on you MasseyM.  Had a bad day....  but am glad you apologized too.
0
 
LVL 10

Expert Comment

by:MasseyM
ID: 2571638
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question