Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to prevent multiple instances of a vb app?

Posted on 2000-02-29
6
Medium Priority
?
148 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

721 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