Only one instance of an application

Posted on 1999-07-13
Medium Priority
Last Modified: 2010-06-08
Dear Friends,
           I am writing a form based application in using VB 6.0 . Now the problem I am stuck with is that I want to find out if an instance of my application is already running so that if it is running then I should be able to maximize the window of the already running application thereby allowing only one instance of my application running at a time. Can this be done ?  
Question by:adarsh_bhat
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
LVL 13

Expert Comment

ID: 1524172
If App.PrevInstance then End

Accepted Solution

setiawan earned 300 total points
ID: 1524173
if app.previnstance = true then
 end if
LVL 10

Expert Comment

ID: 1524174
Guys, read the question!
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 10

Expert Comment

ID: 1524175
Create an activeX .exe

In this ActiveX .exe put one global multiuse class

This class will contain the following code

Option Explicit
Public Function NewInstance(objForm As Object) As Boolean

If gobjF Is Nothing Then
    Set gobjF = objForm
    NewInstance = True
    Exit Function
End If

NewInstance = False
gobjF.WindowState = vbMaximized

End Function
Public Sub EndInstance()

Set gobjF = Nothing

End Sub

Also, put in a normal VB module with
Option Explicit
Global gobjF As Object

Then, your VB project could contain something like this:
Option Explicit

Private Sub Form_Load()

If Not newinstance(Me) Then
End If

End Sub

Private Sub Form_Terminate()


End Sub

(Very roughly....)


Expert Comment

ID: 1524176
Is the caption of your main window constant? Then you could use the FindWindow API to find the window and then use the ShowWindow API with the SW_MAXIMIZE parameter to maximize that window. Like this:

Private Const SW_MAXIMIZE = 3
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long

Private Function Maximize(av_Caption As String) As Boolean

    ' Finds hWnd to Window w/ matching caption then Maximizes
    On Error GoTo Maximze_Error

    Dim lv_hWnd As Long
    Dim lv_RC As Long
    lv_hWnd = FindWindow(vbNullString, av_Caption)
    If lv_hWnd <> 0 Then
        Call ShowWindow(lv_hWnd, SW_MAXIMIZE)
        Maximize = True
        Maximize = False
    End If
    Exit Function
    Maximize = False

End Function

If the caption of the main window on the other hand is not constant you can perhaps save the hWnd of the main window (the one you want maximized) in the registry somewhere at startup of the app and use that value when App.PrevInstance = True.. Just remove the FindWindow API then and set lv_hWnd value to the value stored in the registry.
LVL 10

Expert Comment

ID: 1524177
Good idea, VBmaster!

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

719 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