Solved

How to prevent multiple instances of a vb app?

Posted on 2000-02-29
6
144 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting dates in JavaScript 2 51
Javascript_Set_dropdown_value 7 40
CSS: Making responsive table look nicer 7 31
Insert Button on a table 16 36
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 …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 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