Solved

How to prevent multiple instances of a vb app?

Posted on 2000-02-29
6
147 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
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.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

628 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